Tips     Beginners     Intermediate     Advanced     Resources     E-mail me

Arkanoid/Breakout

Part 4:
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;
			}
		
Now the moveable paddle will stay in the middle of the screen.
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

movie1.hitTest(movie2); This will be true if the movie1 and movie2 overlap.


In order to test this we need to name the paddle. You might think the paddle has a name but it doesn't yet. The paddle is the symbol from the library, like a class in Java. We need to name the paddle say something like myPaddle. To do this click on the paddle and bring up the instance panel.

Instance Panel for paddle
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;
				}		
		
You'll notice that we've just deleted the and statement. However if the ball doesn't hit the paddle and continues outside the box we need to end the game.
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.

Back to top    Previous
Hosted by www.Geocities.ws

1