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

2019/6/19

What am I doing wrong here? (Variables)

Kozume Kozume

2019/6/19

#
So I am currently working on a game for a school project and in order to not have to create a new actor for every level I've decided to let the Actor count the levels, then adapt to it. But the Variable doesnt seem to be changable and I do not know why, can someone help me? This is the important part of the code (I've tried with two levels firstly):
    public void act()
    {
      controlls();
      checkFalling();
      checklevel();
      getWorld().showText(""+a, 10, 10);
    }
    
    public void checklevel()
    {
      if (a == 1)
      {
        if (getY()>700 )
        {
           Greenfoot.setWorld(new Level1());
        }

        if (getX()==1650 && getY()<520 && getY()>480)
        {
           Greenfoot.setWorld(new Level2());
           a = a + 1; //I already tried a+=1; and a++;
        }
      }

      if (a == 2)
      {
        if (getY()>700 )
        {
           Greenfoot.setWorld(new Level2());
        }
        
        if (getX()==850 && getY()<200 && getY()>100)
        {
            Greenfoot.setWorld(new Level2());
            a = a + 1;
        }
      }
    }
danpost danpost

2019/6/19

#
How is the variable a declared?
Kozume Kozume

2019/6/19

#
danpost wrote...
How is the variable a declared?
int a = 1;
coder123 coder123

2019/6/19

#
can you show me your class code(the entire class)
danpost danpost

2019/6/19

#
Kozume wrote...
int a = 1;
That would be an instance variable -- meaning each instance of the class will have its own value for a similar field with the name -- a. That is why it does not appear to change. Each level creates a new instance of the actor. You can either use the same actor instance in all your worlds or change the field to a class variable. As a class field, you will need to set its initial value in your initial world constructor. Use to following to make it a class field:
static int a;
When initializing the value of the field in your initial world constructor, use the class name where the field is located, plus ".a" (ex. "Player.a") to refer to the field.
Kozume Kozume

2019/6/19

#
danpost wrote...
Kozume wrote...
int a = 1;
That would be an instance variable -- meaning each instance of the class will have its own value for a similar field with the name -- a. That is why it does not appear to change. Each level creates a new instance of the actor. You can either use the same actor instance in all your worlds or change the field to a class variable. As a class field, you will need to set its initial value in your initial world constructor. Use to following to make it a class field:
static int a;
When initializing the value of the field in your initial world constructor, use the class name where the field is located, plus ".a" (ex. "Player.a") to refer to the field.
Does that also occur when the initialisation is in the same class?
Kozume Kozume

2019/6/19

#
Nevermind, it worked, thank you so much man
You need to login to post a reply.