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

2016/1/5

if this OR that?

TheSilverWaters TheSilverWaters

2016/1/5

#
Let's say I wanted a player to advance to world1 if an actor was in world2 OR in world 3. Is there some sort of or statement? Here's what I have, but if I had like 5 different worlds that would advance to world 1, it would get over-redundant. if (getWorld().getClass() == World2.class) { Greenfoot.setWorld(new World1()); } else if (getWorld().getClass() == World3.class) { Greenfoot.setWorld(new World1()); }
danpost danpost

2016/1/5

#
There is an OR operator in java. It is double bar symbol ( '||' ). It would be more readable using the 'instanceof' keyword instead of comparing classes:
if (getWorld() instanceof World2 || getWorld() instanceof World3)
However, if all worlds the actor could be in leads to world1, then there is no reason to even ask. And, if you did not want a new world1 created if the actor was already in a world1 world, then the condition would be that the actor is not in world1:
if (!(getWorld() instanceof World1))
You need to login to post a reply.