Flash dodge ball game part 5 the ball


In this tutorial we will make an enemy for our small dodge ball game, so we will be making a ball, that bounces on the walls and kills our character when it touches him.


1.
First of we need to make the ball, so as you can see from the image below I just made an oval, with a gradient and a reflection.
Select it, right click and convert it to a movie clip.

In the properties panel name it ball, so we can refere to it later in code.

Photoshop Tutorial thumb picture


2.
Now go to the _root stage in flash, click the ball movie clip and go to the actionscript panel, type in the following code

onClipEvent (load) {
xspeed = random(5)+11;
yspeed = random(5)+11;
}
onClipEvent (enterFrame) {
this._x += xspeed;
this._y += yspeed;
}

first of we make it create to variables when the flash movie is loaded, xspeed and yspeed they are going to be a random number between 5 + 11.

then in the onclipevent (this event is raised repeatly) what this will do is to tell flash to move this._x and this._y (our ball) to its original posistion + the x,y speed variables.

try to test your movie now and you should see that the ball is moving, but we have one problem, it moves out of the stage, so we need to change its direction when it hits a wall, luckly we made walls in part 4.

Photoshop Tutorial thumb picture


3.
Now we will use the HitTest method to make boundaries for the ball.

So in the onClipEvent(enterFrame) type the following.

if (hitTest(_root.right_wall)==true)
{
xspeed /= -1;
}

What this does it to repeatly check if the ball is touctching the right wall, and if it does then change the x direction via xspeed /= -1;

test the movie and see what happens when the ball hits the right wall.

Now here is the code for all the walls

--------------------------------------
onClipEvent (enterFrame) {
this._x += xspeed;
this._y += yspeed;

if (hitTest(_root.right_wall)==true)
{
xspeed /= -1;
}
if (hitTest(_root.left_wall)==true)
{
xspeed /= -1;
}
if (hitTest(_root.top_wall)==true)
{
yspeed *= -1;
}
if (hitTest(_root.bottom_wall)==true)
{
yspeed /= -1;
}

}
--------------------------------------

4.
One last thing to add is to change the posistion for the ball when it hits our character.
So still in the onClipEvent (enterFrame) type in the following.

if (hitTest(_root.joe)==true)
{
_x = 0;
_y = 0;
}

Now test the movie.

Tip: you could change the x_ and _y to some random positions for the ball when it hits our charector.

if (hitTest(_root.joe)==true)
{
_x = random(200);
_y = random(200);
}

 

Part 1 Part 2 Part 3 Part 4 Part 5 Part 6 Part 7

0tutor.com rss feed