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

2015/7/31

How to access Space methods

hubert14 hubert14

2015/7/31

#
So, long story short, Space is a subclass of world. I wrote a few methods in Space that a few actors have to change, such as a setVariable(int var) method. When I use getWorld(), I can't access the new methods. I can't create a Space s = new getWorld(); for obvious reasons. So how do I add my actors to Space and call the Space methods? Does that make sense?
danpost danpost

2015/7/31

#
You cannot use 'getWorld()' alone because it returns a reference to a World object, not a Space object -- so the compiler does not know to look in the Space class for those methods unless you inform it that the returned object is indeed a Space object. This is does by casting its type:
((Space)getWorld()).setVariable(var);
// or
Space space = (Space)getWorld();
space.setVariable(var);
You need to login to post a reply.