»»»
Object Collision Detection with Flash 5
part 2
To
test this out, we are going to add a paddle to the
game, as shown in Figure C. You will be able to control
the movement of the paddle with your arrow keys. The
paddle will hit the ball and bounce it off the walls.
Figure C: The
Bouncing Ball
The paddle bounces the ball off the walls The following
script adds the movement:
for(thisPad=0;thisPad<2;thisPad++){
var thePaddle = ThePlayer[thisPad];
if (this.MovieClip.hitTest(thePaddle) &&
this.deltaY > 0) {
this.paddleHit(thePaddle);
}
}
Here, the movie
clip method “hitTest” is used. The hitTest method
allows an object to physically collide with another.
In this case, the ball can collide with the paddle.
Once a collision has occurred an event can be triggered.
In this instance, the value to the variable
deltaY is triggered. This causes the ball to be sent
back up the screen.
Some more script can
be added to remove a life from the player is they
miss the ball with the paddle. That script is:
if (this.MovieClip._y>(GamePlayArea[3]+20))
{
theBallsList[this.name] = 0;
removeMovieClip(this.MovieClip);
CurrentBallNum--;
if (CurrentBallNum<=0) {
ballsLeft--;
if (ballsLeft<0) {
ballsLeft = 0;
gameOn = 0;
gotoAndStop (5);
} else {
addNewBall();
}
}
}
What we see
in this scipt is that Flash is looking for the current
position of the ball in relationship to the GamePlayArea.
If the ball falls below a specific point, ie: where
the paddle is, then player automatically looses a
life.
Hitting the
Blocks
The blocks across the
screen are controlled programmatically. Each block
is an instance of the same block. A color effect is
applied to each to change the color of the blocks.
In addition, the following script is also applied
to each block:
onClipEvent
(load) {
startX = this._x
}
This script
will be used to tell the block to hide. The only difference
between each block is the instance name. Each block
has a different name. The names for each all start
the same Tile and then followed by a number between
1-35. At the top of the script you tell the game how
many blocks are on the stage:
TileNum = 36;
You then tell
Flash how to calculate that the object you have just
hit is a block:
function maketargetList
(numOfTiles) {
for (i=0; i<numOfTiles; i++) {
TargetList[i] = String("tile"+(i+1));
}
}
The final step
is to add the script that will do something when a
block when it is hit. That is done with:
//check if ball hits a tile
var theTileNum = TargetList.length;
for (i=0; i<theTileNum; i++) {
var thisTile = TargetList[i];
if (this.MovieClip.hitTest(_root[thisTile]))
{
this.increaseSpeed();
Total += (10 * CurrentBallNum);
_root[thisTile]._x += -1000;
TargetList.splice(i, 1);
this.flipY();
if (TargetList.length == 0) {
resetGame();
}break;
}
}
Here the script
verifies that a target is hit and then increases the
games score by 10 points after the collision.
Now you have the basics
to a breakout game. The complete code can be downloaded
from the web site. The key to any game is colliding
with an object. This may be in the form a ball, a
laser gun or a rampant Pac-Man eating powerpills.
The key is to fully understand how to do this programmatically
through ActionScript. Here you have learnt how to
use the HitTest method and programmatically define
the area of a game. The next step if for you to develop
the game further to allow for more sophisticated control
and scoring.
----------------------------------------------------------------------------------------------
Matthew David is
a long standing evangelist for Macromedia’s product
line. He has contributed to some of the best selling
Macromedia books, such as The Dreamweaver Bible (IDG/Hungry
Minds), Flash 5 Magic (New Riders) and Inside Flash
MX (New Riders). He has also written Flash MX Magic
(New Riders) and Building Great Games with Flash MX
(Wiley).
He is a regular
contributor to Macromedia’s Designer/Developer site,
InformIT.com, Element K Journals’ Macromedia Solutions
and MX Insite Magazines.
http://www.matthewdavid.ws
Matthew works
as a freelance consultant. He can be contacted at
262-488-0026
----------------------------------------------------------------------------------------------
<<PREVIOUS
PAGE
Click
here to print this page