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

2015/10/7

How to make some specific character move zig zag

ajikresna ajikresna

2015/10/7

#
Hi, im a newbie here, can you guys tell me how to make a specific class move zig zag around the world? thanks :D
danpost danpost

2015/10/7

#
I believe you are wanting that your actor move to a random point in the world; and then to another, and then to another, etc. One way is to have int fields to hold the following: (1) the x-coordinate of the starting point, 'x0' (2) the y-coordinate of the starting point, 'y0' (3) the x-coordinate of the destination point, 'x1' (4) the y-coordinate of the destination point, 'y1' (5) the total number of steps required to move from the destination point, 'reqSteps' (6) the number of steps processed from the starting point, 'steps' (7) the speed of the actor (pixels per move), 'speed' Then, each act that the number of steps is not reached: (1) add one the the step counter, 'steps' (2) move to the starting location, '(x0, y0)' (3) move the distance calculated by the following formula: speed*steps Otherwise: (1) save new starting coordinate values, 'x0' and 'y0', using current location of actor (2) assign new random destination coordinate values, 'x1' and 'y1' (3) turn toward the new destination, '(x1, y1)' (4) reset the step counter, 'steps' (5) calculate new required number of steps to reach destination using the following formula: Math.hypot(y1-y0, x1-x0)/speed Since 'x1' and 'y1' are only used when a new destination point is being chosen, they do not need to be instance fields, but can be made local variables within the code-block where the number of steps required is calculated.
You need to login to post a reply.