Ping Pong - Moving Pads
| Category | : Flash | Views | : 18484 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : ping-pong-moving-pads.zip | ||
| Result | : See the result | ||||
| <<< Previous | Index Series | Next >>> |
In this tutorial we are going to make the Player's pad move with the arrow keys and the Opponent's Pad move automatically depending on where the ball is.
If you followed the last tutorial's instruction on where to put the Movie Clips you choose have two instances of the Pad Movie Clip inside "Pads", one named "Player" and the other "Opponent".
On the actions of the "Player" Movie Clip, paste this:
onClipEvent(enterFrame)
{
//If the player presses the Right Key
if(Key.isDown(Key.RIGHT))
{
//create an object to hold x and y values
point = new Object();
//set the x and y of point to be equal to the pad
point.x = this._x;
point.y = this._y;
//change the coordinate of point to be global
localToGlobal(point);
//if the point.x plus the speed of movement is bigger than the
//limit for the x coordinate we "return" and the next line
//of code wont be executed.
if(point.x + 10 >= 220)
{
return;
}
//if we are not going to pass the limit of
//movement, we move right by 10 pixels
this._x += 10;
}
//If pressed Left
else if(Key.isDown(Key.LEFT))
{
point = new Object();
//create a new object and set x and y.
point.x = this._x;
point.y = this._y;
localToGlobal(point);
//if the point.x minus the speed is less than the left limit. We return.
if(point.x - 10 <= -20)
{
return;
}
//move if it's not in the limit.
this._x -= 10;
}
}
You should have notice that we used the function localToGlobal("point"); it converts the coordinates from our movie clip to be global.
Inside "Pads", the _x of the player instance may be 13px and at the same time 50px in the global coordinates.
As I have the limits as global coordinates I need to convert the Movie Clip's _x and _y to compare them.
Now open the action for the Opponent instance and paste:
//When the Movie Clip loads
onClipEvent(load)
{
//this is the previous position of the Ball Movie Clip
oldX = _root.Ball._x
oldY = _root.Ball._y
//speed of the pad
speed = 5
}
onClipEvent(enterFrame)
{
//don't move the Movie Clip until we set a speed
speed = 0;
//if the current _x of the ball is bigger than the last one
//we added + 10 to the oldX to make a gap so this Movie Clip
//won't move if it is close to the ball
if(oldX + 10 < _root.Ball._x)
{
//speed positive to move to the right
speed = 2.5
}
//if ball moved to the left
else if(oldX - 10 >= _root.Ball._x)
{
//speed negative to move this Movie Clip to the left
speed = -2.5;
}
//if the ball is close to the pad
if(this._y < 70)
{
//increase the speed
speed = speed * 4;
}
//move the Movie Clip left or right
this._x += speed
//if the Movie Clip moved to far to the right
if(this._x > 60)
{
//go back
this._x -= speed
}
//limit to the left
if(this._x < -60)
{
//go back
this._x -= speed
}
//set oldX to current ball._x
oldX = _root.Ball._x;
}
![]() |
![]() |
![]() |
![]() |
![]() |
| <<< Previous | Index Series | Next >>> |






