New tile based platform engine – AS3 version
First, I would like to say the engine is not finished as I have a lot of tile types to add.
This is the AS3 version of part 6.
I am fixing some glitches and preparing another post about the theory of platform games
This is the 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 | package {
import flash.display.Sprite;
import flash.ui.Mouse;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class newplat06_as3 extends Sprite {
var over = "";
var bonus_speed = 0;
var press_left = false;
var press_right = false;
var press_up = false;
var press_down = false;
var press_space = false;
var x_pos = 0;
var y_pos = 0;
var tile_size = 20;
var ground_acceleration = 1;
var ground_friction = 0.8;
var air_acceleration = 0.5;
var air_friction = 0.7;
var ice_acceleration = 0.15;
var ice_friction = 0.95;
var treadmill_speed = 2;
var max_speed = 3;
var xspeed = 0;
var yspeed = 0;
var falling = false;
var gravity = 0.5;
var jump_speed = 6;
var climbing = false;
var climb_speed = 0.8;
var level = new Array();
var player = new Array(5,1);
var h:hero = new hero();
public function newplat06_as3() {
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
level[4] = [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[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, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1];
level[9] = [1, 1, 1, 1, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 1, 1];
create_level(level);
addEventListener(Event.ENTER_FRAME,on_enter_frame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
}
public function key_down(event:KeyboardEvent) {
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) {
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) {
var level_container:Sprite = new Sprite();
var level_height = l.length;
var level_width = l[0].length;
addChild(level_container);
for (j=0; j<level_height; j++) {
for (i=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]);
level_container.addChild(t);
}
}
}
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;
level_container.addChild(h);
}
public function on_enter_frame(event:Event) {
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") {
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;
xspeed -= bonus_speed;
}
public function ground_under_feet() {
bonus_speed = 0;
var left_foot_x = Math.floor((x_pos-6)/tile_size);
var right_foot_x = Math.floor((x_pos+5)/tile_size);
var foot_y = Math.floor((y_pos+9)/tile_size);
var left_foot = level[foot_y][left_foot_x];
var 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 :
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;
}
}
public function check_collisions() {
y_pos += yspeed;
get_edges();
// collision to the bottom
if (yspeed>0) {
if ((bottom_right != 0 && bottom_right != 6) || (bottom_left != 0 && bottom_left != 6)) {
if (bottom_right != 5 && bottom_left != 5) {
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;
}
}
}
}
// collision to the top
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();
// collision to the left
if (xspeed<0) {
if ((top_left != 0 && top_left != 5 && top_left != 6) || (bottom_left != 0 && bottom_left != 5 && bottom_left != 6)) {
x_pos = (left+1)*tile_size+6;
xspeed = 0;
}
}
// collision to the right
if (xspeed>0) {
if ((top_right != 0 && top_right != 5 && top_right != 6) || (bottom_right != 0 && bottom_right != 5 && bottom_right != 6)) {
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];
}
}
} |
The result is the same, so no need to publish it… just download the source code.
» 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.
They can be easily customized to meet the unique requirements of your project.
9 Responses to “New tile based platform engine – AS3 version”
Leave a Reply
Trackbacks
-
Introduction to AS3 classes : Emanuele Feronato on
October 28th, 2008 11:16 am
[...] taken it from New tile based platform engine – AS3 version. PLAIN TEXT [...]
- 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)


(8 votes, average: 3.75 out of 5)





haha, i did nearly the exact same thing last night and was going to send as comment but no need. nice work.
How is this supposed to work? While file do you compile?
make sure its not in a temp directory. extract elements to desktop or folder and run the as file. worked fine for me. you might want to explain the code/platform tile engines more than you did but it was well written. gl john
did you delete comment? there was question but seems it got deleted… i have a version of a engine ive been wokring on that allows the user to change the enviorment as they play. i can upload if u want
I never delete comments.
I am working on the same thing you are working on… for a game I am about to release.
If you want me to publish it, send it by email at info@emanueleferonato.com
You’re trying to manage too many separate tasks in a single class which is never a good idea. Try to break tasks into categories like map management, a player movement controller, and maybe a game drawing class.
Your enter frame function should look something like this:
playerController.update();
mapSystem.update();
collisionManager.check();
gfx.draw();
Thanks for the AS3 version! I was looking for something like this! ;-)
what other tiles are you working on Emanuele?