This site requires JavaScript, please enable it in your browser!
Greenfoot back
Phytrix
Phytrix wrote ...

2013/6/9

Issues with variable

Phytrix Phytrix

2013/6/9

#
So basically, I have a class which I'll call ClassA. I then have ClassB which is a child class of ClassA. ClassA has a variable called question. At the top of the document, it's defined as...
public int question;
So in my World class, I add an object of ClassA. This works fine, and the setImage is random based on this variable. Although, when I attempt to access the variable question from ClassB (the child class), it's defined as 0. In troubleshooting, I set the variable as 10 as default, and then changed it. Although, ClassB still thinks of the variable as 10 in this case. I think the issue is with updating the variable, although I'm not entirely sure... Here's the code for ClassA...
public class ClassA extends Actor
{
    public int question;
    public int size = 25; // size of text for child classes
    public int nonRepeat = 1;

    public void act() 
    {
        if(nonRepeat == 1)
        {
            questions();
            nonRepeat = 2;
        }
    }    

    public void questions()
    {
        if(Greenfoot.getRandomNumber(3) == 0)
        {
            setImage("AAA-3.png");
            question = 0;
            getImage().setTransparency(100);   
        }
        else if(Greenfoot.getRandomNumber(3) == 1)
        {
            setImage("AAO-1.png");
            question = 1;
            getImage().setTransparency(100);   
        }
        else if(Greenfoot.getRandomNumber(3) == 2)
        {
            setImage("AAO-4.png");
            question = 2;
            getImage().setTransparency(100);   
        }
And here's what the code looks like for ClassB...
public class ClassB extends ClassA
{
   
    public void act() 
    {
            setImageIncorrect();            
    }    

    public void setImageIncorrect()
    {
        if(answer == 0)
        {
            setImage(new GreenfootImage("EAE-1", size, Color.WHITE, Color.BLACK));
        }
        else if(answer == 1)
        {
            setImage(new GreenfootImage("AAA-3", size, Color.WHITE, Color.BLACK));
        }
        else if(answer == 2)
        {
            setImage(new GreenfootImage("IAO-2", size, Color.WHITE, Color.BLACK));
        }
Zamoht Zamoht

2013/6/9

#
Your problem is that ClassB overwrites the act method meaning that questions() won't get called. Unless this is on purpose you can fix it by copy pasting the code from act() in ClassA to ClassB or you can just write super.act(); in ClassB's act method.
Gevater_Tod4711 Gevater_Tod4711

2013/6/9

#
If I understod you right your problem is that when you change the variable question in an instance of ClassA the value is not changed in ClassB. This is because every instance of the classes (A or B) has it's own variable question and you have to change every value on your own. If you change the value in one instance of ClassA in another instance of this class the value will not be changed too. If you want the value of question to be the same for every instance of ClassA, ClassB and all other subclasses you need to declare it static: public static int question;
You need to login to post a reply.