×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Unable to update variables

    I am having a lot of grief with a program I'm writing. The program is an interactive mobile presentation of a project I'm working on at school.

    The user has three lifes to start with, and if the user gives an uncorrect answer the lifes decrease (or should decrease). If the user has 1-3 lifes the user should be redirected to the same slide, but different remaining life images should be displayed first. If I set lifes to a value, the correct image is displayed and the program redirects to the correct place in the program. However the lifes refuse to decrement for each wrong answer, so the player never dies.

    I have a dedicated class that takes care of the lifes:

    Code:
    public class CLifeCounterClass
    {
        private int lifes;
    
        CLifeCounterClass()
        {
            lifes = 4;
        }
    
        public int returnAndRemoveLifes()
        {
            if(lifes > 0)
            {
                lifes--;
                
                return lifes;
            }
            else
            {
                return 0;
            }
        }
    
        public int returnLifes()
        {
            return lifes;
        }
    }
    An object of CLifeCounterClass is created in DoConDaMobile.java which is the "main" class. No matter how many times I run CLifeCounterClass.returnAndRemoveLifes() lifes never seems to decrement. I have tried creating a variable lifes in DoConDaMobile, but the same thing happened there too. lifes refused to decrement. I have also tried using char and double - also with no luck.

    What I find very odd is that I have no trouble what so ever manipulating the boolean "musicOn" in the customSoundManager class.

    The complete sourcecode can be downloaded here --> http://www.arcticsilence.com/source.zip

    I greatly appreciate any advice I can get, as I am very desperate to get the very last bit of the program working.

    Regards

    Francis

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Unable to update variables

    I think your problem is not what you think. The class you post here is fine.

    In DoConDaMobile.commandAction(), you have two calls to:

    Code:
    int nrOfLifes = lifeCounter.returnLifes();
    So... these don't decrement.

    There are also two calls:

    Code:
    int lifes = lifeCounter.returnAndRemoveLifes();
    These do decrement. Two things I notice. First, these are both followed immediately by:

    Code:
    lifes--;
    So, you're decrementing the value again (but, only the local copy, not the one in the object).

    Second, these are both wrapped in if{}s, like:

    Code:
    if (Display_Lifes_1 == null) {
    }
    This appears to be where you create the Display_Lifes_1 object. But that variable never becomes null, so this code (and the call to returnAndRemoveLifes()) can only happen once.

    Calls to getDisplay_Lifes_1() will always return the same display, and will decrement the number of lives only the first time it is called.

    Is that intentional?

    I think your problem is that you are simply not calling returnAndRemoveLifes() as many times as you think.

    Cheers,
    Graham.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Re: Unable to update variables

    Thankyou for your reply!

    The extra lifes--; are not supposed to be there. They are reminents from when I tried to have lifes in the DoConDaMobile class.

    I will try and go through the program following your advice, and see what I can manage.

    Thanks again!

    Regards

    Francis

Similar Threads

  1. Replies: 1
    Last Post: 2008-08-24, 15:22
  2. Unable to update sync profil bye sms configuration
    By sonia_m in forum OMA DM/DS/CP
    Replies: 0
    Last Post: 2006-04-10, 10:37
  3. Replies: 3
    Last Post: 2005-10-12, 05:26
  4. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×