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

2013/1/18

how to get acces of the World class with an actor class

schoki7 schoki7

2013/1/18

#
Hey, right now we are trying to program a level based zelda fake. :D and our main problem is that we cannot get access of a variable in World class with an actor class. So basically in the World class we created three hearts and now we want to have access in actor class act at one of these hearts. And then later remove it. We kind of already have a solution with: Object herz = ((getWorld()).getObjects(Herz.class)).get(0); Actor herz2 = (Actor) herz; if(bombe != null) { getWorld().removeObject(bombe); getWorld().removeObject(herz2); it would be awesome if you could help me. =D PS: sry I know in theory how to write access (btw i can't edit the topic name)
danpost danpost

2013/1/18

#
To access a variable in your sub-class of World (not the World class, which is the super-class of the world where you placed the variable), You need to cast what is returned with 'getWorld()' to that which you named your sub-class of World. If your sub-class of world was named 'MyWorld', then:
World world = getWorld();
MyWorld myworld = (MyWorld) world;
// or more simply
MyWorld myworld = (MyWorld) getWorld();
// if your varaible was named 'counter', then
myworld.level++;
// or (again) more simply
((MyWorld) getWorld).level++;
provided that the variable has public (or undisclosed) access.
You need to login to post a reply.