Creation of a platform game with Flash - step 2
Posted by Emanuele Feronato on 11/3/07 in Flash, Game design
This is a quick update to the tutorial/engine started with Creation of a platform game with Flash - step 1.
I fixed some bugs and added a ladder object (the one defined in the array with "2" and painted in light purple).
Use arrows to move and space to jump.
I won't explain the script line by line at the moment because I want to add more features before writing a complete tutorial.
But the source code is someway commented
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());
-
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, 0, 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, 1, 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, 1, 1, 1, 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) {
-
place_brick = lad.attachMovie("block", "block_"+lad.getNextHighestDepth(), lad.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
-
place_brick.gotoAndStop(level[y][x]);
-
}
-
}
-
}
-
_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;
-
// floor control
-
while (_root.lev.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)) {
-
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);
-
}
This is the result you will get:
And this is the source code to download.
If you liked this post buy me a beer (or two) » 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
lewis | Nov 3, 2007 | Reply
cool man…i dont understand how to make side scrolling games please help!
Kevin | Nov 3, 2007 | Reply
Your tutorials are so awesome! thanks
I’m also hoping that you do a side scrolling tutorial.
Marcus Andersson | Nov 3, 2007 | Reply
Wow!
I gave up on making games in Flash a couple of years ago. Too bad this tutorial wasn’t around then (maybe it was and I didn’t see it?).
Great job!
LaRue | Nov 3, 2007 | Reply
Instead of moving the man on the x-axis, only let him move on the y-axis(to jump). And make the background move. So if you press the left arrow key, make the background move right.
chaew | Nov 3, 2007 | Reply
this is great - with the ladders and everything but do you think you could do a similar tutorial but for an art-based scroller? Im currently developing a platform game (actually with the intention of publishing it on mochiads ;p) cheers
RJ | Nov 3, 2007 | Reply
Simply fantastic
Tony | Nov 3, 2007 | Reply
How’s the monetizing experiment going?
I’m impatient to know it
Keep up with this great work!
Emanuele Feronato | Nov 3, 2007 | Reply
Marcus: you couldn’t see it a couple of years ago because I posted it today :)
But you can resume your skills and start programming again :)))
Tony: I’ll write about it on Sunday (Italy time GMT+1), a week after I released the game
About side scrolling: it’s an option I will inclue in future tutorials.
sam | Nov 4, 2007 | Reply
side scrolling would be easy. You can just make him move by 5 when you hit right AND make the whole scene move right. making boundaries and such too.i think new grounds has a tutorial thread full of lots of things about this. its called as: main i think. nice tuts Eman. Keep it up…
Alejandro | Nov 5, 2007 | Reply
Is there any reason about to fill the array in the explicit way?
Why don’t you use just
level = [[1, 1,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0, 0, 0, 0, 0, 0, 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, 1]]
In this way is easy to create a simple code to create levels and add a trace ouput to replace the level. I hope it helps with something
Great tutorials!
Sam | Nov 11, 2007 | Reply
Will there be a third part to this tutorial? Perhaps one showing how to change the character from a block to better image…
LonestarComics | Nov 13, 2007 | Reply
Is there a way to use XML data to load the level instead of arrays?
This is the best Platformer tutorial I’ve found for Flash, keep up the great work and I can’t wait for part three!
Emanuele Feronato | Nov 14, 2007 | Reply
Sure, I wrote a tutorial about it on this blog.
Search for “XML”
Teal | Nov 14, 2007 | Reply
This s great stuff. I’ve already started working on a new platform game using these tutorials, and it’s looking good.
Strid | Nov 16, 2007 | Reply
Really nice
would like to see a tutorial on angular collision and “loops” though
Michael | Nov 17, 2007 | Reply
Hey, I am having trouble getting the ladder to work, I have copied the tutorial exactly, I just made the stage bigger. The thing that is wrong is that when I go to climb the ladder it just doesn’t let me pass through it and is treating it like it is part of the ground rather than the ladder. Any ideas on what is wrong?
Randomguy | Dec 20, 2007 | Reply
I got a way to avoid jumping in mid-air if you walked off a platform. just make sure that when space is down the guy is touching the floor. otherwise, he can jump in midair because his jumping is not set to true. this also removes the need for the can_jump and jumping variables.
Pandz | Feb 16, 2008 | Reply
How do we add enemys? I’ve tried myself but its no t going that great… I’d really appreciate some help with this.
Zenn | Mar 4, 2008 | Reply
I would love to see this tutorial continued on to adding enemies, coins, getting you character to animate, and more!
kylian | Apr 2, 2008 | Reply
oh great tutorial but now i dnont now how to create a finnish plz help me
Dan | Apr 28, 2008 | Reply
this is using actionscript 3.0 right?
stevie | May 8, 2008 | Reply
no dan u mug
Gecko | Jun 14, 2008 | Reply
If you jump on the ladder, you will fall through it… Neat platform engine though!
fraser | Jun 29, 2008 | Reply
how would i make a door to go to the next area ive already made a block for it and added it to the level i just need the code to send me to the next level