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

2015/11/30

Specifying a sub-class?

oscarreks oscarreks

2015/11/30

#
I'm working on a collision-checking method for a Player actor. The actor uses this:
Obstacle up = (Obstacle)getOneObjectAtOffset(0, 10, Obstacle.class);
...in order to get an Obstacle. Maybe my error is here, and I should be using a list or something. I dunno. Assuming the error isn't here, then later in the method, I need to know specifically what sub-class of Obstacle "up" is. This statment:
if(up == Rock.class || up == Spikes.class)
...doesn't work because it says Obstacle and Rock.class are incomparable. But shouldn't I be able to ask "up" if it a certain sub-class of Obstacle? Hope someone can help.
danpost danpost

2015/11/30

#
The 'instanceof' keyword is used to verify that an object is of a specific class:
if (up instanceof Rock || up instanceof Spikes)
oscarreks oscarreks

2015/11/30

#
danpost wrote...
The 'instanceof' keyword is used to verify that an object is of a specific class:
if (up instanceof Rock || up instanceof Spikes)
Many thanks danpost.
You need to login to post a reply.