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
package 
{

	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.utils.getTimer;


	public class Platformerhowto extends MovieClip
	{

		private var hero:HeroMC;
		private var ground:GroundMC;
		private var platformMiddle:PlatformMC;
		private var platformMiddle2:PlatformMC;

		private var leftKey:Boolean = false;
		private var rightKey:Boolean = false;
		private var jumpKey:Boolean = false;
		private var runKey:Boolean = false;
		private var onGround:Boolean = false;
		private var falling:Boolean = false;

		private var walkSpeed:Number = 4;
		private var runSpeed:Number = 14;

		private var steps = 16;
		private var pSpeed = 4;
		private var pSpeed2 = 4;

		private var gravity:Number = 2;
		private var jumpSpeed:Number = 20;
		private var vx:Number = 0;
		private var vy:Number = 0;



		public function Platformerhowto()
		{
			init();
		}

		private function init():void
		{
			//Create and place the Hero, the Ground and the Platform 
			hero = new HeroMC  ;
			hero.x = stage.stageWidth * 0.5;
			hero.y = 30;
			addChild(hero);

			ground = new GroundMC  ;
			ground.x = stage.stageWidth * 0.5;
			ground.y = stage.stageHeight - 50;
			addChild(ground);

			platformMiddle = new PlatformMC  ;
			platformMiddle.x = 100;
			platformMiddle.y = 260;
			addChild(platformMiddle);

			platformMiddle2 = new PlatformMC  ;
			platformMiddle2.x = 250;
			platformMiddle2.y = 260;
			addChild(platformMiddle2);

			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKEYDOWN);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKEYUP);
			addEventListener(Event.ENTER_FRAME, onENTERFRAME);

		}

		private function onKEYDOWN(event:KeyboardEvent):void
		{

			var pressedKey = event.keyCode;

			if (pressedKey == Keyboard.LEFT)
			{
				leftKey = true;
			}
			if (pressedKey == Keyboard.RIGHT)
			{
				rightKey = true;
			}
			if (pressedKey == Keyboard.Z)
			{
				runKey = true;
			}
			if (pressedKey == Keyboard.X)
			{
				jumpKey = true;
			}

		}

		private function onKEYUP(event:KeyboardEvent):void
		{

			var releasedKey = event.keyCode;

			if (releasedKey == Keyboard.LEFT)
			{
				leftKey = false;
			}
			if (releasedKey == Keyboard.RIGHT)
			{
				rightKey = false;
			}
			if (releasedKey == Keyboard.Z)
			{
				runKey = false;
			}
			if (releasedKey == Keyboard.X)
			{
				jumpKey = false;

				if (vy <= 0)
				{
					vy = 0;
				}
			}

		}











		private function onENTERFRAME(event:Event):void
		{

			//Direction statements.
			if (leftKey)
			{
				vx =  -  walkSpeed;

				if (runKey)
				{
					vx =  -  runSpeed;
				}
			}

			if (rightKey)
			{
				vx = walkSpeed;

				if (runKey)
				{
					vx = runSpeed;
				}
			}


			//Initialize the jumping
			if (jumpKey && onGround)
			{
				vy = 0;
				gravity = 1.6;
				vy -=  jumpSpeed;
				onGround = false;
			}


			vy +=  gravity;


			//Move the platforms
			if (platformMiddle2.x > 400)
			{
				pSpeed2 = -4;
			}

			if (platformMiddle2.x < 250)
			{
				pSpeed2 = 4;
			}

			if (platformMiddle.y + 100 > 380)
			{
				pSpeed = -4;
			}
			if (platformMiddle.y < 200)
			{
				pSpeed = 4;
			}

			platformMiddle.y +=  pSpeed;
			platformMiddle2.x +=  pSpeed2;



			//If you are on the ground and not pressing directional keys (right or left), you stand still.
			if (onGround && ! leftKey && ! rightKey && ! falling)
			{
				vx = 0;
			}




			//Add velocity values to player
			//The for-loop represents multi-sampling. Within one frame, I divide the step that Hero takes into smaller steps, 
			//and at each of these small steps, I check if there is a collision to get a more accurate collision detection. 
			for (var i:int; i < steps; i++)
			{
				
				hero.y +=  vy / steps;
				hero.x +=  vx / steps;

				//Check for any collisions
				collisionVerticalPlatform();
				collisionHorisontalPlatform();
				
			}
			

		}
		
		
		
		
		
		
		
		

		private function collisionHorisontalPlatform():void
		{

			//The distance between Hero and the Platform
			var deltaX2:Number = hero.x - platformMiddle2.x;
			var deltaY2:Number = hero.y - platformMiddle2.y;

			//The amount of overlap between Hero shape and Platform shape. (The widths of both shapes, minus the distance between them)
			//If the distance between the two shapes is less than the added heights/widths of the shapes, then we are overlapping(IE the amount of overlap is over 0)
			var olapX2:Number = ((hero.width * 0.5) + (platformMiddle2.width * 0.5)) - Math.abs(deltaX2);
			var olapY2:Number = ((hero.height * 0.5) + (platformMiddle2.height * 0.5)) - Math.abs(deltaY2);


			//Check to see if there is an overlap on the X-axis (are the shapes intersecting on the X-axis)
			if (olapX2 > 0)
			{

				//Check to see if there is an overlap on the Y-axis as well.
				if (olapY2 > 0)
				{

					//If there is an overlap on BOTH axises, we have a collision.
					//Check to see which axis is overlapping the most.
					//If we are overlapping more on the X-axis, that means we are either colliding with the TOP of the platform or BOTTOM
					if (olapX2 > olapY2)
					{

						//If velocity value on Y-axis is less than 0 and we are beneath the platform, we are hitting BOTTOM side of platform.
						if (vy <= 0 && hero.y > platformMiddle2.y - 10)
						{

							hero.y = platformMiddle2.y + (hero.height * 0.5) + (platformMiddle2.height * 0.5);
							vy = 0;

						}

						//If velocity value on Y-axis is bigger than 0 and we are ABOVE platform, then we are hitting TOP side of platform.
						if (vy > 0 && hero.y < platformMiddle2.y)
						{

							hero.y = platformMiddle2.y - (hero.height * 0.5) - (platformMiddle2.height * 0.5);
							onGround = true;
							falling = false;
							vy = 0;
						
						//If we are standing on platform, and not pressing any key, you move along with platform speed.
							if (! rightKey && ! leftKey && !jumpKey)
							{
								vx = pSpeed2;
							}
							
							//If not, you need to add the platform speed to whatever initial speed hero has.
							else
							{
								
								if (rightKey)
								{
									if (pSpeed2 > 0)
									{
										
										if (runKey)
										{
											vx = pSpeed2 + runSpeed;
										}else{
											vx = pSpeed2 + walkSpeed;

										}

									}
								}

								if (leftKey)
								{
									if (pSpeed2 < 0)
									{
										vx = pSpeed2 - walkSpeed;

										if (runKey)
										{
											vx = pSpeed2 - runSpeed;
										}

									}
								}

							}


						}


					//But if the overlap is more on the Y-axis, that means we are colliding with either right side or left side.
					}
					else if (olapY2 > olapX2)
					{

						//If Heros position is to the left of the platform, we are hitting left side
						if (hero.x < platformMiddle2.x)
						{

							hero.x = platformMiddle2.x - (platformMiddle2.width * 0.5) - (hero.width * 0.5);
							vx = 0;


						}

						//If Heros position is to the right of the platform, we are hitting right side.
						if (hero.x > platformMiddle2.x)
						{

							hero.x = platformMiddle2.x + (platformMiddle2.width * 0.5) + (hero.width * 0.5);
							vx = 0;

						}

					}
				}
			}



		}


		private function collisionVerticalPlatform():void
		{

			var deltaX:Number = hero.x - platformMiddle.x;
			var deltaY:Number = hero.y - platformMiddle.y;

			var olapX:Number = ((hero.width * 0.5) + (platformMiddle.width * 0.5)) - Math.abs(deltaX);
			var olapY:Number = ((hero.height * 0.5) + (platformMiddle.height * 0.5)) - Math.abs(deltaY);

			if (olapX > 0)
			{

				if (olapY > 0)
				{

					if (olapX > olapY)
					{

						if (vy <= 0 && hero.y > platformMiddle.y - 10)
						{

							hero.y = platformMiddle.y + (hero.height * 0.5) + (platformMiddle.height * 0.5);
							vy = 0;

						}


						if (vy > 0 && hero.y < platformMiddle.y)
						{

							hero.y = platformMiddle.y - (hero.height * 0.5) - (platformMiddle.height * 0.5);
							onGround = true;
							falling = false;
							gravity = 0;
						}



					}
					else if (olapY > olapX)
					{

						if (hero.x < platformMiddle.x)
						{

							hero.x = platformMiddle.x - (platformMiddle.width * 0.5) - (hero.width * 0.5);
							vx = 0;


						}

						if (hero.x > platformMiddle.x)
						{

							hero.x = platformMiddle.x + (platformMiddle.width * 0.5) + (hero.width * 0.5);
							vx = 0;

						}

					}
				}
			}
			
			if ((hero.y + hero.height * 0.5) + vy > ground.y - (ground.height * 0.5))
			{
				vy = 0;
				hero.y = ground.y - (ground.height * 0.5) - (hero.height * 0.5);
				onGround = true;
				falling = false;
				gravity = 0;
			}


		}

		

	}

}