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

2018/11/23

accesor

destroyer9909 destroyer9909

2018/11/23

#
 if(Greenfoot.mouseClicked(this))
        {
            if(type.equalsIgnoreCase("ninja"))
            {
                Greenfoot.playSound("select.mp3");
                hero = "ninja";
                Greenfoot.setWorld(new Level1());
            }
 public String getHero()
    {
        return hero;
    }
is there any way to get the string value in the level 1 world class?
danpost danpost

2018/11/23

#
destroyer9909 wrote...
<< Code Omitted >> << Code Omitted >> is there any way to get the string value in the level 1 world class?
Yes, but a "getter" method (as shown) would not be that most direct way. For the Level1 world to use the method, it would need to have a reference to the world that has the method (the selection world). Best would be to pass the string to the Level1 world constructor. First, in your Level1 class, change:
public Level1() {
to
public Level1(String hero) {
Next, change line 7 above to:
Greenfoot.setWorld(new Level1(hero));
Then you can deal with its value right there in the constructor. Please note that the variable is local to the constructor and must be somehow dealt with there in the constructor -- whether saving it to an instance field for use later or utilizing its value to create the proper state of the actor.
destroyer9909 destroyer9909

2018/11/23

#
So I don't need the accessor anymore?
danpost danpost

2018/11/23

#
destroyer9909 wrote...
So I don't need the accessor anymore?
If that was the only reason for having it, then I guess not. Also, I cannot come up with any possible reason for needing it.
You need to login to post a reply.