»»»Object
Collision Detection with Flash 5
One
of the core prerequisites for any game built with
Macromedia’s Flash is the ability to hit and react
to other objects on the stage. This is more often
referred to as “collision detection,” that is when
you hit something you get hit back. Your basic physics
class.
In this article
you will learn how to have movies in Flash collide
with other movies on the stage. To help demonstrate
this more completely we will be using the game BreakOut
as an example, as shown in Figure A.
Figure A: BreakOut
Game
BreakOut has
all of the basic features needed for collision detection.
By the end of this article you will know how to do
the following:
o
Understand the principles of
collision detection
o
Define, programmatically, the
area of a game
o
Define, programmatically, how
movies should react when they are hit.
You can download
the source code for this game from the Element K Journal’s
Web site.
The Game
Breakout is a 1976 classic.
First appearing on the Atari and then receiving the
honors of millions of clones. The basic foundation
for the game is to bat a ball around a screen and
hit blocks. Each block you hit gives you points. When
you have hit all of the blocks you go onto the next
screen. You have three lives. When all of the lives
are gone, the game ends.
The main object
of the game is to send a ball bouncing around the
screen. To do this you need to define the area of
the screen, what the ball to be bounced is and the
paddle from which the ball will be bounced from.
Area of the
Game
The first step is to
define where the ball will be bouncing. The size of
the movie for the BreakOut game is 400px x 500px.
This defines the size of the movie, not the area of
the game. The game area has to be defined programmatically
with ActionScript. The first scripts that will be
added will be in Frame 1 of the movie. We start out
by creating a variable,
called GamePlayArea, with the values calculated in
an array. It sounds complicated, but here is what
it looks like:
GamePlayArea = [0,
0, 400, 500];
Essentially,
what we are doing is telling Flash that the game will
cover an area 400 px x 500 px from the top left hand
corner of the screen. As we move through the game
code, we can reference the Variable
name GamePlayArea if we need to know the coordinates
of the area of the game.
The Ball
With the game play area
now defined, we need to add the ball that will bounce
around the stage. It is important that the ball is
referenced correctly, other wise the code will not
work. The first thing to drag onto the stage is an
instance of the movie clip ball. We are going to name
the instance of the ball “ball” as shown in Figure B.

Figure B: The
instance of the ball is name “Ball” By naming the
instance we can reference it in our script. With this
done, we can now begin adding the ActionScript that
will bounce the ball. All
of the script that controls the ball is placed in
the first frame of the movie. The first script you
will need to write will control how the ball moves.
The script is is:
newBall.prototype.moveBall = function
() {
this.MovieClip._x += this.deltaX;
this.MovieClip._y += this.deltaY;
//check if ball hits top of screen
if (this.MovieClip._y<0) {
this.flipY();
}
if (this.MovieClip._x<GamePlayArea[0] ||
this.MovieClip._x>GamePlayArea[2]) {
this.flipX();
}
}
if (this.MovieClip._x<GamePlayArea[0] ||
this.MovieClip._x>GamePlayArea[2]) {
this.flipX();
}
Here the movie
instance “ball” knows where the area of the game is
by referencing the variable
“GamePlayArea.” Earlier we told Flash that the game
area is only 400x500. The ball will now bounce off
an imaginary wall that is 400x500.
NEXT PAGE>>
Until next time..
Have fun
Click
here to print this page
------------------------------------------------------
****************************************
Written by Yury Rush Copyright (©) 2002 motionrush
media labs (link)
Creative & Intelligent website design located
In Sarasota, Bradenton, Venice Florida
Want this article for your website? Keep this resource
box intact & Links
working & it's yours! www.motionrush.com
****************************************
------------------------------------------------------