Chopper Command: Player Movement

Next comes the player controls. Chopper Command had a pretty unique way of handling change of direction and it was one of the main challenges in the game play, so I took special care in replicating it.

So basically, as you turn the chopper around, the whole screen must scroll as the chopper moves to its proper position on the other side of the screen. This, in the actual game, will make the enemies scroll towards the player.

Here is what I mean:

Use arrows to control and spacebar to shoot

Although the full effect can't be experienced yet without the enemies on screen.

I decided to implement parallax in the game, even though the original did not have it and the effect causes some discrepancies with the mountains in the radar tile, as we'll see later. If I were to make the mountains behave in the same way as in the original game, I would make them part of the GameTile.

But here, in this version, they sit on the main stage behind everything else.

The Main Class

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
 
package 
 
{
 
 
 
	import flash.display.Sprite;
 
	import flash.events.Event;
 
	import flash.events.MouseEvent;
 
	import flash.events.KeyboardEvent;
 
	import flash.geom.Rectangle;
 
 
 
	public class Main extends Game
 
	{
 
 
 
		private var _player:Player;
 
 
 
		private var _mountains1:Sprite;
 
		private var _mountains2:Sprite;
 
 
 
 
 
		public function Main ()
 
		{
 
 
 
			super ();
 
 
 
			init ();
 
		}
 
 
 
		private function init ():void
 
		{
 
 
 
			this.addChild ( new MainBgSkin());
 
 
 
			//add 3 moutain sprite inside each moutain container
 
			_mountains2 = new Sprite();
 
			_mountains2.addChild ( new Mountain2 ());
 
			this.addChild (_mountains2);
 
 
 
			var mountain:Sprite;
 
			mountain = new Mountain2();
 
			mountain.x = mountain.width;
 
			_mountains2.addChild (mountain);
 
 
 
			mountain = new Mountain2();
 
			mountain.x = 2 * mountain.width;
 
			_mountains2.addChild (mountain);
 
			_mountains2.y = 91;
 
 
 
 
 
 
 
 
 
			_mountains1 = new Sprite();
 
			_mountains1.addChild ( new Mountain1 ());
 
			_mountains1.y = 109;
 
			this.addChild (_mountains1);
 
			_mountains1.x = _mountains2.x = -500;
 
 
 
			mountain = new Mountain1();
 
			mountain.x = mountain.width;
 
			_mountains1.addChild (mountain);
 
 
 
			mountain = new Mountain1();
 
			mountain.x = 2 * mountain.width;
 
			_mountains1.addChild (mountain);
 
 
 
 
 
			_player = new Player (this, new Rectangle (stage.stageWidth * 0.1, stage.stageHeight * 0.25, 
 
			stage.stageWidth * 0.8, stage.stageHeight * 0.56));
 
 
 
			this.addChild (_player);
 
 
 
			this.addEventListener (Event.ENTER_FRAME, onLoop);
 
			stage.addEventListener (KeyboardEvent.KEY_DOWN, onKey_Down);
 
			stage.addEventListener (KeyboardEvent.KEY_UP, onKey_Up);
 
 
 
 
 
 
 
		}
 
 
 
		private function onLoop (event:Event):void
 
		{
 
			_player.update ();
 
 
 
 
 
			_mountains2.x +=  _player.screenScroll * 0.5;
 
			_mountains1.x +=  _player.screenScroll * 2;
 
 
 
			if (_player.screenScroll < 0)
 
			{
 
 
 
				if (_mountains2.x <  -  Game.SCREEN_WIDTH)
 
				{
 
					_mountains2.x += Game.SCREEN_WIDTH - ((_mountains2.x + Game.SCREEN_WIDTH));
 
				}
 
 
 
				if (_mountains1.x <  -  Game.SCREEN_WIDTH)
 
				{
 
					_mountains1.x += Game.SCREEN_WIDTH - ((_mountains1.x + Game.SCREEN_WIDTH));
 
				}
 
 
 
			}
 
			else if (_player.screenScroll > 0)
 
			{
 
 
 
				if (_mountains2.x > -100)
 
				{
 
					_mountains2.x -=  Game.SCREEN_WIDTH + _mountains2.x + 100;
 
				}
 
 
 
				if (_mountains1.x > -100)
 
				{
 
					_mountains1.x -=  Game.SCREEN_WIDTH + _mountains1.x + 100;
 
				}
 
 
 
			}
 
 
 
 
 
		}
 
 
 
 
 
		private function onKey_Up (event:KeyboardEvent):void
 
		{
 
 
 
			switch (event.keyCode)
 
			{
 
				case 38 :
 
					//UP KEY is up
 
					_player.moveUp = false;
 
					break;
 
				case 39 :
 
					//RIGHT KEY is up
 
					_player.moveRight = false;
 
					break;
 
				case 37 :
 
					//LEFT Key is up
 
					_player.moveLeft = false;
 
					break;
 
				case 40 :
 
					//DOWN KEY is up
 
					_player.moveDown = false;
 
					break;
 
				case 32 :
 
					//SHOOT
 
					_player.shooting = false;
 
					break;
 
			}
 
		}
 
		private function onKey_Down (event:KeyboardEvent):void
 
		{
 
 
 
			switch (event.keyCode)
 
			{
 
				case 38 :
 
					//UP KEY is down
 
					_player.moveUp = true;
 
					break;
 
				case 39 :
 
					//RIGHT KEY is down
 
					_player.moveRight = true;
 
 
 
					break;
 
				case 37 :
 
					//LEFT Key is down
 
					_player.moveLeft = true;
 
 
 
					break;
 
				case 40 :
 
					//DOWN KEY is down
 
					_player.moveDown = true;
 
					break;
 
				case 32 :
 
					//SPACE BAR is down
 
					_player.shooting = true;
 
					break;
 
 
 
			}
 
 
 
		}
 
	}
 
 
 
}

The Player Class

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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
 
package 
 
{
 
 
 
	import flash.display.Sprite;
 
	import flash.text.TextField;
 
	import flash.geom.Point;
 
	import flash.geom.Rectangle;
 
	import flash.display.MovieClip;
 
 
 
	public class Player extends GameSprite
 
	{
 
 
 
		private static const ACCELERATION:int = 16;
 
 
 
		public var screenScroll:Number = 0;
 
 
 
		public var moveLeft:Boolean = false;
 
 
 
		public var moveRight:Boolean = false;
 
 
 
		public var moveUp:Boolean = false;
 
 
 
		public var moveDown:Boolean = false;
 
 
 
		public var shooting:Boolean = false;
 
 
 
		private var _nextPosition:Point;
 
 
 
		private var _vector:Point = new Point(0,0);
 
 
 
		private var _minX:Number;
 
 
 
		private var _maxX:Number;
 
 
 
		private var _minY:Number;
 
 
 
		private var _maxY:Number;
 
 
 
		private var _range:Number;
 
 
 
		private var _acceleration:Point = new Point(0.5,4);
 
 
 
		private var _maxSpeed:Number = 20;
 
 
 
		private var _transition:Number = 0;
 
 
 
		private var _screenScrollNow:Number = 0;
 
 
 
		private var _screenScrollTarget:Number;
 
 
 
		private var _bulletsPool:Array = [];
 
 
 
		private var _bulletsPoolIndex:int = 0;
 
 
 
		private var _timeBetweenBullets:int = 0;
 
 
 
		private var _intervalBetweenBullets:int = 8;
 
 
 
 
 
 
 
 
 
		public function Player (game:Game, boundaries:Rectangle)
 
		{
 
 
 
			super (game);
 
 
 
			this.addChild (new PlayerSkin());
 
 
 
			_nextPosition = new Point(x,y);
 
 
 
			_minX = boundaries.x;
 
			_maxX = boundaries.right;
 
			_minY = boundaries.y;
 
			_maxY = boundaries.bottom;
 
			_range = boundaries.width * 0.35;
 
 
 
			x = _minX;
 
			y = _minY + (_maxY - _minY) * 0.5;
 
 
 
			_vector = new Point(0,0);
 
			squaredRadius = Math.pow(this.width * 0.5,2);
 
 
 
			var bullet:Bullet;
 
			for (var i:int = 0; i < 50; i++)
 
			{
 
				bullet = new Bullet(game);
 
				game.addChild (bullet);
 
				_bulletsPool.push (bullet);
 
			}
 
 
 
		}
 
 
 
 
 
 
 
		public function update ():void
 
		{
 
 
 
			if (shooting)
 
			{
 
				_timeBetweenBullets++;
 
				if (_timeBetweenBullets > _intervalBetweenBullets)
 
				{
 
					_timeBetweenBullets = 0;
 
					shoot ();
 
				}
 
 
 
			}
 
 
 
			//handle up and down movement (easy peasy)
 
			_vector.y = 0;
 
 
 
			if (moveUp)
 
			{
 
				_vector.y =  -  _acceleration.y;
 
			}
 
			else if (moveDown )
 
			{
 
				_vector.y = _acceleration.y;
 
			}
 
			_nextPosition.y +=  _vector.y;
 
 
 
			if (_nextPosition.y < _minY)
 
			{
 
				_nextPosition.y = _minY;
 
			}
 
			if (_nextPosition.y > _maxY)
 
			{
 
				_nextPosition.y = _maxY;
 
			}
 
 
 
 
 
			//handle flip
 
			if (this.scaleX > 0 && moveLeft)
 
			{
 
				this.scaleX = -1;
 
			}
 
			if (this.scaleX < 0 && moveRight)
 
			{
 
				this.scaleX = 1;
 
			}
 
 
 
 
 
			//if player is inside update region
 
			if ((this.scaleX > 0 && this.x <= _minX + _range) 
 
			|| (this.scaleX < 0 && this.x >= _maxX - _range ))
 
			{
 
 
 
				if (moveRight || moveLeft)
 
				{
 
					_vector.x +=  _acceleration.x;
 
				}
 
				else
 
				{
 
					_vector.x =  -  _acceleration.x * ACCELERATION;
 
				}
 
				if (this.scaleX > 0)
 
				{
 
					_screenScrollTarget = _maxSpeed * ((this.x - _minX)/(_minX + _range)) * -1;
 
				}
 
				if (this.scaleX < 0)
 
				{
 
					_screenScrollTarget = _maxSpeed * ((_maxX - this.x )/(_minX + _range));
 
 
 
 
 
				}
 
			}
 
			else
 
			{
 
				//player is flipping
 
				_vector.x =  -  _acceleration.x * ACCELERATION;
 
				if (_screenScrollTarget != _vector.x * this.scaleX)
 
				{
 
					_transition = 0;
 
					_screenScrollTarget = _vector.x * this.scaleX;
 
				}
 
			}
 
 
 
			if (_vector.x > _maxSpeed)
 
			{
 
				_vector.x = _maxSpeed;
 
			}
 
			if (_vector.x <  -  _maxSpeed)
 
			{
 
				_vector.x =  -  _maxSpeed;
 
			}
 
 
 
			_nextPosition.x +=  _vector.x * this.scaleX;
 
 
 
			if (this.scaleX > 0 && this.x <= _minX + _range)
 
			{
 
				if (_nextPosition.x >= _minX + _range)
 
				{
 
					_nextPosition.x = _minX + _range;
 
				}
 
			}
 
 
 
			if (this.scaleX < 0 && this.x >= _maxX - _range)
 
			{
 
				if (_nextPosition.x <= _maxX - _range)
 
				{
 
					_nextPosition.x = _maxX - _range;
 
				}
 
			}
 
 
 
			if (_nextPosition.x < _minX)
 
			{
 
				_nextPosition.x = _minX;
 
			}
 
			if (_nextPosition.x > _maxX)
 
			{
 
				_nextPosition.x = _maxX;
 
			}
 
 
 
 
 
			this.x = _nextPosition.x;
 
			this.y = _nextPosition.y;
 
 
 
			updateScreenScroll ();
 
 
 
			updateBullets ();
 
 
 
		}
 
 
 
		override public function set x (value:Number):void
 
		{
 
			super.x = value;
 
			_nextPosition.x = value;
 
		}
 
 
 
		override public function set y (value:Number):void
 
		{
 
			super.y = value;
 
			_nextPosition.y = value;
 
		}
 
 
 
 
 
		public function reset ():void
 
		{
 
 
 
		}
 
 
 
		public function get nextPosition ():Point
 
		{
 
			return _nextPosition;
 
		}
 
 
 
		private function updateScreenScroll ():void
 
		{
 
 
 
 
 
			if (_screenScrollNow != _screenScrollTarget)
 
			{
 
 
 
				_screenScrollNow = _screenScrollNow + (_screenScrollTarget - _screenScrollNow) * _transition;
 
				_transition +=  0.01;
 
 
 
				screenScroll = _screenScrollNow;
 
			}
 
 
 
			if (_transition > 1)
 
			{
 
				_transition = 0;
 
 
 
 
 
			}
 
		}
 
 
 
		private function updateBullets ():void
 
		{
 
 
 
			for each (var bullet:Bullet in _bulletsPool)
 
			{
 
				if (bullet.visible)
 
				{
 
					bullet.update ();
 
				}
 
			}
 
 
 
		}
 
 
 
		private function shoot ():void
 
		{
 
 
 
			var bullet:Bullet = _bulletsPool[_bulletsPoolIndex];
 
			_bulletsPoolIndex++;
 
			_bulletsPoolIndex = _bulletsPoolIndex % _bulletsPool.length;
 
 
 
			bullet.reset (this);
 
		}
 
 
 
 
 
 
 
	}
 
 
 
}

The public property screenScroll will be used later to scroll the GameTile objects as well. Right now it's only being used in the parallax effect.

The interesting bit in the logic is where you find the commented line "if player is inside update region." So I divide the screen into three vertical sections. The player may flip the chopper at any given time, but acceleration will only be possible if the chopper is in the correct section. So when facing left, the chopper must be within this invisible left section of the screen for acceleration to take effect. The same thing occurs when facing right and with the right-side section. While the chopper is moving to is proper section, acceleration is blocked.

So the player can be in one of two states. Either accelerating, or being moved to its proper side of the screen according to its orientation (scaleX).

Another effect that was quite hard to reproduce was the sense of inertia when acceleration returns. So that as the chopper reaches the right side of the screen, if the player is accelerating, the chopper will counteract the acceleration for a brief moment. It does add a nice effect though!

The Bullet Class

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
 
package 
 
{
 
 
 
	import flash.display.Sprite;
 
	import flash.text.TextField;
 
	import flash.geom.Point;
 
	import flash.geom.Rectangle;
 
 
 
	public class Bullet extends GameSprite
 
	{
 
 
 
		private static const START_COLOR:uint = 0x222222;
 
 
 
		private static const END_COLOR:uint = 0x3366CC;
 
 
 
 
 
		public var firing:Boolean = false;
 
 
 
		public var direction:int;
 
 
 
		private var _w:Number = 1;
 
 
 
		private var _xSpeed:Number;
 
 
 
		private var _wSpeed:Number = 32;
 
 
 
		private var _direction:int;
 
 
 
		private var _progress:Number = 0;
 
 
 
		private var _color:uint;
 
 
 
 
 
		public function Bullet (game:Game)
 
		{
 
 
 
			super (game);
 
			visible = false;
 
		}
 
 
 
 
 
		public function update ():void
 
		{
 
 
 
			this.x +=  _xSpeed * _direction;
 
 
 
			_xSpeed +=  8;
 
			_w +=  _wSpeed;
 
			_color = _color + (END_COLOR - START_COLOR) * _progress;
 
			this.graphics.clear ();
 
			this.graphics.beginFill (_color);
 
			this.graphics.drawRect (0,0,_w, 4);
 
 
 
			_progress +=  0.001;
 
			if (_progress > 1)
 
			{
 
				_progress = 1;
 
			}
 
 
 
			if (this.x > Game.SCREEN_WIDTH)
 
			{
 
				visible = false;
 
 
 
			}
 
		}
 
 
 
		public function checkCollision (enemyY:Number, enemyX:Number):Boolean
 
		{
 
 
 
			return false;
 
 
 
		}
 
 
 
		public function reset (player:Player):void
 
		{
 
 
 
			_direction = player.scaleX;
 
 
 
			this.scaleX = _direction;
 
			this.x = player.x + 16 * _direction;
 
			this.y = player.nextPosition.y;
 
 
 
			_color = START_COLOR;
 
 
 
			_xSpeed = 32;
 
			_w = 16;
 
			_progress = 0;
 
 
 
			this.graphics.clear ();
 
			this.graphics.beginFill (_color);
 
			this.graphics.drawRect (0,0,_w, 4);
 
 
 
			visible = true;
 
		}
 
 
 
	}
 
 
 
}

With the Bullet class I use interpolation to change the bullet color from within a set range. And that's it. For now.