Ping Pong - Score
| Category | : Flash | Views | : 18887 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : ping-pong-score.zip | ||
| Result | : See the result | ||||
| <<< Previous | Index Series | Next >>> |
Now let's make the game get difficult.
Every time the ball bounces on the pads or on the wall we need to increase the speed, this is also very easy.
Just add this code to the collision detection inside the Ball Movie Clip:
if (_root.Limits.Border.hitTest(this._x + xspeed, this._y + yspeed, true))
{
xspeed = -xspeed;
//if xspeed was 2; 2 * 1.05 = 2.1
xspeed *= 1.05;
//if yspeed was 2; 2 * 1.05 = 2.1
yspeed *= 1.05;
}
if (_root.Limits.Pads.hitTest(this._x + xspeed, this._y + yspeed, true))
{
yspeed = -yspeed;
//the same thing as above
xspeed *= 1.05;
yspeed *= 1.05;
}
Code Explanation
Most of the codes here have already been explained in the previous tutorial. We will just discuss the code that we have made changes.
var opScore = 0;
var plScore = 0;
The first variable is used for the opponent's score and the second the player's score.
if (this._y > 300)
{
_x = (Math.random() * 180) + 10;
_y = (Math.random() * 280) + 10;
dir = Math.round(Math.random() * 1);
speed = 15;
The rest of this code was already explained in the first tutorial of this game.
_root.opScore++;
We need to add this line, so we increase the opponent score by one every time the player didn't catch the ball.
else if (this._y < 0)
{
_x = (Math.random() * 180) + 10;
_y = (Math.random() * 280) + 10;
dir = Math.round(Math.random() * 1);
speed = 15;
_root.plScore++;
Here, the code is used to tncrease the player score by one every time the opponent didn't catch the ball.
if (_root.Limits.Border.hitTest(this._x + xspeed, this._y + yspeed, true))
{
xspeed = -xspeed;
xspeed *= 1.05;
yspeed *= 1.05;
The last two lines are used to increase the speed of the ball every time it hits a wall.
if (_root.Limits.Pads.hitTest(this._x + xspeed, this._y + yspeed, true))
{
yspeed = -yspeed;
xspeed *= 1.05;
yspeed *= 1.05;
The last two lines increase the speed of the ball every time it hits a pad.That all for this tutorial, in the next one we are going to make the game pause and add a scrolling text on it.
More cool tech articles from other blogs.
| <<< Previous | Index Series | Next >>> |
