Creation of a platform game with Flash – step 3
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
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 165 166 167 168 169 | // 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.
They can be easily customized to meet the unique requirements of your project.
28 Responses to “Creation of a platform game with Flash – step 3”
Leave a Reply
- 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
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- 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
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- 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 flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/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)

(12 votes, average: 4.67 out of 5)





Very useful, thanks
Nice,
I hope you also create a side scrolling code, and clouds code, etc.
Thanks
Nice… Could you make the Super mario jump…. Thing? That would be awesome :).
Let us work together against Vaporware!
Looks like I just came up with an enemy for our bombuzal. The Vaporbomber!!
very sweet, and we all have to work agains vaporware ;)
Thats a great Idea! :D
Nice emanuele.
Aweseome!
The Vaporware has been…..Vaporized!!!!
little bug report: when u fall into the red, you are still jumpable. so while ur falling, u can jump thus defying gravity.
You’re doing some good stuff here, Emanuele… keep it up :)
I feel like I want to make another platform game now!
Emanuele, you hit me and everybody. Wake up people. Thanks and a lot of kisses for you.
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 :)
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.
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.
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?
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.
;}
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
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…
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
Oh, and I’d love to learn how to create a level editor. Arrays are my enemy. Thanks again.
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
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?
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
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.
Need to add coins and an objective i dont know how Help need action script
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?
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?
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
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