|
The problem is that it's possible to move the paddle off the screen. This is not very
useful. We need to edit the code so that if the paddle moves past the beginning or end
of the bounding box it stops.
Change the code in the onClipEvent (enterFrame); function as follows.
if ( Key.isDown(Key.RIGHT) and this._x < 500-(this._width/2.0) ){
this._x+= paddle_speed;
} else if ( Key.isDown(Key.LEFT) and this._x > 50+(this._width/2.0) ){
this._x-= paddle_speed;
}
|
|
|
Next we need to change what happens when the ball reaches the bottom of the rectangle.
There are two things that can happen it.
1. It hits the paddle and bounces back in. 2. It leaves the area and the player loses.
|
|
|
We can use the hitTest() function to test whether the ball hits the paddle or not.
If it does hit the paddle we change the direction of the y component of the velocity.
The hit test function has the syntax
|
|
Type the following code into the action window for the ball
if (this.hitTest(_root.myPaddle) ) {
y_direction*=-1;
this._y+= y_direction*(random(randomness)+1)*y_speed;
}
| |
In order to make it a game we need to change the code that so that the ball doesn't
bounce off the bottom side of the rectangle. Change the code to the following
// This code keeps the ball below the upper side of the rectangle
if ( this._y > 50 ){
this._y+= y_direction*y_speed;
} else {
y_direction*=-1;
this._y+= y_direction*(random(randomness)+1)*y_speed;
}
|
|
|
Create a new layer called Game Over. Click on Frame 3 of this layer and give it a name "gameOver"
using the Frame panel and typing gameOver in the label box.
Now if the y position of the ball is below the paddle we call move to the gameOver frame. Type the following code into the Actions for the ball.
// This code calls the Game Over frame if the ball leaves the area
if ( this._y > 365 ) {
_root.gotoAndStop("gameOver");
}
We can make the ball disappear when the game is over by putting a blank key frame (F7) in frame 3 of the ball layer. Also to prevent the Game Over notice coming up at the start place a blank key frame (F7) in frame 1 of the Game Over layer. We also need to put a stop(); function in Frame 1 of the Ball Layer so that it doesn't continually restart a new instance each time it enter the frame. You should now have a basic game working. |
|