New tile based platform engine – part 9 – coins n’ spikes
You asked for coins and spikes, and here they are...
As usual I had to define some rules... in this case spikes are deadly only when the player has both feet on it... later I will make a lava (or electric) tile that will kill the player even if he only has one foot on it.
At the moment the player just restarts when he dies, later I will manage real deaths.
Coins are... well... coins... just collect them.
Fix for the fix for infamous Ladder Bug in the Platform Tutorials
In New tile based platform engine - more theory I said superdean fixed the ladder bug... that's not completely true because with his code the player cannot climb down a ladder when he's walking over it.
So the correct if statement to perform when the player presses DOWN is neither mine
if (over == "ladder") {
nor superdean's one
if (top_right == 6 or bottom_right == 6 or top_left == 6 or bottom_left == 6){
but
if ((over == "ladder") or (top_right == 6 or bottom_right == 6 or top_left == 6 or bottom_left == 6)) {
Now grab the source code:
-
tile_size = 20;
-
ground_acceleration = 1;
-
ground_friction = 0.8;
-
air_acceleration = 0.5;
-
air_friction = 0.7;
-
ice_acceleration = 0.15;
-
ice_friction = 0.95;
-
treadmill_speed = 2;
-
max_speed = 3;
-
xspeed = 0;
-
yspeed = 0;
-
falling = false;
-
gravity = 0.5;
-
jump_speed = 6;
-
climbing = false;
-
climb_speed = 0.8;
-
level = new Array();
-
enemy = new Array();
-
coin = new Array();
-
level[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];
-
level[1] = [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] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 1, 0, 0, 0, 0, 1];
-
level[3] = [1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
-
level[4] = [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
-
level[5] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
-
level[6] = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 1, 0, 0, 0, 0, 0, 1];
-
level[7] = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1];
-
level[8] = [1, 1, 1, 1, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 7, 0, 0, 6, 0, 0, 0, 0, 7, 0, 1];
-
level[9] = [1, 1, 1, 1, 1, 1, 1, 2, 2, 8, 8, 8, 8, 1, 1, 3, 3, 1, 4, 4, 1, 8, 1, 8, 1];
-
player = [5, 8];
-
enemy[0] = [10, 3, -2];
-
enemy[1] = [3, 3, -2];
-
coin[0] = [2, 2];
-
coin[1] = [23, 4];
-
coin;
-
function create_level(l) {
-
_root.createEmptyMovieClip("level_container", 1);
-
level_height = l.length;
-
level_width = l[0].length;
-
for (y=0; y<level_height; y++) {
-
for (x=0; x<level_width; x++) {
-
if (l[y][x] != 0) {
-
t = level_container.attachMovie("tile", "t"+y+"_"+x, _root.level_container.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
-
t.gotoAndStop(l[y][x]);
-
}
-
}
-
}
-
place_player();
-
for (x=0; x<coin.length; x++) {
-
coin_mc = level_container.attachMovie("coin", "coin_"+_root.level_container.getNextHighestDepth(), _root.level_container.getNextHighestDepth(), {_x:coin[x][0]*tile_size+tile_size/2, _y:coin[x][1]*tile_size+tile_size/2+1});
-
coin_mc.onEnterFrame = function() {
-
if (this.hitTest(level_container.hero._x, level_container.hero._y, true)) {
-
this.removeMovieClip();
-
}
-
};
-
}
-
for (x=0; x<enemy.length; x++) {
-
foe = level_container.attachMovie("patrol", "patrol_"+_root.level_container.getNextHighestDepth(), _root.level_container.getNextHighestDepth(), {_x:enemy[x][0]*tile_size+tile_size/2, _y:enemy[x][1]*tile_size+tile_size/2+1});
-
foe.speed = enemy[x][2];
-
foe.onEnterFrame = function() {
-
this.x_pos = this._x;
-
this.y_pos = this._y;
-
this.x_pos += this.speed;
-
this.left_foot_x = Math.floor((this.x_pos-6)/tile_size);
-
this.right_foot_x = Math.floor((this.x_pos+5)/tile_size);
-
this.foot_y = Math.floor((this.y_pos+9)/tile_size);
-
this.bottom = Math.floor((this.y_pos+8)/tile_size);
-
this.left_foot = level[this.foot_y][this.left_foot_x];
-
this.right_foot = level[this.foot_y][this.right_foot_x];
-
this.left = level[this.bottom][this.left_foot_x];
-
this.right = level[this.bottom][this.right_foot_x];
-
if (this.left_foot != 0 and this.right_foot != 0 and this.left == 0 and this.right == 0) {
-
this._x = this.x_pos;
-
} else {
-
this.speed *= -1;
-
}
-
};
-
}
-
}
-
create_level(level);
-
_root.onEnterFrame = function() {
-
ground_under_feet();
-
walking = false;
-
climbing = false;
-
if (Key.isDown(Key.LEFT)) {
-
xspeed -= speed;
-
walking = true;
-
}
-
if (Key.isDown(Key.RIGHT)) {
-
xspeed += speed;
-
walking = true;
-
}
-
if (Key.isDown(Key.UP)) {
-
get_edges();
-
if (top_right == 6 or bottom_right == 6 or top_left == 6 or bottom_left == 6) {
-
jumping = false;
-
falling = false;
-
climbing = true;
-
climbdir = -1;
-
}
-
}
-
if (Key.isDown(Key.DOWN)) {
-
get_edges();
-
if ((over == "ladder") or (top_right == 6 or bottom_right == 6 or top_left == 6 or bottom_left == 6)) {
-
jumping = false;
-
falling = false;
-
climbing = true;
-
climbdir = 1;
-
}
-
}
-
if (Key.isDown(Key.SPACE)) {
-
get_edges();
-
if (!falling and !jumping) {
-
jumping = true;
-
yspeed = -jump_speed;
-
}
-
}
-
if (!walking) {
-
xspeed *= friction;
-
if (Math.abs(xspeed)<0.5) {
-
xspeed = 0;
-
}
-
}
-
if (xspeed>max_speed) {
-
xspeed = max_speed;
-
}
-
if (xspeed<max_speed*-1) {
-
xspeed = max_speed*-1;
-
}
-
if (falling or jumping) {
-
yspeed += gravity;
-
}
-
if (climbing) {
-
yspeed = climb_speed*climbdir;
-
}
-
if (!falling and !jumping and !climbing) {
-
yspeed = 0;
-
}
-
xspeed += bonus_speed;
-
check_collisions();
-
level_container.hero._x = x_pos;
-
level_container.hero._y = y_pos;
-
xspeed -= bonus_speed;
-
};
-
function ground_under_feet() {
-
bonus_speed = 0;
-
left_foot_x = Math.floor((x_pos-6)/tile_size);
-
right_foot_x = Math.floor((x_pos+5)/tile_size);
-
foot_y = Math.floor((y_pos+9)/tile_size);
-
left_foot = level[foot_y][left_foot_x];
-
right_foot = level[foot_y][right_foot_x];
-
if (left_foot != 0) {
-
current_tile = left_foot;
-
} else {
-
current_tile = right_foot;
-
}
-
switch (current_tile) {
-
case 0 :
-
over = "air";
-
speed = air_acceleration;
-
friction = air_friction;
-
falling = true;
-
break;
-
case 1 :
-
over = "ground";
-
speed = ground_acceleration;
-
friction = ground_friction;
-
break;
-
case 2 :
-
over = "ice";
-
speed = ice_acceleration;
-
friction = ice_friction;
-
break;
-
case 3 :
-
over = "treadmill";
-
speed = ground_acceleration;
-
friction = ground_friction;
-
bonus_speed = -treadmill_speed;
-
break;
-
case 4 :
-
over = "treadmill";
-
speed = ground_acceleration;
-
friction = ground_friction;
-
bonus_speed = treadmill_speed;
-
break;
-
case 5 :
-
over = "cloud";
-
speed = ground_acceleration;
-
friction = ground_friction;
-
break;
-
case 6 :
-
over = "ladder";
-
speed = ground_acceleration;
-
friction = ground_friction;
-
break;
-
case 7 :
-
over = "trampoline";
-
speed = ground_acceleration;
-
friction = ground_friction;
-
break;
-
case 8 :
-
over = "spikes";
-
if (left_foot == 8 and right_foot == 8) {
-
place_player();
-
}
-
}
-
}
-
function check_collisions() {
-
y_pos += yspeed;
-
get_edges();
-
// collision to the bottom
-
if (yspeed>0) {
-
if ((bottom_right != 0 and bottom_right != 6) or (bottom_left != 0 and bottom_left != 6)) {
-
// not a cloud...
-
if (bottom_right != 5 and bottom_left != 5) {
-
// a trampoline
-
if ((bottom_right == 7 or bottom_left == 7) and (Math.abs(yspeed)>1)) {
-
yspeed = yspeed*-1;
-
jumping = true;
-
falling = true;
-
} else {
-
y_pos = bottom*tile_size-9;
-
yspeed = 0;
-
falling = false;
-
jumping = false;
-
}
-
} else {
-
//cloud
-
if (prev_bottom<bottom) {
-
y_pos = bottom*tile_size-9;
-
yspeed = 0;
-
falling = false;
-
jumping = false;
-
}
-
}
-
}
-
}
-
// collision to the top
-
if (yspeed<0) {
-
if ((top_right != 0 and top_right != 5 and top_right != 6) or (top_left != 0 and top_left != 5 and top_left != 6)) {
-
y_pos = bottom*tile_size+1+8;
-
yspeed = 0;
-
falling = false;
-
jumping = false;
-
}
-
}
-
x_pos += xspeed;
-
get_edges();
-
// collision to the left
-
if (xspeed<0) {
-
if ((top_left != 0 and top_left != 5 and top_left != 6 and top_left != 7) or (bottom_left != 0 and bottom_left != 5 and bottom_left != 6 and bottom_left != 7)) {
-
x_pos = (left+1)*tile_size+6;
-
xspeed = 0;
-
}
-
}
-
// collision to the right
-
if (xspeed>0) {
-
if ((top_right != 0 and top_right != 5 and top_right != 6 and top_right != 7) or (bottom_right != 0 and bottom_right != 5 and bottom_right != 6 and bottom_right != 7)) {
-
x_pos = right*tile_size-6;
-
xspeed = 0;
-
}
-
}
-
prev_bottom = bottom;
-
}
-
function get_edges() {
-
// right edge
-
right = Math.floor((x_pos+5)/tile_size);
-
// left edge
-
left = Math.floor((x_pos-6)/tile_size);
-
// bottom edge
-
bottom = Math.floor((y_pos+8)/tile_size);
-
// top edge
-
top = Math.floor((y_pos-9)/tile_size);
-
// adjacent tiles
-
top_right = level[top][right];
-
top_left = level[top][left];
-
bottom_left = level[bottom][left];
-
bottom_right = level[bottom][right];
-
}
-
function place_player() {
-
level_container.hero.removeMovieClip();
-
x_pos = player[0]*tile_size+tile_size/2;
-
y_pos = player[1]*tile_size+tile_size/2+1;
-
level_container.attachMovie("hero", "hero", _root.level_container.getNextHighestDepth(), {_x:x_pos, _y:y_pos});
-
}
and play the game.
Can you grab the rightmost coin? (the other is too easy because patrols can't kill the player)
Download the source code and enjoy.
Nest step... code optimization.
They can be easily customized to meet the unique requirements of your project.
22 Responses to “New tile based platform engine – part 9 – coins n’ spikes”
Leave a Reply
Trackbacks
-
Basic level editor for a tile based game : Emanuele Feronato on
October 8th, 2008 12:43 pm
[...] am sharing with you a basic level editor made for the platform engine but easily adaptable to any tile based game made by Daniel Felipe Rodriguez Your recent set of [...]
-
New tile based platform engine - part 10 - optimization, doors n’ keys : Emanuele Feronato on
October 13th, 2008 3:48 pm
[...] how does the script at step 9 determine if a tile is walkable (player can pass through [...]
Posts
- Rick Triqui: my first PlayCrafter game
- Prototype of a Flash game like Meeblings
- Games for the game developers!
- The art of debugging
- How to embed a text file in Flash
- Create a Flash game in minutes with PlayCrafter
- Upgrade your Flash CS4 to 10.0.2
- Play Mazeroll, my latest Box2D game
- Triqui MochiAds Arcade plugin for WordPress Released!!
- The MochiAds funnel
- Flash game creation tutorial - part 1
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Create a flash draw game like Line Rider or others - part 1
- Create a Flash Racing Game Tutorial
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash artillery game - step 1
- Create a flash draw game like Line Rider or others - part 5
- Flash game creation tutorial – part 5.2




(4.9 out of 5) - Flash game creation tutorial – part 3




(4.86 out of 5) - Creation of a platform game with Flash – step 2




(4.84 out of 5) - Create a survival horror game in Flash tutorial – part 1




(4.82 out of 5) - Create a flash artillery game – step 1




(4.82 out of 5) - Create a Flash Racing Game Tutorial




(4.8 out of 5) - Create a flash artillery game – step 2




(4.75 out of 5) - New tile based platform engine – part 6 – ladders




(4.74 out of 5) - Flash game creation tutorial – part 2




(4.73 out of 5) - The experiment – one year later




(4.7 out of 5)


Awesome. I allredy made spikes but these are good too. And after all stuff, can you make level editor/loader? Thanks!
This is great and all but what’s happening with the WordPress arcade plugin?
Nice tut… But where’s the WP Arcade?
And BoxHead Tutorial?
And AS3 Versions?
Ok… I know I’m asking too much…
Yeah, I think the Boxhead tutorial became vaporware. I was waiting on that one too.
How can I contact you, I dont think me email is going through ?
Tower defense tuts lol pls that would make my month
and gh4 is gonna be awesome but tower defense tuts good i guess
Very nice.
I might actually use this! =)
Thank you!
Boxhead tutorial became vaporware I suppose.
But anyhow, keep it up, you’ve improved with the blog. =) …And me = Bad
Code optimization! Cool! I always want to make my code as efficient as possible.
BTW, I hope the Boxhead tutorial will not become vaporware…
Not boxhead tut. Nano wars :)
aww…so i was WRONG???
i knew i forgot something…
Its okay superdean, who needs to climb down a ladder when they can jump down right. :)
christo: info[at]emanueleferonato.com
Awesome stuff here Emanuele, I still check daily even if I don’t post a response. I’ve also got a good game idea that uses a tile engine I made before. It should be good, it also hlps that I love Pixel art too. I’d love to get as much money as Nitrome per game, they must get 5k per game atleast, they’re fantastic !
Ok I try again, allready sent you 3 or 4 emails, perhaps they are not going through, I really dont want too spam you, I’ll try again now.
Thanks
BTW awesome tutorials on your site :)
Great work as always.
Excellent engine, definitely going to look into developing it further.
Also, optimisation? What is this ‘optimisation’ you speak of?
/Recently got shouted at for poor optimisation
http://frozenhaddock.co.uk/2008/10/avoiding-game-pt-5b/
One interesting tile will be slopes, really dont have any idea to do it.
Just found your blog through google, as I am interested in getting some actionscript experience and want to do so while making a side scrolling action adventure. So, there are two particular things that have been on my mind as a completely inexperienced programmer:
-How to implement doors? So that you can switch from the one levelsheet to the other? I have seen some tutorials with doors implemented, but the problem with those were that they relied on going to a different frame. What I am searching for is to replace the level while staying in the same frame; cleaning up one level and loading the other.
-How to implement a scrolling feature, where the player walks to the middle of the screen and as long as the level is long enough the level starts scrolling at that point?
Sorry for the questions already…
Ahm may i ask something sir? this question might have been ask by others on the comment but,
is there a way where the players jump height can be determine on how long the player presses the jump key?
i mean just like how super mario does but of course theres a limit to how high he jumps. i already got the idea but dont really know how to apply it, im thinking of a loop where it checks if the max height of a jump is reached.
hi,
i have a question. Can you write me an e-mail? I wanna send you a try of my platform game…. but I still have two problems.