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

2016/5/6

Problem equals object

JonSkyWalker JonSkyWalker

2016/5/6

#
hey, i wanna ask what if i have a code like this :
if(getWorld().equals(Stage1.class)){
        hitBadGuy();
}else if(getWorld().equals(Stage1.class)){
hitBadGuy1();
}
the code i wrote is it true? because i want to use the hitbadguy method if the current stage is stage1 the hitbadguy1 if the current stage is stage2. please answer need help
danpost danpost

2016/5/6

#
JonSkyWalker wrote...
hey, i wanna ask what if i have a code like this : < Code Omitted > the code i wrote is it true? because i want to use the hitbadguy method if the current stage is stage1 the hitbadguy1 if the current stage is stage2.
A class is a class and an object is an object. Objects can be created from a class; but objects themselves are not classes or considered a type of class. They are of a class type, or created from a specific type of class; but, they are two distinct things. There is a way to ask what you are trying to here, however:
if (getWorld() instanceof Stage1)
Here we are asking if the world object that the actor is in is an instance of (or was created from) the 'Stage1' class (or any subclass of the 'Stage1' class). More to the point, is it a Stage1 object.
danpost danpost

2016/5/6

#
In a literal sense, a object can be a class -- that is a Class object is truly an object -- an object of type Class. But an object of type Class is never an object of type World. So, it is like comparing apples to bananas.
You need to login to post a reply.