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

2020/3/26

accessing a variable of the world from an actor

AnotherUser AnotherUser

2020/3/26

#
Hey! So I've got a World with the following relevant variable and method:
    int newRound;
    
    public void setNewRound(){
        newRound = 1;
    }
now I try to access this variable from an actor with either
            getWorld().setNewRound();
(I created the method setNewRound() specifically for this purpose) or
getWorld().newWorld = 1;
but neither of those approaches seem to work. They both get an "cannot find variable/method" error. How do you do it?
Super_Hippo Super_Hippo

2020/3/26

#
‘getWorld()’ returns a World object. The class World doesn’t have this method. Your subclass of world has it. You need to cast the World object to a <insert the name of your world subclass here> object. For example if the name of your world subclass is ‘MyWorld’:
((MyWorld) getWorld()).setNewRound();
AnotherUser AnotherUser

2020/3/27

#
thank you very much!
You need to login to post a reply.