New tile based platform engine – part 11 – slopes part a

You asked for it, I made it… ladies and gentlemen… SLOPES! (applause).

I decided to split slopes in two parts, because they are a bit more difficult than other tile types.

In this part, you can walk on slopes but you can’t jump on/from them. Oh, well, you can actually jump or land on them, but without the second part of the code, there could be some glitches.

Obviously if you want to suggest your jumping routine, you’re welcome.

Slopes need a lot of rules in order to work, that can be summarized in one big rule: don’t make senseless slopes.

For senseless slopes I mean everything… senseless… refer to the picture:

Green shapes show some possible slopes while red ones show impossible ones.

Then the is_on_slope function solves slope walking.

Once I’ll publish jumping routine, I’ll write a detailed slope tutorial.

Meanwhile take 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
walkable_tiles = Array(0, 5, 6, 7, 10, 11);
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;
on_slope = false;
gravity = 0.5;
jump_speed = 6;
climbing = false;
climb_speed = 0.8;
level = new Array();
enemy = new Array();
coin = new Array();
key = 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, 2, 2, 1, 0, 4, 4, 8, 3, 3, 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, 5, 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, 10, 1, 11, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1];
level[8] = [1, 1, 1, 1, 0, 9, 0, 0, 10, 1, 1, 1, 11, 0, 7, 0, 0, 6, 0, 0, 0, 0, 7, 0, 1];
level[9] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 1, 4, 4, 1, 8, 1, 8, 1];
player = [10, 6];
enemy[0] = [10, 3, -2];
enemy[1] = [3, 3, -2];
coin[0] = [2, 2];
coin[1] = [23, 4];
key[0] = [1, 5, 5, 8];
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<key.length; x++) {
		key_mc = level_container.attachMovie("key", "key"+_root.level_container.getNextHighestDepth(), _root.level_container.getNextHighestDepth(), {_x:key[x][0]*tile_size+tile_size/2, _y:key[x][1]*tile_size+tile_size/2+1});
		key_mc.ind = x;
		key_mc.onEnterFrame = function() {
			if (this.hitTest(level_container.hero._x, level_container.hero._y, true)) {
				open_x = [key[this.ind][2]];
				open_y = [key[this.ind][3]];
				level[open_y][open_x] = 0;
				_root.level_container["t"+open_y+"_"+open_x].removeMovieClip();
				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();
			}
		default :
			over = "ground";
			speed = ground_acceleration;
			friction = ground_friction;
			break;
	}
}
function check_collisions() {
	get_edges();
	is_on_slope();
	y_pos += yspeed;
	get_edges();
	// collision to the bottom 
	if (yspeed>0 and !on_slope) {
		if ((bottom_right != 0 and bottom_right != 6 and bottom_right != 10) or (bottom_left != 0 and bottom_left != 6 and bottom_left != 10)) {
			// 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 and !on_slope) {
		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 (!is_walkable(top_left) or !is_walkable(bottom_left)) {
			x_pos = (left+1)*tile_size+6;
			xspeed = 0;
		}
	}
	// collision to the right                                                                                                                          
	if (xspeed>0) {
		if (!is_walkable(top_right) or !is_walkable(bottom_right)) {
			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});
}
function is_walkable(tile) {
	walkable = false;
	if (!on_slope) {
		for (x=0; x<walkable_tiles.length; x++) {
			if (tile == walkable_tiles[x]) {
				walkable = true;
				break;
			}
		}
	}
	else {
		walkable = true;
	}
	return (walkable);
}
function is_on_slope() {
	x_slope_detector = Math.floor(x_pos/tile_size);
	y_slope_detector = Math.floor(y_pos/tile_size);
	if (!jumping and !falling) {
		if (level[y_slope_detector+1][x_slope_detector] == 10 or level[y_slope_detector+1][x_slope_detector] == 11) {
			y_pos = (y_slope_detector+1)*tile_size+tile_size/2+1;
			y_slope_detector += 1;
		}
		if (level[y_slope_detector][x_slope_detector] == 10 or level[y_slope_detector][x_slope_detector] == 11) {
			if (level[y_slope_detector][x_slope_detector] == 10) {
				x_offset = tile_size-x_pos%tile_size;
			}
			// could be an "else"...   
			if (level[y_slope_detector][x_slope_detector] == 11) {
				x_offset = x_pos%tile_size;
			}
			y_pos = Math.floor(y_pos/tile_size)*tile_size-9+x_offset;
			on_slope = true;
		}
		else {
			on_slope = false;
		}
	}
	if (!jumping and !falling and !on_slope and over != "ladder") {
		y_pos = (y_slope_detector)*tile_size+tile_size/2+1;
	}
}

And the final example:

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (18 votes, average: 4.39 out of 5)
Loading ... Loading ...
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.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 21 comments

  1. Dude

    on October 19, 2008 at 7:22 pm

    Hello mr. Feronato,

    Nice to see the next part of your tutorial.
    I was browsing for part 10 and when i reloaded the flash section part 11 was online =)
    Big THanks

  2. Niall Lavigne

    on October 19, 2008 at 7:47 pm

    Dang, looks nice. Can’t wait for the 2nd slopes part, it surprised me when I fell through after jumping!

  3. Daniel Rodriguez

    on October 19, 2008 at 7:51 pm

    wow!! that so nice It has a lot of bugs how you said, but anyway its nice, cant way to part 2 for make my own slopes.
    Thanks

  4. Arxanas

    on October 19, 2008 at 10:16 pm

    Well, the slopes work except for the jumping thing, but there isn’t any speed difference between when you go up and down. I see you made the patrols kill you, though.

  5. Gecko

    on October 19, 2008 at 11:03 pm

    Very glitchy, but cool as always!

  6. lartar

    on October 19, 2008 at 11:48 pm

    wow, i jump and the hero dissapear :P i think ibroke it

  7. Andy Cook

    on October 20, 2008 at 1:31 am

    Awesome! I am taking a look at the code right now. I am interested to see how you did this.

    Keep up the good work! This tutorial series is really great so far. Brings back the feeling of reading the “Create a Flash Game” series years ago. I can think of so many great applications to make from this stuff…Thanks!

  8. stupid

    on October 20, 2008 at 5:02 am

    Make it so that you rotate to the angle of the slope too.

  9. Massimo M.

    on October 20, 2008 at 12:48 pm

    bug report:
    if i jump from the slope to the <– tile i fall down !!!!!

  10. Monkios

    on October 20, 2008 at 3:01 pm

    Massimo M. : If you jump from from the slopes to whatever tile, you fall down.

    I’m interested in knowing why some slopes are good and some are bad. In games like “N : The way of the ninja” there are lots of what you would call senseless.

  11. Daniel Rodriguez

    on October 20, 2008 at 9:41 pm

    Monkios: It depends of the code, in this case, a lot of the slopes are senseless if you code more and make it better just a few of slopes will be senseless.

  12. fraser

    on October 21, 2008 at 12:59 pm

    i really need to know how to make an exit because ive been using your tutorial to help me make a game which was due in a while ago but i couldnt work out how to make an exit or my teacher will go nuts thanks for all the other tutorials though.

  13. Thouble

    on October 21, 2008 at 8:50 pm

    Much better level design in this one, too. :>

  14. Harman

    on October 21, 2008 at 9:30 pm

    Nice but can you make the character rotate or slanted when hes walking up or down the slopes?

  15. Josh of Cubicle Ninjas

    on October 21, 2008 at 9:52 pm

    You’ve blown my mind.

  16. Maciek

    on October 22, 2008 at 11:22 pm

    It really easily buggs, just jump around the slope a few times, skipping from one side to other and you will fall through the ground.

  17. New tile based platform engine - part 11 - slopes part b : Emanuele Feronato

    on October 24, 2008 at 1:44 pm

    [...] said in New tile based platform engine – part 11 – slopes part a, the game needed a fix for the jump from/on slope [...]

  18. Adam

    on November 19, 2008 at 7:51 am

    I like the code, reminds me of the the objects/tile sets used for the game Agent Platformer. I noticed a slight bug though. I didn’t looks into the coding very deeply yet, but if you are standing on the very right part of the slope you start off on, then jump to the right it seems as if you fall through one of the tiles. I’m guessing its just a slight bug possibly with a “while” command being before or after and if, causing the character to fall through the ground in between a few frames. Looks very good though. Right now Im making a scroller box physics AS2 game in which I’m using a tad of your code. Would you suggest me to change some of your AS2 code into the main time line to speed it up a bit, or do you think it will be unnoticeable in speed? Thanks!

  19. help!

    on December 30, 2008 at 4:48 pm

    can u make a win tile so that when the player touches it, it goes to another frame, hence player won!?

  20. Gravyerd

    on March 22, 2009 at 9:43 am

    anyone know where is the map creator? (to create this lvls using an easy editor (not only numbers)

  21. KonkilA

    on November 29, 2009 at 11:25 pm

    Hello Mr Feronato!
    These tutorials are brilliant, and I am learning so much more about actionscript. However, on creating my own version of this I have encountered an error. I have a squirrel as the playable character, but he only faces one way when moving. I have set up two frames inside my hero movieclip, each with the squirrel facing a different direction, and a stop() command. Then, my right command reads:

    if (Key.isDown(Key.RIGHT)) {
    _root.hero.gotoAndPlay(2);
    xspeed += speed;
    walking = true;
    }

    however, the squirrel still faces left :(. Any ideas?