Creation of a platform game with Flash - step 3
Posted by Emanuele Feronato on 03/19/08 in Flash, Game design
To show you how hard I am going to try wiping the vaporware out of my life, I am publishing the 3rd part of the platform game tutorial.
Read parts 1 and 2 if you don't remember what I am talking about.
In this update, I introduced two new tile types: the lava tile that kills you when you walk over it (read: when you walk over it, not when you touch it), and the horizontally moving tile, that automatically (read: automatically) connects two spots.
The code needs to be cleaned, but everything seems to work
ACTIONSCRIPT:
-
// player default speeds
-
xspeed = 0;
-
yspeed = 0;
-
max_yspeed = 10;
-
walk_speed = 4;
-
climb_speed = 2;
-
// am I climbing?
-
climbing = false;
-
// am I jumping?
-
jumping = false;
-
// can I jump?
-
can_jump = true;
-
// gravity & jump settings
-
gravity = 1;
-
jump_power = 10;
-
walking_while_jumping = true;
-
// level creation
-
level = new Array();
-
_root.createEmptyMovieClip("lev", _root.getNextHighestDepth());
-
_root.createEmptyMovieClip("lad", _root.getNextHighestDepth());
-
_root.createEmptyMovieClip("lava", _root.getNextHighestDepth());
-
_root.createEmptyMovieClip("moving", _root.getNextHighestDepth());
-
level[0] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
-
level[1] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
-
level[2] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
-
level[3] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
-
level[4] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
-
level[5] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1);
-
level[6] = new Array(1, 0, 0, 0, 0, 0, 1, 1, 1, 4, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
-
level[7] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
-
level[8] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
-
level[9] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
-
level[10] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
-
level[11] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
-
level[12] = new Array(1, 0, 0, 0, 0, 0, 0, 1, 3, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
-
level[13] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
-
level[14] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1);
-
for (y=0; y<=14; y++) {
-
for (x=0; x<=24; x++) {
-
if (level[y][x] == 1) {
-
place_brick = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
-
place_brick.gotoAndStop(level[y][x]);
-
}
-
if (level[y][x] == 2) {
-
ladder_brick = lad.attachMovie("block", "block_"+lad.getNextHighestDepth(), lad.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
-
ladder_brick.gotoAndStop(level[y][x]);
-
}
-
if (level[y][x] == 3) {
-
lava_brick = lava.attachMovie("block", "block_"+lava.getNextHighestDepth(), lava.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
-
lava_brick.gotoAndStop(level[y][x]);
-
}
-
if (level[y][x] == 4) {
-
moving_brick = moving.attachMovie("block", "block_"+moving.getNextHighestDepth(), moving.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
-
moving_brick.gotoAndStop(level[y][x]);
-
moving_brick.dir = 1;
-
moving_brick.onEnterFrame = function() {
-
this._x += this.dir;
-
if ((_root.lev.hitTest(this._x+10, this._y, true) and (this.dir>0)) or ((_root.lev.hitTest(this._x-10, this._y, true) and (this.dir<0)))) {
-
this.dir *= -1;
-
}
-
if (_root.player.hitTest(this._x, this._y-11, true) or _root.player.hitTest(this._x+5, this._y-11, true) or _root.player.hitTest(this._x-5, this._y-11, true)) {
-
player._x += this.dir;
-
}
-
};
-
}
-
}
-
}
-
_root.attachMovie("player", "player", _root.getNextHighestDepth(), {_x:40, _y:40});
-
// end of level creation
-
player.onEnterFrame = function() {
-
if (Key.isDown(Key.LEFT)) {
-
if (climbing) {
-
xspeed = -climb_speed;
-
} else {
-
if (walking_while_jumping or can_jump) {
-
xspeed = -walk_speed;
-
}
-
}
-
}
-
if (Key.isDown(Key.RIGHT)) {
-
if (climbing) {
-
xspeed = climb_speed;
-
} else {
-
if (walking_while_jumping or can_jump) {
-
xspeed = walk_speed;
-
}
-
}
-
}
-
if (!feet_on_ladder()) {
-
climbing = false;
-
}
-
if (Key.isDown(Key.UP)) {
-
if (feet_on_ladder()) {
-
yspeed = -climb_speed;
-
climbing = true;
-
jumping = false;
-
}
-
}
-
if (Key.isDown(Key.DOWN)) {
-
if (feet_on_ladder() or ladder_under_my_feet()) {
-
yspeed = climb_speed;
-
climbing = true;
-
jumping = false;
-
}
-
}
-
if ((Key.isDown(Key.SPACE)) and can_jump and !jumping and !climbing) {
-
yspeed -= jump_power;
-
jumping = true;
-
}
-
// adjusting y speed
-
if (!climbing) {
-
yspeed += gravity;
-
}
-
if (yspeed>max_yspeed) {
-
yspeed = max_yspeed;
-
}
-
if (level_under_my_feet() and !jumping and !climbing) {
-
yspeed = 0;
-
}
-
if (ladder_under_my_feet() and !jumping and !climbing) {
-
yspeed = 0;
-
}
-
forecast_x = this._x+xspeed;
-
forecast_y = this._y+yspeed;
-
// lava control
-
if (_root.lava.hitTest(forecast_x, forecast_y+this._height/2-1, true)) {
-
forecast_x = 40;
-
forecast_y = 40;
-
jumping = false;
-
}
-
// floor control
-
while ((_root.lev.hitTest(forecast_x, forecast_y+this._height/2-1, true)) or (_root.moving.hitTest(forecast_x, forecast_y+this._height/2-1, true))) {
-
forecast_y--;
-
xspeed = 0;
-
yspeed = 0;
-
jumping = false;
-
}
-
// ceiling control
-
while (_root.lev.hitTest(forecast_x, forecast_y-this._height/2, true) or (_root.lava.hitTest(forecast_x, forecast_y-this._height/2, true)) or (_root.moving.hitTest(forecast_x, forecast_y-this._height/2, true))) {
-
forecast_y++;
-
yspeed = 0;
-
}
-
// left wall control
-
while (_root.lev.hitTest(forecast_x-this._width/2+1, forecast_y, true)) {
-
forecast_x++;
-
xspeed = 0;
-
}
-
// right wall control
-
while (_root.lev.hitTest(forecast_x+this._width/2, forecast_y, true)) {
-
forecast_x--;
-
xspeed = 0;
-
}
-
this._x = forecast_x;
-
this._y = forecast_y;
-
// adjusting speeds for next frame
-
xspeed = 0;
-
if (climbing) {
-
yspeed = 0;
-
}
-
};
-
function feet_on_ladder() {
-
return _root.lad.hitTest(player._x, player._y+player._height/2-1, true);
-
}
-
function level_under_my_feet() {
-
return _root.lev.hitTest(player._x, player._y+player._height/2, true);
-
}
-
function ladder_under_my_feet() {
-
return _root.lad.hitTest(player._x, player._y+player._height/2, true);
-
}
and, more important, I am learning how to fight vaporware plague.
Download the source code and give me the proof I can defeat vaporware.
Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
Tell me what do you think about this post. I'll write better and better entries.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

tag this
(4.6 out of 5)
RJ | Mar 19, 2008 | Reply
Very useful, thanks
Daniel | Mar 19, 2008 | Reply
Nice,
I hope you also create a side scrolling code, and clouds code, etc.
Thanks
David | Mar 19, 2008 | Reply
Nice… Could you make the Super mario jump…. Thing? That would be awesome :).
Grifo | Mar 19, 2008 | Reply
Let us work together against Vaporware!
Looks like I just came up with an enemy for our bombuzal. The Vaporbomber!!
styxtwo | Mar 19, 2008 | Reply
very sweet, and we all have to work agains vaporware ;)
EagleVision | Mar 19, 2008 | Reply
Thats a great Idea! :D
Nice emanuele.
Jack Hopkisn | Mar 19, 2008 | Reply
Aweseome!
The Vaporware has been…..Vaporized!!!!
Kesh | Mar 20, 2008 | Reply
little bug report: when u fall into the red, you are still jumpable. so while ur falling, u can jump thus defying gravity.
Michael | Mar 20, 2008 | Reply
You’re doing some good stuff here, Emanuele… keep it up :)
I feel like I want to make another platform game now!
Suely (Snakesue) | Mar 20, 2008 | Reply
Emanuele, you hit me and everybody. Wake up people. Thanks and a lot of kisses for you.
abhilash | Mar 20, 2008 | Reply
please would u do the next tut on line rider i’am still waiting for that from last year.
Nice tut ,ur first tutorial against vaporware :)
Turboll | Mar 20, 2008 | Reply
Many thanks for your great blog man, it is a great online game programming resource. I read most of the game design posts and you have inspired me picking up flash and actionscript.
Your blog is as good as any game programming books, and it is an helpful guide in any programming language as it covers many basic gaming concepts. It has really helped me understanding some basic game programming functions that whas left in the dark before. It is allso helpful seeing the result of the code instantly in your web-browser.
If I may ask: is it correct that your examples are written in actionscript 2? Can you recommend starting with As 2 or 3.0? (I’ve done some small games in C before).
Best Regards.
Eblup | Mar 21, 2008 | Reply
Hello this tutorial can lead to side scrolling that can be done by having everything in one moiveclip and have it move to keep the guy in the screen.
Amanuele | Mar 22, 2008 | Reply
It looks like a very nice tutorial but can you help me? like can you breifly explain step by step or should I just read from parts 1-3 again?
Eblup | Mar 24, 2008 | Reply
Hello, you should put in a area on youre sight for beginers and have some tutorials for actionscript 3.0.
P.S. i think this may help you’re website a lot.
;}
Eblup | Mar 24, 2008 | Reply
sorry for posting so much but if you have this
1,1,1,1,1,1 and if you run into the moving block
1,4,0,0,0,1 you glitch out.
1,1,1,1,1,1
Emanuele Feronato | Mar 24, 2008 | Reply
I know, the moving platform should kill the player but at the moment this does not happen.
The first AS3 tutorial should be out today…
Josh of Pixelton | Apr 1, 2008 | Reply
Hello Emanuele,
I simply had to say this is my favorite blog. I’m very bad at flash but your clear tutorials and awesome samples help show me the way. I feel that I might actually be able to learn this…
So thank you! :D
- Josh
Josh of Pixelton | Apr 2, 2008 | Reply
Oh, and I’d love to learn how to create a level editor. Arrays are my enemy. Thanks again.
Shaun | Apr 5, 2008 | Reply
Hey I have a question. why does it get all messed up when you raise the speed of the main character. It jumps up to the floor when you hit the wall at a higher speed
ricky | Apr 9, 2008 | Reply
heyy thnx for this TUT its great
but im wondering how to make a WIN spot?
when u arive in the win-block it loads a swf.
can anyone help me?
Joshua Thong | Apr 14, 2008 | Reply
great tutorial, but can you like make a tut for a non-tile platform game? I’m not really at home with xml and other stuff, only actionscript. like, can you publish a tut on how to make a platform game where the hero is a symbol and the map and other obstacles etc. is a symbol too? XD ty man
Mini Chris | Apr 17, 2008 | Reply
well so far tonypa’s is better but theres a few thing both of you dont have and thats freaking win spot to take you to next level i have tried and tried to do it but i cant.
Jake | Apr 25, 2008 | Reply
Need to add coins and an objective i dont know how Help need action script
Quintus | May 16, 2008 | Reply
Heho! :D
i have a question.
i am making a platform game in a soort op rpg style.
but how do i make enemy’s, that attack and walk to you if you come close to them?
Quintus | May 17, 2008 | Reply
just put this action on your WIN place (alpha 0)
onClipEvent(enterFrame) {
if(_root.player.hitTest(this)) {
//what ever you would happen if you win
}
}
or do you mean somthing totally different where i dont have a clue in what for script it is?
brart | Jun 10, 2008 | Reply
For a port to the next level you have to make a 3 dimensial array where the first number is the level.
When you finish the level, remove the movieclip lev, recreate it and than build the map with a higher number.
I will try to make a tut for emanuele about this genre in as3. Maybe it will contain those “next level teleports”.
Brart
fraser | Jul 12, 2008 | Reply
now all we need is a tutorial for a door then were pretty much done and thanks these tutorials are a real help for flash game making