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

2020/4/2

Using methods from another class

resh47 resh47

2020/4/2

#
     if(getOneObjectAtOffset(0, 0, Player.class) != null){
            GameOverState gameoverstate = new GameOverState((GameWorld)getWorld());
            getWorld().setState(gameoverstate);
        }
im trying to access the setState method from an actor class, how do I fix this
danpost danpost

2020/4/3

#
In line 3, you need to typecast the world as a GameWorld object (just as in line 2). To avoid double work, you can set the world to an object reference variable prior to line 2:
if (getOneObjectAtOffset(0, 0, Player.class) != null) {
    GameWorld gWorld = (GameWorld)getWorld();
    gWorld.setState(new GameOverState(gWorld));
}
It allowed for the rest to be pretty short as one line, so I combined your lines 2 and 3 as my line 3.
You need to login to post a reply.