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

2012/12/13

How to detect what object is currently using a global method?

UnclePedro UnclePedro

2012/12/13

#
In my Enemy.class, which Rat.class and Turtle.class extend, they both use a collision method that turns their direction around and reverses their speed. How can I detect which one of these classes is using the method when I change the variables? For instance, when the rat hits the wall, I don't want the turtle to turn around as well.
UnclePedro UnclePedro

2012/12/13

#
Well, I figured it out kinda. I'm using a State class to assign variables to the different monsters. Is this the correct way to do this?
danpost danpost

2012/12/13

#
I think that all you need is to put an instance boolean variable in the Enemy class, call it 'reversed'. Each object created from that class or any subclass will have a copy of that field for its use. You can also add the following method within the Enemy class to return the state of any object of that class (or subclass):
public boolean getReversedState()
{
    return reversed;
}
Just change the state of the 'reversed' field every time you turn the object around.
You need to login to post a reply.