Learn Create Your Own Split Screen

Next Page

To express that idea as code, first we calculate the x and y distances. We do this using the centre point of the sprite (its top-left plus half its height-width):
var distance_x = target_x - (this.x + this.MyImg.width / 2); var distance_y = target_y - (this.y + this.MyImg.height / 2);
This all happens inside our Move_Towards method, of course.

Then we set the velocity equal to the distance. Only thing is, we don't want it to make the journey in a single frame -- that would be too quick and we wouldn't see a thing. We want it to take some time so we can watch it happen. We divide the distance by ten, so it will take ten frames to travel the distance.

this.velocity_x = distance_x / 10; this.velocity_y = distance_y / 10;
Try waving your mouse or finger around and watch the jellyfish move.

Try changing the 10 to a larger or smaller number. What happens to the speed of motion?