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

2014/12/10

How do i make it so an eneamy will move from left to right only a certain amount

aaronmckenzie aaronmckenzie

2014/12/10

#
I want my enemies in my donkey Kong game I'm making to move from left to right(right to left) and continue on a loop but i don't want them to say move off of a platform.
danpost danpost

2014/12/10

#
If the platform is not moving also, you can just have it turn 180 when the edge of the platform is reached. (1) move (2) check for platform below - move back and turn 180 if no platform below I am not saying to use 'turn(180)' for turning around. You probably need to change or mirror the image and negate a direction field.
danpost danpost

2014/12/10

#
There is another way to do this; however, it takes some extra steps when the actor is placed in the world. But, there will be no checking for the platform during movement. It involves using the 'addedToWorld(World)' method to determine the range of movement allowed and saving the results for use when moving. Like, the X coordinate of the platform would be the center point of the actors movement and half the width of the platform would be the range from that point. If, for example 200 was the X of the platform stored in a field called 'centerX' and the platform image was 120 wide stored as half that value in a field called 'range', and if your actor had a direction field that held positive one for moving right and negative one for moving left called 'direction' (this could be used as a factor in the 'move' command), then :
move(direction * speed);
if ((getX()-centerX)*direction >= range) {
    direction = -direction;
    // change or mirror image if needed
}
should restrict the movement to between 140 and 260 -- on the platform.
aaronmckenzie aaronmckenzie

2014/12/10

#
Thanks ill try this out
You need to login to post a reply.