Flash game creation tutorial – part 5.3
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | // 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | // 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | // 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | // 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;
borderColor = 0xffffff;
backgroundColor = 0x000000;
_alpha = 80;
}
with (count_down.time_left) {
textColor = 0x00ff00;
background = true;
border = true;
borderColor = 0xffffff;
backgroundColor = 0x000000;
_alpha = 80;
}
// timer setup
start_time = getTimer();
countdown = 60000;
kira.onEnterFrame = function() {
// timer
elapsed_time = getTimer()-start_time;
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--;
if (brakes<60) {
count_down.brake_left.textColor = 0xffff00;
}
if (brakes<30) {
count_down.brake_left.textColor = 0xff0000;
}
count_down.brake_left.text = "Brakes: "+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));
if (energy<60) {
count_down.energy_left.textColor = 0xffff00;
}
if (energy<30) {
count_down.energy_left.textColor = 0xff0000;
}
_root.count_down.energy_left.text = "Shield: "+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: "+minutes+":"+seconds+":"+hundredths;
} |
Lines 16-24: Note how I use the with statement to change brake text attributes.
According to Adobe Livedocs, the attributes you may be interested in are:
_alpha:Number – Sets or retrieves the alpha transparency value of the text field.
background:Boolean – Specifies if the text field has a background fill.
backgroundColor:Number – The color of the text field background.
border:Boolean – Specifies if the text field has a border.
borderColor:Number – The color of the text field border.
filters:Array – An indexed array containing each filter object currently associated with the text field. (you can see some filters in action in draw game creation tutorial part 1)
html:Boolean – A flag that indicates whether the text field contains an HTML representation.
htmlText:String – If the text field is an HTML text field, this property contains the HTML representation of the text field’s contents.
_rotation:Number – The rotation of the text field, in degrees, from its original orientation.
text:String – Indicates the current text in the text field.
textColor:Number – Indicates the color of the text in a text field.
_x:Number – An integer that sets the x coordinate of a text field relative to the local coordinates of the parent movie clip.
_y:Number – The y coordinate of a text field relative to the local coordinates of the parent movie clip.
Lines 25-33: Same thing with the enery text
Lines 34-41: Same thing with the timer text
Lines 66-71: If the brakes are damaged (<60) or critical (<30), I change brakes text color to yellow or red
Lines 90-95: Same thing with the energy text
Now we have a nice text representation… let’s cover another thing…
Advancing levels
What kind of game do we have if we cannot advance levels? Now I am teaching you how to add new caves.
In the movieclip where you have the first cavern, simply add a frame and draw another cavern. You can add how many caverns you want, just remember to put a stop() in each frame and to add, in the end of the cave, a movieclip instanced as end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | // attaching movies
_root.attachMovie("kira", "kira", 8000, {_x:234, _y:159});
_root.attachMovie("wall", "wall", 10000, {_width:5700, _height:5700});
_root.attachMovie("bg", "bg", 2000, {_width:5700, _height:5700});
_root.attachMovie("count_down", "count_down", 12000, {_x:0, _y:1});
// kira setup
start_x = new Array(240, -2160,-1400);
start_y = new Array(0, 2700,2200);
level = 1;
position_bg(level);
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;
borderColor = 0xffffff;
backgroundColor = 0x000000;
_alpha = 80;
}
with (count_down.time_left) {
textColor = 0x00ff00;
background = true;
border = true;
borderColor = 0xffffff;
backgroundColor = 0x000000;
_alpha = 80;
}
// timer setup
start_time = getTimer();
countdown = 60000;
kira.onEnterFrame = function() {
// timer
elapsed_time = getTimer()-start_time;
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.textColor = 0x00ff00;
if (brakes<60) {
count_down.brake_left.textColor = 0xffff00;
}
if (brakes<30) {
count_down.brake_left.textColor = 0xff0000;
}
count_down.brake_left.text = "Brakes: "+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)) {
if (!_root.wall.end.hitTest(this._x, this._y, true)) {
energy -= Math.round(Math.sqrt(xspeed*xspeed+yspeed*yspeed));
count_down.energy_left.textColor = 0x00ff00;
if (energy<60) {
count_down.energy_left.textColor = 0xffff00;
}
if (energy<30) {
count_down.energy_left.textColor = 0xff0000;
}
count_down.energy_left.text = "Shield: "+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 {
xspeed = 0;
yspeed = 0;
level++;
energy += 100;
brakes += 100;
start_time += 60000;
count_down.energy_left.textColor = 0x00ff00;
count_down.brake_left.text = "Shield: "+energy;
count_down.energy_left.text = "Shield: "+energy;
count_down.brake_left.text = "Brakes: "+brakes;
position_bg(level);
}
} 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: "+minutes+":"+seconds+":"+hundredths;
}
function position_bg(level) {
wall.gotoAndStop(level);
wall._x = start_x[level-1];
wall._y = start_y[level-1];
bg._x = start_x[level-1];
bg._y = start_y[level-1];
} |
Line 7: array with the 3 _x starting position of the level walls
Line 8: array with the 3 _y starting position of the level walls
Line 9: initializing starting level to 1
Line 10: calling the position_bg function. This function, located at lines 158-164, simply performs a gotoAndStop to the current level (frame) and places walls and backgrounds in their starting positions (declared in lines 7 and 8) according to the level
Line 94: When the player hits the wall, another test is performed to see if the player is hitting the end. If not, the game continues in the same way seen previously
Lines 110-122: If the player reached the exit, xspeed and yspeed are set to zero, level is increased, time, brakes and energy are increased by a bonus and the text on screen are updated. Finally, the position_bg function is called again.
Can you beat level 3?
Well, how you may see, I am designing a cavern racing game. Do you want to be part of it? You can! Soon you will find everything you need to design a level or more.
Meanwhile, download all source examples and have fun… until next tut…
They can be easily customized to meet the unique requirements of your project.
77 Responses to “Flash game creation tutorial – part 5.3”
Leave a Reply
Trackbacks
-
Flash game creation tutorial - part 1 at Emanuele Feronato on
March 14th, 2007 12:51 pm
[...] March 14th update: part 5.3 released. March 3rd update: part 5.2 released. February 9th update: part 5.1 released. December 31st update: 5th part released. December 23rd update: 4th part released. December 6th update: 3rd part released. November 18th update: 2nd part released. [...]
-
Flash game creation tutorial - part 3 at Emanuele Feronato on
March 14th, 2007 12:52 pm
[...] March 14th update: part 5.3 released. March 3rd update: part 5.2 released. February 9th update: part 5.1 released. December 31st update: 5th part released. December 23rd update: 4th part released. [...]
-
Flash game creation tutorial - part 4 at Emanuele Feronato on
March 14th, 2007 12:53 pm
[...] March 14th update: part 5.3 released. March 3rd update: part 5.2 released. February 9th update: part 5.1 released. December 31st update: 5th part released. [...]
-
Flash game creation tutorial - part 5.1 at Emanuele Feronato on
March 14th, 2007 12:53 pm
[...] March 14th update: part 5.3 released. March 3rd update: part 5.2 released. [...]
-
Flash game creation tutorial - part 5.2 at Emanuele Feronato on
March 14th, 2007 12:53 pm
[...] March 14th update: part 5.3 released. [...]
-
Flash game creation tutorial - part 5 at Emanuele Feronato on
March 14th, 2007 12:57 pm
[...] March 14th update: part 5.3 released. March 3rd update: part 5.2 released. February 9th update: part 5.1 released. [...]
-
Flash game creation tutorial - part 2 at Emanuele Feronato on
March 14th, 2007 12:58 pm
[...] March 14th update: part 5.3 released. March 3rd update: part 5.2 released. February 9th update: part 5.1 released. December 31st update: 5th part released. December 23rd update: 4th part released. December 6th update: 3rd part released. [...]
-
A Gem of Flash Game Tutorials | Newbie Game Programmers on
December 17th, 2007 4:23 pm
[...] Flash game creation tutorial – Part 1 :: Part 2 :: Part 3 :: Part 4 :: Part 5 :: Part 5.1 :: Part 5.2 :: Part 5.3 [...]
- Citrus Engine released for free for learning
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Triqui MochiAds Arcade plugin for WordPress official page
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.74/5)
- Create a flash artillery game – step 2 (4.74/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 1 (4.71/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(60 votes, average: 4.58 out of 5)



nice tut can’t wait for the next!
i want to make a flash game very similar to this one:
http://www.adultswim.com/games/inuyasha_demon/index.html?showId=324233&name=Inuyasha&timezone=EST
as it is somewhat different from the tutorial i was wondering if you could give me some tips. or maybe a jump start. or maybe a simple tutorial. o.o
may you should stop using the method of assigning actions to frame becoze the earlier method was a way easier than this.
Ace, Carn’t Wait for the next Step onto making an ultimate game, ! qucik question when the shield get to 0 it dose not re-set
how could you fix this??
It mai saund stupid but i can`t understend wat prigram siould we youse to make this kined of game. maibe you can tell me with is beater to download
The program is Macromedia Flash!
yes it is macromedia flash and sorry to say but you cannot download flash pro 8 which you need. you need to buy the whole thing for
$300-$600 with a key so sorry.
i’ve tested my game in hero mode. I just need to know how to go to a new fram when timer reaches 0. Please!
@sdobson: idont know relly know but if the timmer is a textbox take this code
if (_root.texboxname
eee on my computer the code didnt come up so i post it agian
if(_root.textboxname
yo i got some questions for ya email me plz
hi can you let theis people think how to make things for themself i made my own jumping code and its beater than any youve ever seen
Very nice… is there a way you can provide the connection parameters if we want to get the position of movie clips from a php page? maybe with xml? or nay other way?
lets say we have a chess setup already in a database, with columns, rows, and piece types…
Is there anyway in which to make the character so that when it hits into the wall it bounces off but from the edge and not the center of the character?
to Joseph hey i can help you make a game but ill only help you make 1 enemy and 1 guy and no extra card evrey level so take the offer?
Great tutorial, but at the part where you set the alpha of the
text (time left, energy, brake) I just couldn’t get it to work no matter what I tried. But I did get it to work by setting the blend mode of my three movieclips to layer… Can anyone explain why this works?
I can’t get the code for changing the frame when the timer reaches zero to work. If I use the code
if (_root.textboxname = 0){
gotoAndStop(“frame”)
}
it just sets the timer to zero. Why is this happening? (note the code is nestled in the same onClipEvent(enterFrame) handler as everything else, if this makes a difference)
Also, what is the code for scaling the size of the level?
Very nice tutorial, I think everyone here appreicates your easy to understand and fun tutorials. Keep it going man!!!
Hi Emanuele Feronato. Uhm, I have a problem with my game… You see, I’m making a game (in flash MX, it uses flash player 6.0) and I want it to look like a Crono Trigger… well my problem is that I want to include a lot of movie clips in the background and I’m using 2 kind of movement (in the center of the screen, the character changes its position x, y and when it hits the edge of the screen, then the background moves)… When I do so, the character and the bachground moves too slowly. I tried to change the vector images to GIF and PNG images but it’s just the same… I’d really appreciate if you answer to my mail dude. By the way, I found your tutorial very useful. Thanks for transmiting your knowledge about flash, I’m sure this will helkp many people. Well, see you.
How would you make a game like security 2?
Did not know about this game… but it rocks!
Expect a tutorial very soon.
yey!
Can somebody please tell me how to make a couter counting up(a stopwatch)
i guess its quite easy to do but im only a beginner so i dont have
a clue.
please reply to leoleo999@hotmail.co.uk
Just wanted to say hi !
Downloaded all tutorials, will enjoy.
flanture
[flash adventure]
to jordanleffl:
You can actually download Macromedia Flash Pro 8, you just have to download first “Ares”, then, with this one, download the program, and finally, the serial number…
Hey, how can I re-set the game once the shield gets to “0″???
Can’t see the swf file, its black.
how do u test the game u made.
Hey, I have 2 big question and you surely know the answers.
*What is the actionscript I should put if I want to go to another frame or scene when all the enemies are dead?????
and
*I know how to “kill” enemies when you fire at them, but how do I do to kill them only after 2 shots??????
I hope you answer me
You can simply add energy to the enemies and “kill” them when it is over.
Very easy way to do that is to add some variables:
_root.onLoad = function() {
enemyCount = 50; // if you have 50 enemies
}
and then for each killed enemy do enemyCount–; and then check for the number of enemies and if it is zero jump to frame 10 for example ;)
then for each enemy add his nrg and subtract 1 for each hitTest(), and when enemy energy reaches 0 “kill” him.
if (evilNRG == 0) {
this.removeMovieClip();
enemyCount–;
}
Im going to make a game quite similiar to this with more pixel graphics, and a gender selection and all I just need to be more advanced in actionscript im a noob at this.
Ive tried following these steps and Im not as good as I believed I was. With pretty much the first step everytime I test movie (ctrl enter) and I get 2 errors. But it does work but very very jumpy. 2nd im having trouble getting the yellow circles the collecting coins to work. Please help guys email me happytodd@ozemail.com.au
Great tutorial series!
Is there a Pool Table (Billiard) tutorial on its way? (or does one already exist?)
ok i got to 5.2 and it just stopped working and i dont see any of the games here either, copy and pasting the code(taking out the line numbers and fixing the formatting) doesnt work either, it doesnt create anything on the stage…
huh..
your tutorial is really good!
i am trying to code something myself.
its a lil different then your game i have a problem. here is the code:
if (_root.map.rightwall.hitTest(_x, _y, true)) {
_root.map._x = power;
}
\—\
if (_root.map.wall1.hitTest(_x, _y, true)) {
_root.map._x = power;
_root.map._y = power;
}
well it only test 1 named mc that is inside another mc (map mc).
any solution or ideas how to make it work?
I really dig what you’re doing, the only thing is how do I make it so that the line that is drawn is at a lower depth than the other layers? I dont like how you can write over “Draw a line here” box.
Any help is appreciated!
1billioN!
hi.. im using flash MX.. may i know wat version u r using? coz i cant open d file tat downloaded from this website…
or any1 whoever downloaded this project and save it as MX file? pls help me…
send to chupy_fish@hotmail.com (msn also can)
my screens are black 2 it says movie not loaded when i right click it, i’ve renewed the page but nothing.
yo peter. I’m having the same problem. Probably the flash animations got off of the server.
I am just getting into flash game programming from 3d game programming and this is one of the best tutorial i have seen yet.. It would be better to update it to action script 3 though..
To understand this we should be able to understand Math fucntions and i gave up
YES! Emanuele, I have a small favor to ask of you. Would you plese create a FROGGER tutorial. litertaly no one has created one!
Thank you for the tutorials!
I know it wouldn’t be for a while (Even if you did jump to it now, I’d probably be totally lost) but it would be cool if you’d show how to make… like, for example, a first person shooter. Just a thought.
I made it! I got infinite time, shield, and brakes! Woo-hoo! :)
same here
I’ll stick to java
How do you make it so when the sheild gets to zero, you die…well go to another frame.
or when the timer ends…
Flash tuts seem to be black…can’t see them???
Haha, yes, i finally beat level three. I’m also making a sidescroller, kind of like Aegis wing*, and i was wondering how to make weapons and stuff, but TY for all the help, it’s AWESOME!.
*Aegis Wing, is an X-Box live Arcade game, for all of you who don’t know.
Okay, i seriously loved this Belissima tutorial, however i was wondering something. How on earth do you change the controls from being just the arrow keys, to the WASD format we see so often? I’v made a two-player game, but can’t get any other keys to work so well, one configuration is uncomfortably close to player 1, and the other one is all vertical button alignment. Thanks for the help though, much appreciated. ROCK ON FERONATO!!!
Hey -Black cat- you make want to use:
if (_root.textboxname == 0){
gotoAndStop(â€frameâ€)
}
———————————-
You were seting the textboxname value to zero.
“==” test for equality
“=” sets the value
hey how can i send you my game.
cant see the game at all?
hi,
on the scrolling level
how do you make the end?
because when the player hits the end it just goes back to the start.
can you help me?
flash movies do not load…
Are these flash clips compatible with flash player 10? They don’t load
Thanks for the tutorial. It was awesome to say the least. Learnt lots of stuff from it. You can make it a little more lucid for the absolute beginners by specifying where exactly the script has to be entered at various places.
I created a running sprite animation from a sprite sheet, and copied the code from the 1st page of the hero to it, so i could move it. But it would be nice if you could help by creating a code for ground that he could walk on instead of being sent to the middle of the screen when the ground is touched.
thanks!
coolll!!!!!!!!!!!
Hey, uhhm… I got to level three. Finished it too.
Mainly because it’s still on god mode on my browser, and now when I hit the exit, my shield + brakes go up. And time, but that was screwed anyway.
hey. If you could email me, i’d reaallyy love to know how to pause the game when you hit space and unpause it when u hit space again or another button. Can it be done with a simple action script ok keira?
Hey, love the tutorials. They got me into this language, thanks!
Also, you guys can check out my site at http://www.rslegion.net
Can i have some comments on the quality of my tutorials please?
nice. But
1) What program do i need to do this
2) Where do i get it?
I really want to make this game.
Excellent Tutorial!! Thanks a Bunch!
Thinking of coding in AS3 the next game :)?
I Found a Glitch on Level 2 Where there is a Part of the wall you can get stuck on!!!!
Great tutorials, however all the examples from the timer onwards are coming up as a black box on my screen (Tried firefox and chrome) maybe they’re not compatible with the latest flash player. Just a heads up.
instead of using two frames i use _xscale = -100