Flash game creation tutorial - part 5.3
Filed Under Flash •
Let's go with another step.
In this part, we'll learn how to (almost) complete our first game... a tunnel race game.
Read tutorials from 1 to 5.2 if you haven't done it already and follow me in the game creation.
I'll continue from the code optimization seen in part 5.2, to continue adding features to the scrolling game.
Adding a timer
If you plan to make a racing game, the timer is very important. Some of the topics covered in this example are taken from the Flash simple timer/countdown tutorial.
-
// attaching movies
-
_root.attachMovie("kira", "kira", 8000, {_x:234, _y:159});
-
_root.attachMovie("wall", "wall", 10000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("bg", "bg", 2000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("count_down", "count_down", 12000, {_x:0, _y:0});
-
// kira setup
-
yspeed = 0;
-
xspeed = 0;
-
wind = 0.00;
-
power = 0.65;
-
gravity = 0.1;
-
upconstant = 0.75;
-
friction = 0.99;
-
// timer setup
-
start_time = getTimer();
-
countdown = 60000;
-
kira.onEnterFrame = function() {
-
// timer
-
elapsed_time = getTimer()-start_time;
-
_root.count_down.time_left.text = time_to_string(_root.countdown-elapsed_time);
-
// keys
-
if (Key.isDown(Key.LEFT)) {
-
xspeed = xspeed-power;
-
}
-
if (Key.isDown(Key.RIGHT)) {
-
xspeed = xspeed+power;
-
}
-
if (Key.isDown(Key.UP)) {
-
yspeed = yspeed-power*upconstant;
-
}
-
if (Key.isDown(Key.DOWN)) {
-
yspeed = yspeed+power*upconstant;
-
}
-
// speed calculation
-
xspeed = (xspeed+wind)*friction;
-
yspeed = yspeed+gravity;
-
if (xspeed>0) {
-
this.kira.gotoAndStop(1);
-
} else {
-
this.kira.gotoAndStop(2);
-
}
-
// screen update
-
_root.wall._y -= yspeed;
-
_root.wall._x -= xspeed;
-
_root.bg._y -= yspeed;
-
_root.bg._x -= xspeed;
-
// collision
-
if (_root.wall.hitTest(this._x, this._y, true)) {
-
xspeed = 0;
-
yspeed = 0;
-
_root.wall._x = 240;
-
_root.wall._y = 0;
-
_root.bg._x = 240;
-
_root.bg._y = 0;
-
}
-
};
-
function time_to_string(time_to_convert) {
-
elapsed_hours = Math.floor(time_to_convert/3600000);
-
remaining = time_to_convert-(elapsed_hours*3600000);
-
elapsed_minutes = Math.floor(remaining/60000);
-
remaining = remaining-(elapsed_minutes*60000);
-
elapsed_seconds = Math.floor(remaining/1000);
-
remaining = remaining-(elapsed_seconds*1000);
-
elapsed_fs = Math.floor(remaining/10);
-
if (elapsed_hours<10) {
-
hours = "0"+elapsed_hours.toString();
-
} else {
-
hours = elapsed_hours.toString();
-
}
-
if (elapsed_minutes<10) {
-
minutes = "0"+elapsed_minutes.toString();
-
} else {
-
minutes = elapsed_minutes.toString();
-
}
-
if (elapsed_seconds<10) {
-
seconds = "0"+elapsed_seconds.toString();
-
} else {
-
seconds = elapsed_seconds.toString();
-
}
-
if (elapsed_fs<10) {
-
hundredths = "0"+elapsed_fs.toString();
-
} else {
-
hundredths = elapsed_fs.toString();
-
}
-
return minutes+":"+seconds+":"+hundredths;
-
}
Line 4: Nothing to do with the timer, but I added a background to the cavern. The way I added the background is the same I added the main cavern. Look at the depth (0)... the background is behind all other objects
Line 5: Here I add the movieclip that will contain the timer. Look at the depth (12000)... the timer is in front of all aother objects. In the timer object I have a textarea called time_left that will be updated. We will add all texts covered in this tutorial in the same way
Lines 15-16: Timer setup... I want the level to be completed in 60 secods = 60,000 milliseconds. Again I strongly recommend to read the Flash simple timer/countdown tut.
Line 19: Determining elapsed time
Line 20: The text area time_left inside the movieclip count_down is updated according to the function time_to_string (lines 56-87). You can find this function fully explained... guess where?... Yes, at Flash simple timer/countdown tutorial!
You will notice nothing happens if the time reaches zero... I wanted it to fully test the game. You hould always test the game in a "god mode" to verify all levels are designed correctly, then start testing it in "normal mode".
The energy
Now, let's imagine we do not want the player to die if he hits the wall, but we want instead he to lose "energy" or "shields" or "bananas" until it reaches zero.
-
// attaching movies
-
_root.attachMovie("kira", "kira", 8000, {_x:234, _y:159});
-
_root.attachMovie("wall", "wall", 10000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("bg", "bg", 2000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("count_down", "count_down", 12000, {_x:0, _y:0});
-
// kira setup
-
yspeed = 0;
-
xspeed = 0;
-
wind = 0.00;
-
power = 0.65;
-
gravity = 0.1;
-
upconstant = 0.75;
-
friction = 0.99;
-
energy = 100;
-
count_down.energy_left.text = "Shield left: "+energy;
-
// timer setup
-
start_time = getTimer();
-
countdown = 60000;
-
kira.onEnterFrame = function() {
-
// timer
-
elapsed_time = getTimer()-start_time;
-
_root.count_down.time_left.text = time_to_string(_root.countdown-elapsed_time);
-
// keys
-
if (Key.isDown(Key.LEFT)) {
-
xspeed = xspeed-power;
-
}
-
if (Key.isDown(Key.RIGHT)) {
-
xspeed = xspeed+power;
-
}
-
if (Key.isDown(Key.UP)) {
-
yspeed = yspeed-power*upconstant;
-
}
-
if (Key.isDown(Key.DOWN)) {
-
yspeed = yspeed+power*upconstant;
-
}
-
// speed calculation
-
xspeed = (xspeed+wind)*friction;
-
yspeed = yspeed+gravity;
-
if (xspeed>0) {
-
this.kira.gotoAndStop(1);
-
} else {
-
this.kira.gotoAndStop(2);
-
}
-
// screen update
-
_root.wall._y -= yspeed;
-
_root.wall._x -= xspeed;
-
_root.bg._y -= yspeed;
-
_root.bg._x -= xspeed;
-
// collision
-
if (_root.wall.hitTest(this._x, this._y, true)) {
-
energy -= Math.round(Math.sqrt(xspeed*xspeed+yspeed*yspeed));
-
_root.count_down.energy_left.text = "Shield left: "+energy;
-
xspeed = 0
-
yspeed = 0
-
_root.wall._y = old_y;
-
_root.wall._x = old_x;
-
_root.bg._y = old_y;
-
_root.bg._x = old_x;
-
} else {
-
old_x = _root.wall._x+2*xspeed;
-
old_y = _root.wall._y+2*yspeed;
-
}
-
};
-
function time_to_string(time_to_convert) {
-
elapsed_hours = Math.floor(time_to_convert/3600000);
-
remaining = time_to_convert-(elapsed_hours*3600000);
-
elapsed_minutes = Math.floor(remaining/60000);
-
remaining = remaining-(elapsed_minutes*60000);
-
elapsed_seconds = Math.floor(remaining/1000);
-
remaining = remaining-(elapsed_seconds*1000);
-
elapsed_fs = Math.floor(remaining/10);
-
if (elapsed_hours<10) {
-
hours = "0"+elapsed_hours.toString();
-
} else {
-
hours = elapsed_hours.toString();
-
}
-
if (elapsed_minutes<10) {
-
minutes = "0"+elapsed_minutes.toString();
-
} else {
-
minutes = elapsed_minutes.toString();
-
}
-
if (elapsed_seconds<10) {
-
seconds = "0"+elapsed_seconds.toString();
-
} else {
-
seconds = elapsed_seconds.toString();
-
}
-
if (elapsed_fs<10) {
-
hundredths = "0"+elapsed_fs.toString();
-
} else {
-
hundredths = elapsed_fs.toString();
-
}
-
return "Time left: "+minutes+":"+seconds+":"+hundredths;
-
}
Line 14: Set the starting energy at 100
Line 15: Display player energy on screen
Line 51: In case of collision, the player does not die but his energy drains according to player speed. The higher the speed, the bigger the damage
Line 55-58: wall and background are resetted to their last position where no collision was detected
Lines 59-62: if there wasn't a collision, save actual position (with a little offset due by player speed)
These last lines remind a bit the collision checking seen in flash draw game tutorial part 4.
Now, next feature...
The brakes
I want the player to have the capability of using brakes, even if for a limited amount of time.
-
// attaching movies
-
_root.attachMovie("kira", "kira", 8000, {_x:234, _y:159});
-
_root.attachMovie("wall", "wall", 10000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("bg", "bg", 2000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("count_down", "count_down", 12000, {_x:0, _y:0});
-
// kira setup
-
yspeed = 0;
-
xspeed = 0;
-
wind = 0.00;
-
power = 0.65;
-
gravity = 0.1;
-
upconstant = 0.75;
-
friction = 0.99;
-
energy = 100;
-
brakes = 100;
-
count_down.energy_left.text = "Shield left: "+energy;
-
count_down.brake_left.text = "Brakes left: "+brakes;
-
// timer setup
-
start_time = getTimer();
-
countdown = 60000;
-
kira.onEnterFrame = function() {
-
// timer
-
elapsed_time = getTimer()-start_time;
-
_root.count_down.time_left.text = time_to_string(_root.countdown-elapsed_time);
-
// keys
-
if (Key.isDown(Key.LEFT)) {
-
xspeed = xspeed-power;
-
}
-
if (Key.isDown(Key.RIGHT)) {
-
xspeed = xspeed+power;
-
}
-
if (Key.isDown(Key.UP)) {
-
yspeed = yspeed-power*upconstant;
-
}
-
if (Key.isDown(Key.DOWN)) {
-
yspeed = yspeed+power*upconstant;
-
}
-
if ((Key.isDown(Key.SPACE))and(brakes> 0)) {
-
yspeed = yspeed/2;
-
xspeed = xspeed/2;
-
brakes --;
-
count_down.brake_left.text = "Brakes left: "+brakes;
-
}
-
// speed calculation
-
xspeed = (xspeed+wind)*friction;
-
yspeed = yspeed+gravity;
-
if (xspeed>0) {
-
this.kira.gotoAndStop(1);
-
} else {
-
this.kira.gotoAndStop(2);
-
}
-
// screen update
-
_root.wall._y -= yspeed;
-
_root.wall._x -= xspeed;
-
_root.bg._y -= yspeed;
-
_root.bg._x -= xspeed;
-
// collision
-
if (_root.wall.hitTest(this._x, this._y, true)) {
-
energy -= Math.round(Math.sqrt(xspeed*xspeed+yspeed*yspeed));
-
_root.count_down.energy_left.text = "Shield left: "+energy;
-
xspeed = 0
-
yspeed = 0
-
_root.wall._y = old_y;
-
_root.wall._x = old_x;
-
_root.bg._y = old_y;
-
_root.bg._x = old_x;
-
} else {
-
old_x = _root.wall._x+2*xspeed;
-
old_y = _root.wall._y+2*yspeed;
-
}
-
};
-
function time_to_string(time_to_convert) {
-
elapsed_hours = Math.floor(time_to_convert/3600000);
-
remaining = time_to_convert-(elapsed_hours*3600000);
-
elapsed_minutes = Math.floor(remaining/60000);
-
remaining = remaining-(elapsed_minutes*60000);
-
elapsed_seconds = Math.floor(remaining/1000);
-
remaining = remaining-(elapsed_seconds*1000);
-
elapsed_fs = Math.floor(remaining/10);
-
if (elapsed_hours<10) {
-
hours = "0"+elapsed_hours.toString();
-
} else {
-
hours = elapsed_hours.toString();
-
}
-
if (elapsed_minutes<10) {
-
minutes = "0"+elapsed_minutes.toString();
-
} else {
-
minutes = elapsed_minutes.toString();
-
}
-
if (elapsed_seconds<10) {
-
seconds = "0"+elapsed_seconds.toString();
-
} else {
-
seconds = elapsed_seconds.toString();
-
}
-
if (elapsed_fs<10) {
-
hundredths = "0"+elapsed_fs.toString();
-
} else {
-
hundredths = elapsed_fs.toString();
-
}
-
return "Time left: "+minutes+":"+seconds+":"+hundredths;
-
}
Line 15: Set the starting amount of brakes to 100
Line 17: Display player brakes on screen
Line 38: Checking if the player is braking (or pressing spacebar...) and the player has brakes left
Lines 39-42: Player speed is reduced by a half, brakes are reduced and new brakes value is displayed on the screen
Now we have all the information required on screen. Time to show them in a clearer way.
Text styles
Let's see how to give some style to our texts
-
// attaching movies
-
_root.attachMovie("kira", "kira", 8000, {_x:234, _y:159});
-
_root.attachMovie("wall", "wall", 10000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("bg", "bg", 2000, {_x:240, _y:0, _width:5700, _height:5700});
-
_root.attachMovie("count_down", "count_down", 12000, {_x:0, _y:1});
-
// kira setup
-
yspeed = 0;
-
xspeed = 0;
-
wind = 0.00;
-
power = 0.65;
-
gravity = 0.1;
-
upconstant = 0.75;
-
friction = 0.99;
-
energy = 100;
-
brakes = 100;
-
with (count_down.brake_left) {
-
text = "Brakes: "+brakes;
-
textColor = 0x00ff00;
-
background = true;
-
border = true;
-
borderColor = 0xffffff;
-
backgroundColor = 0x000000;
-
_alpha = 80;
-
}
-
with (count_down.energy_left) {
-
text = "Shield: "+energy;
-
textColor = 0x00ff00;
-
background = true;
-
border = true;
-