New tile based platform engine – AS3 version updated to step 10 PLUS scrolling
If you are looking for AS3 version of step 10, here it is… made by Robin vd Vleuten from the Netherlands with scrolling included!
Hello Emanuele,
I made it with flex so I use a swc to access the tiles. All the code is now in one actionscript-file, but I’m trying to convert the code in different classes. I will email it also when its finished if you’re interested in it.
Also many thanks for helping me out with the basics of a platform engine. It helped me a lot with a project I’m working on.
Regards,
Robin
P.S. Just another cool thing I had forgot to tell you. I made the game scrollable with just 2 extra lines of code :D
Here it is the source code…
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
[SWF(width='500', height='400', frameRate='40', backgroundColor='0xffffff')]
public class Platform extends Sprite
{
private var over:String = "";
private var bonus_speed:Number = 0;
private var press_left:Boolean = false;
private var press_right:Boolean = false;
private var press_up:Boolean = false;
private var press_down:Boolean = false;
private var press_space:Boolean = false;
private var x_pos:Number = 0;
private var y_pos:Number = 0;
private var tile_size:Number = 20;
private var ground_acceleration:Number = 1;
private var ground_friction:Number = 0.8;
private var air_acceleration:Number = 0.5;
private var air_friction:Number = 0.7;
private var ice_acceleration:Number = 0.15;
private var ice_friction:Number = 0.95;
private var treadmill_speed:Number = 2;
private var max_speed:Number = 3;
private var xspeed:Number = 0;
private var yspeed:Number = 0;
private var falling:Boolean = false;
private var gravity:Number = 0.5;
private var jump_speed:Number = 6;
private var climbing:Boolean = false;
private var climb_speed:Number = 0.8;
private var coins:Array = new Array();
private var level:Array = new Array();
private var levelObj:Array = new Array
private var keys:Array = new Array();
private var player:Array = new Array();
private var enemy:Array = new Array();
private var walkable_tiles:Array = new Array(0, 5, 6, 7);
private var h:hero = new hero();
private var level_container:Sprite = new Sprite();
private var bottom:Number;
private var left:Number;
private var right:Number;
private var top:Number;
private var bottom_left:Number;
private var bottom_right:Number;
private var top_left:Number;
private var top_right:Number;
private var climbdir:Number;
private var current_tile:Number;
private var friction:Number;
private var speed:Number;
private var jumping:Boolean;
private var walking:Boolean;
private var prev_bottom:Number;
public function Platform()
{
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, 1, 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, 1, 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, 1, 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, 9, 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 = [4,8];
coins[0] = [2, 2];
coins[1] = [23, 4];
enemy[0] = [10, 3, -2];
enemy[1] = [3, 3, -2];
keys[0] = [1, 5, 5, 8];
create_level(level);
addEventListener(Event.ENTER_FRAME,onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
}
public function key_down(event:KeyboardEvent):void
{
if (event.keyCode == 32)
{
press_space = true;
}
if (event.keyCode == 37)
{
press_left = true;
}
if (event.keyCode == 38)
{
press_up = true;
}
if (event.keyCode == 39)
{
press_right = true;
}
if (event.keyCode == 40)
{
press_down = true;
}
}
public function key_up(event:KeyboardEvent):void
{
if (event.keyCode == 32)
{
press_space = false;
}
if (event.keyCode == 37)
{
press_left = false;
}
if (event.keyCode == 38)
{
press_up = false;
}
if (event.keyCode == 39)
{
press_right = false;
}
if (event.keyCode == 40)
{
press_down = false;
}
}
public function create_level(l:Array):void
{
var level_height:Number = l.length;
var level_width:Number = l[0].length;
addChild(level_container);
for (var j:Number = 0; j<level_height; j++)
{
levelObj[j] = new Array();
for (var i:Number = 0; i<level_width; i++)
{
if (l[j][i] != 0)
{
var t:tile = new tile();
t.x = i*tile_size;
t.y = j*tile_size;
t.gotoAndStop(l[j][i]);
levelObj[j][i] = t;
level_container.addChild(levelObj[j][i]);
}
}
}
place_player();
level_container.addChild(h);
for (var m:int = 0; m < coins.length; m++)
{
var c:coin = new coin();
c.x = coins[m][0] * tile_size + tile_size / 2;
c.y = coins[m][1] * tile_size + tile_size/2 + 1;
coins[m] = c;
level_container.addChild(coins[m]);
}
for (var n:int = 0; n < keys.length; n++)
{
var _k:key = new key();
_k.x = keys[n][0] * tile_size + tile_size / 2;
_k.y = keys[n][1] * tile_size + tile_size / 2 + 1;
_k.openX = keys[n][2];
_k.openY = keys[n][3];
keys[n] = _k;
level_container.addChild(keys[n]);
}
for (var k:int = 0; k < enemy.length; k++)
{
var foe:patrol = new patrol();
foe.speed = enemy[k][2];
foe.x = enemy[k][0] * tile_size + tile_size / 2;
foe.y = enemy[k][1] * tile_size + tile_size / 2 + 1;
enemy[k] = foe;
level_container.addChild(enemy[k]);
}
}
public function onEnterFrame(event:Event):void
{
ground_under_feet();
walking = false;
climbing = false;
if (press_left) {
xspeed-=speed;
walking = true;
}
if (press_right) {
xspeed += speed;
walking = true;
}
if (press_up) {
get_edges();
if (top_right == 6 || bottom_right == 6 || top_left == 6 || bottom_left == 6) {
jumping = false;
falling = false;
climbing = true;
climbdir = -1;
}
}
if (press_down) {
get_edges();
if (over == "ladder" || top_right == 6 || bottom_right == 6 || top_left == 6 || bottom_left == 6) {
jumping = false;
falling = false;
climbing = true;
climbdir = 1;
}
}
if (press_space) {
get_edges();
if (!falling && !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 || jumping) {
yspeed += gravity;
}
if (climbing) {
yspeed = climb_speed*climbdir;
}
if (!falling && !jumping && !climbing) {
yspeed = 0;
}
xspeed += bonus_speed;
check_collisions();
h.x = x_pos;
h.y = y_pos;
level_container.x = -x_pos + 250;
level_container.y = -y_pos + 200;
xspeed -= bonus_speed;
for (var i:int = 0; i < enemy.length; i++)
{
enemy[i].x_pos = enemy[i].x;
enemy[i].y_pos = enemy[i].y;
enemy[i].x_pos += enemy[i].speed;
enemy[i].left_foot_x = Math.floor((enemy[i].x_pos-6)/tile_size);
enemy[i].right_foot_x = Math.floor((enemy[i].x_pos+5)/tile_size);
enemy[i].foot_y = Math.floor((enemy[i].y_pos+9)/tile_size);
enemy[i].bottom = Math.floor((enemy[i].y_pos+8)/tile_size);
enemy[i].left_foot = level[enemy[i].foot_y][enemy[i].left_foot_x];
enemy[i].right_foot = level[enemy[i].foot_y][enemy[i].right_foot_x];
enemy[i].left = level[enemy[i].bottom][enemy[i].left_foot_x];
enemy[i].right = level[enemy[i].bottom][enemy[i].right_foot_x];
if (enemy[i].left_foot != 0 && enemy[i].right_foot != 0 && enemy[i].left == 0 && enemy[i].right == 0)
{
enemy[i].x = enemy[i].x_pos;
}
else {
enemy[i].speed *= -1;
}
}
for (var j:int = 0; j < coins.length; j++)
{
if(coins[j].hitTestObject(h))
{
level_container.removeChild(coins[j]);
}
}
for (var k:int = 0; k < keys.length; k++)
{
if(keys[k].hitTestObject(h))
{
level[keys[k].openY][keys[k].openX] = 0;
level_container.removeChild(levelObj[keys[k].openY][keys[k].openX]);
level_container.removeChild(keys[k]);
}
}
}
public function ground_under_feet():void
{
bonus_speed = 0;
var left_foot_x:Number = Math.floor((x_pos-6)/tile_size);
var right_foot_x:Number = Math.floor((x_pos+5)/tile_size);
var foot_y:Number = Math.floor((y_pos+9)/tile_size);
var left_foot:Number = level[foot_y][left_foot_x];
var right_foot:Number = level[foot_y][right_foot_x];
if (left_foot != 0) {
current_tile = left_foot;
} else {
current_tile = right_foot;
}
switch (current_tile) {
case 0 :
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 && right_foot == 8)
{
place_player();
}
}
}
public function check_collisions():void
{
y_pos += yspeed;
get_edges();
if (yspeed>0) {
if ((bottom_right != 0 && bottom_right != 6) || (bottom_left != 0 && bottom_left != 6)) {
if (bottom_right != 5 && bottom_left != 5) {
if ((bottom_right == 7 || bottom_left == 7) && (Math.abs(yspeed)>1))
{
// trampoline
yspeed *= -1;
jumping = true;
falling = true;
}else {
y_pos = bottom*tile_size-9;
yspeed = 0;
falling = false;
jumping = false;
}
} else {
if (prev_bottom<bottom) {
y_pos = bottom*tile_size-9;
yspeed = 0;
falling = false;
jumping = false;
}
}
}
}
if (yspeed<0) {
if ((top_right != 0 && top_right != 5 && top_right != 6) || (top_left != 0 && top_left != 5 && top_left != 6)) {
y_pos = bottom*tile_size+1+8;
yspeed = 0;
falling = false;
jumping = false;
}
}
x_pos += xspeed;
get_edges();
if (xspeed < 0) {
if (!is_walkable(top_left) || !is_walkable(bottom_left)) {
x_pos = (left + 1) * tile_size + 6;
xspeed = 0;
}
}
if (xspeed>0) {
if (!is_walkable(top_right) || !is_walkable(bottom_right)) {
x_pos = right * tile_size - 6;
xspeed = 0;
}
}
prev_bottom = bottom;
}
public function get_edges():void
{
right = Math.floor((x_pos+5)/tile_size);
left = Math.floor((x_pos-6)/tile_size);
bottom = Math.floor((y_pos+8)/tile_size);
top = Math.floor((y_pos-9)/tile_size);
top_right = level[top][right];
top_left = level[top][left];
bottom_left = level[bottom][left];
bottom_right = level[bottom][right];
}
public function place_player():void
{
x_pos = (player[0] * tile_size) + (tile_size / 2);
y_pos = (player[1] * tile_size) + (tile_size / 2 + 1);
h.x = x_pos;
h.y = y_pos;
}
public function is_walkable(tile:int):Boolean {
var walkable:Boolean = false;
for (var i:int = 0; i < walkable_tiles.length; i++)
{
if (tile == walkable_tiles[i])
{
walkable = true;
break;
}
}
return (walkable);
}
}
} |
And this is the result:
Download the source code and make big kudos to Robin!!
They can be easily customized to meet the unique requirements of your project.















(23 votes, average: 4.87 out of 5)









This post has 34 comments
Robin
If someone knows how to fix the glitches you see when the container scrolls, please help!
Daniel Rodriguez
NOo! just yesterday I made my own AS3 engine, anyway I found interesting how you do it, and have a few questions, I made an enterFrame for each object in the game, one for the player, one for the coins, one for the patrol, one for the keys, and want to know if its better to have one for each object of one big! enterFrame??!!
Thanks for sharing your work!
Robin
I think its better to use 1 enterFrame function, because a lot of eventlisteners means less performance of your game.
Daniel Rodriguez
Ok, first I can’t download the file xD.
Second, yesterday I suffer the same bug that you have now, when you collect a coin or a key, you remove it(removeChild(…), that gives an Error, In my case the problem is that I need to remove the onEnterFrame, but how you have on big onEnterFrame the posible solution is to reduce the array of the coin and the key.
Emanuele Feronato
Daniel: I put a broken link, now it’s fixed
Sic Jake
Fantastic, thank you very much for doing these. I’ve been wanting to put together a platform game for ages, but could never find a good solid foundation to build upon in flash. Going thru these tuts has taught me alot. Thanks
Gabriel Bianconi
Great… Please do BoxHead tutorials (in AS3 XD)
labs.VETTIGHEID » Blog Archive » AS3 Platform Engine
[...] You can find my code and engine also on the blog of Emanuele. [...]
Massimo M.
Very good work Emanuele!
;)
What is the fps you use on this tutorial ?
flash 9 is sooo smothly animated!! :)
I have to learn it good to evolve from my flash 8 games !!!
Robin
It uses 40 fps, see the meta on line 6 ;)
Andy Cook
Seeing everything combined looks awesome! Are you going to add slopes eventually? That might be a cool feature and I can’t think of a way off the top of my head to do it
Josh of Cubicle Ninjas
The highlight of my day! Awesome work guys. Thanks Robin!
Pete
Awesome translation Robin. Coming from a strict Java programming env this is a better transition (Flash Newbie).
Could you consider adding (or if it’s easy just tell me) collision detection with patrols and multiple levels?
Thanks 5/5
miff
Great for AS3 users :(
What for us on AS2, part with scrolling?
Thanks!
Peter
I have to agree with Miff:
Great for AS3 users :(
What for us on AS2, part with scrolling?
Thanks!
Anton
whoaw great :D
Great for AS3 users :(
What for us on AS2, part with scrolling?
Thanks!
jim
what part of the code does the scrolling?? I’m confused.
[AS3] Tile help
[...] basing my code on this engine (http://www.emanueleferonato.com/2008…ling/#more-573) but Doing everything in 1 big’ol heap isn’t my style so i’m trying to break it [...]
AS3 Platform Engine « labs.VETTIGHEID
[...] gemaakt en de basis hiervan wil ik graag met mijn bloglezers delen. Ook heeft Emanuele zelf er een artikel aan gewijdt over mijn port van AS2 naar [...]
Kist
I agree with the guys above, please can somebody help us make the scrolling for AS2?
We would really apreciate that. :)
Thanks!
Platform Tile Engine in AS3 « labs.VETTIGHEID
[...] engine is gebasseerd op de tutorials van Emanuele Feronato in as2, maar zijn door mij voor hem naar as3 omgezet. En dit is het uiteindelijke [...]
Jml
Someone knows how to add you own sprite tiles?
Peter
I cant get the tiles removed again. If I wanna do a level change, can anyone help?
Peter
Jml, just add a frame in the tiles movieclip and make a new case. Eg:
Draw some fire in frame 10 of the tile movieclip.
case 10 :
over = “fire”;
if (left_foot == 8 && right_foot == 8)
{
place_player();
}
Peter
Oh and the part that makes it scrollable is:
# level_container.x = -x_pos + 250;
# level_container.y = -y_pos + 200;
Dylan Dalrymple
Hey Emanuele,
I was wondering if I could withhold the permission to use this platform engine from yourself and Robin vd Vleuten to create a platform engine and IDE called TileSense (you can see the project at http://www.moddb.com/Engines/TileSense). Contact me by my email address, dylandalrymple10@hotmail.com, with your response.
Sincerely,
Dyan Dalrymple
TileSense: AS3 tile based platform engine : Emanuele Feronato
[...] It’s a platform engine based on New tile based platform engine – AS3 version updated to step 10 PLUS scrolling [...]
labs.VETTIGHEID » Blog Archive » Platform Tile Game with Box2D: First Attempt!
[...] ago, I found Emanuele Feronato’s tutorials on tile-based platform games in AS2 and decided to convert them to AS3. But the engine was still not what I wanted, it was fancier if the player could push crates, walk [...]
Peter
If your holding up and left down, you can’t jump, anyone know how to fix that?
Peter
Found another bug, if your on a ladder and walks out of it while holding down arrow, you can float down with the speed of climbing the ladder.
Fehler beim Entfernen eines MovieClips - Flashforum
[...] [...]
Chandler
can some one plz give me the script/ tell me how to add multiple levels to this. specifcly when player gets all coins–> next scene or frame
thanks,
chandler
Upcoming Game Ideas | Toga Games
[...] Tiling Engine [...]
Kim
I would like to now that too – how to make more levels or new scenes. Thanks.