Ping Pong - Ball Movement
| Category | : Flash | Views | : 40084 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : ping-pong-ball-movement.zip | ||
| Result | : See the result | ||||
| Index Series | Next >>> |
this._x += xspeed;
this._y += yspeed;
Move the Ball to desired direction.
if (this._y > 200)
{
If the Ball moves below 200px the player didn't catch the Ball.
_x = (Math.random() * 80) + 10;
_y = (Math.random() * 180) + 10;
Reset the ball coordinate.
dir = Math.round(Math.random() * 1);
speed = 10;
if (dir == 1)
{
var Ang = 45;
} else {
var Ang = 135;
}
xspeed = speed * Math.cos((Ang) * Math.PI / 180);
yspeed = speed * Math.sin((Ang) * Math.PI / 180);
}
And get new direction and speeds just like on the load event.
else if (this._y < 0)
{
If the Ball is above 0px the PC didn't catch the Ball.
_x = (Math.random() * 80) + 10;
_y = (Math.random() * 180) + 10;
dir = Math.round(Math.random() * 1);
speed = 10;
if (dir == 1)
{
var Ang = -45 ;
} else {
var Ang = 225;
}
xspeed = speed * Math.cos((Ang) * Math.PI / 180);
yspeed = speed * Math.sin((Ang) * Math.PI / 180);
}
}
Above is almost the same code as before; we just use different angles as we want the Ball to start moving up instead of down. For that we use the angles -45 and 225.This is all in the ball's movement. Next tutorial is about moving the player pad.
More cool tech articles from other blogs.
| Index Series | Next >>> |
