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

2015/10/10

Help With getWorld()

Elexios Elexios

2015/10/10

#
i need help with checking what world the actor is in only the first line of code needs fixing. there are 2 sub classes of world called Level and Level2. the only bit that doesn't work it is doesn't recognize Level in the first line. i kind of know why it doesn't work as i think i need to specify where it needs to look for it in but i'm new and don't know enough.
if (getWorld() ==  	Level)
         if (x == 26)
          Greenfoot.setWorld(Level2) ;
Thanks :)
danpost danpost

2015/10/10

#
I believe what you need to ask is this:
if (getWorld() instanceof Level)
davmac davmac

2015/10/10

#
You want 'instanceof' as danpost says. Just be aware that it returns true if the world is an instance of the named class or any subclass. So if Level2 was a subclass of Level, then 'getWorld() instanceof Level' would return true if the world was either a Level or Level2 instance. From your description it doesn't sound like that's the case, so I think using instanceof is fine. If you did need to confine the check to a single class, use:
if (getWorld().getClass() == Level.class)
You need to login to post a reply.