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

2021/3/11

Getting Int from another class out of another world

GambleEnte GambleEnte

2021/3/11

#
I want a int from Button from another World and i don't know how to get it. This is just some code from my project. Enemy is in world "FrogWorld"
public class Enemy extends Actor
{
    private int timer = ((Options) getWorld().getObjects(Button.class)).getDifficulty();
    private int counter= ((Options) getWorld().getObjects(Button.class)).getDifficulty();
}
"Options" is the world, where Button exist.
public class Button extends Actor
{
int difficulty=70;
public int getDifficulty() 
    {
        return difficulty;
    }
}
danpost danpost

2021/3/11

#
GambleEnte wrote...
Enemy is in world "FrogWorld" (Options) getWorld()
The call to getWorld will not ever return an Options world unless the enemy is put in an Options world. Since it is put in a FrogWorld, it will return a world of that type, or null, and the casting as an Options world will fail. To reach an inactive world, there must exist a reference to that world -- in your case, a field of type Options, referencing the world in question). That field should probably be maintained in your FrogWorld class, so an actor in that world can gain access to it. Btw, I wrote "or null" above because it appears you are trying to acquire the world prior to the enemy being put in any world.
GambleEnte GambleEnte

2021/3/11

#
danpost wrote...
GambleEnte wrote...
Enemy is in world "FrogWorld" (Options) getWorld()
The call to getWorld will not ever return an Options world unless the enemy is put in an Options world. Since it is put in a FrogWorld, it will return a world of that type, or null, and the casting as an Options world will fail. To reach an inactive world, there must exist a reference to that world -- in your case, a field of type Options, referencing the world in question). That field should probably be maintained in your FrogWorld class, so an actor in that world can gain access to it. Btw, I wrote "or null" above because it appears you are trying to acquire the world prior to the enemy being put in any world.
Can you help me implementing this? I'm not the best in coding.
danpost danpost

2021/3/11

#
GambleEnte wrote...
Can you help me implementing this? I'm not the best in coding.
You will need to show what codes you have so far.
GambleEnte GambleEnte

2021/3/11

#
danpost wrote...
GambleEnte wrote...
Can you help me implementing this? I'm not the best in coding.
You will need to show what codes you have so far.
I uploaded it to my account.
GambleEnte GambleEnte

2021/3/11

#
I think I got it to work, thanks.
danpost danpost

2021/3/11

#
GambleEnte wrote...
I uploaded it to my account.
I took a look. I think you can just make the difficulty field in the Options class static. Then in your Enemy class, you can refer to it as in the following line:
int counter = Options.difficulty;
You need to login to post a reply.