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

2016/5/5

Problem to get's actor move along the X axis. Need Reply A.S.A.P please...

novalcahyo novalcahyo

2016/5/5

#
How can I get's my actor move along X axis from both side of the world (Left and Right)? Need Answer A.S.A.P please...
danpost danpost

2016/5/5

#
Just add an int direction field to the class (to have either a value of '1' or '-1' to be multiplied by the speed of the actor (the value used in the parameter of the 'move' call). So, instead of
move(speed);
// you would have
move(direction*speed);
To set the appropriate value to the field you can (1) pass it to the constructor of the class to have the constructor set its value, (2) call a method to set its value after creating the object, or (3) use the 'addedToWorld' method as follows:
public void addedToWorld(World world)
{
    if (getX() > world.getWidth()/2) direction = -1; else direction = 1;
}
novalcahyo novalcahyo

2016/5/6

#
and what about if I want to remove those actor while it is intersecting with other object?
danpost danpost

2016/5/6

#
novalcahyo wrote...
and what about if I want to remove those actor while it is intersecting with other object?
Do you mean this?
if (getOneIntersectingObject(ObjectOfClass.class) != null) getWorld().removeObject(this);
You need to login to post a reply.