View difference between Paste ID: eLtQyc5b and LRykpH83
SHOW: | | - or go back to the newest paste.
1
package 
2
{
3
	import flash.display.MovieClip;
4
	import flash.display.FrameLabel;
5
	import flash.display.Stage;
6
	import flash.utils.Timer;
7
	import flash.events.TimerEvent;
8
	import flash.events.Event;
9
	import flash.events.KeyboardEvent;
10
	import flash.ui.Keyboard;
11
	import flash.events.MouseEvent;
12
	import flash.ui.Mouse;
13
	import fl.motion.easing.Back;
14
15
	public class AvoiderGame extends MovieClip
16
	{
17
		public var gadjet:Gadjet;
18
		public var laser:Laser;
19
		public var coinsArray:Array;
20
		public var bulletsArray:Array;
21
		public var newBullet:Bullet;
22
		public var army:Array;
23
		public var enemy:Enemy;
24
		public var gameTimer:Timer;
25
		public var backG:Background;
26
		public var grass:Grass;
27
		public var tile:Tile;
28
		public var bullet:Bullet;
29
		public var gadjetIsMoving:Boolean;
30
		public var leftKeyPressed:Boolean;
31
		public var rightKeyPressed:Boolean;
32
		public var upKeyPressed:Boolean;
33
		public var downKeyPressed:Boolean;
34
		public var spaceBarPressed:Boolean;
35
		public var canFire:Boolean;
36
		// make the jail class available for this class
37
		public var jail:Jail;
38
39
		public function AvoiderGame()
40
		{
41
			Mouse.hide();
42
			var newEnemy = new Enemy(300,200);
43
			army = new Array();
44
			army.push( newEnemy );
45
			addChild( newEnemy );
46
47
			gadjet = new Gadjet(125, 240);
48
			mcGameStage.addChildAt(gadjet, 0);
49
			
50
			leftKeyPressed:Boolean;
51
		        rightKeyPressed:Boolean;
52
		        upKeyPressed:Boolean;
53
		        downKeyPressed:Boolean;
54
		
55
			// instantiate the jail class
56
			jail = new Jail(50, 195);
57
			mcGameStage.addChildAt(jail, 1); // add it to the stage at layer 1
58
			
59
			laser = new Laser();
60
			mcGameStage.addChildAt(laser, 1); // same here
61
			
62
			bulletsArray = new Array();
63
			newBullet = new Bullet(50, 50);
64
65
			grass = new Grass(0, 385);
66
			mcGameStage.addChildAt(grass, 1); // and here
67
			
68
			tile = new Tile(68, 261);
69
			mcGameStage.addChildAt(tile, 1); // here too
70
71
			backG = new Background(-5,10);
72
			mcGameStage.addChildAt(backG, 0); // here too, but at layer 0
73
			
74
			gameTimer = new Timer(25);
75
			gameTimer.addEventListener(TimerEvent.TIMER, onTick);
76
			gameTimer.start();
77
			
78
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
79
			addEventListener(Event.ENTER_FRAME, update);
80
		}
81
		public function addedToStage(e:Event):void
82
		{
83
			stage.addEventListener(KeyboardEvent.KEY_DOWN, holdingKey);
84
			stage.addEventListener(KeyboardEvent.KEY_UP, releasedKey);
85
		}
86
		public function holdingKey(e:KeyboardEvent):void
87
		{
88
			if(e.keyCode == Keyboard.DOWN && jail.Jail2.currentLabel == "damage_5") // left arrow key
89
			{
90
				downKeyPressed = true;
91
			}
92
			else if(e.keyCode == Keyboard.UP && jail.Jail2.currentLabel == "damage_5") // right arrow key
93
			{
94
				upKeyPressed = true;
95
			}
96
			else if(e.keyCode == Keyboard.LEFT && jail.Jail2.currentLabel == "damage_5") // up arrow key
97
			{
98
				leftKeyPressed = true;
99
			}
100
			else if(e.keyCode == Keyboard.RIGHT && jail.Jail2.currentLabel == "damage_5") // down arrow key
101
			{
102
				rightKeyPressed = true;
103
				gadjetIsMoving = true;
104
			}
105
			else if(e.keyCode == 32) // space key
106
			{
107
				spaceBarPressed = true;
108
			}
109
		}
110
		public function releasedKey(e:KeyboardEvent):void
111
		{
112
			//gadjet.gravity = 0;
113
			
114
			if(e.keyCode == Keyboard.DOWN) // left arrow key
115
			{
116
				downKeyPressed = false;
117
			}
118
			else if(e.keyCode == Keyboard.UP) // right arrow key
119
			{
120
				upKeyPressed = false;
121
			}
122
			else if(e.keyCode == Keyboard.LEFT) // up arrow key
123
			{
124
				leftKeyPressed = false;
125
			}
126
			else if(e.keyCode == Keyboard.RIGHT) // down arrow key 
127
			{
128
				rightKeyPressed = false;
129
			}
130
			else if(e.keyCode == 32) // space key
131
			{
132
				spaceBarPressed = false;
133
			}
134
		}
135
		public function goToNextLabel(mc:MovieClip):void 
136
		{
137
		      // recorremos todos los labels de mc
138
		      trace(mc.currentLabel);
139
		      for(var i=0; i < mc.currentLabels.length; i++)
140
		      {
141
			   // if currentLabels[i] matches with currentLabel, and currentLabels[i] is not the last...
142
                           if(mc.currentLabel == mc.currentLabels[i].name && i < mc.currentLabels.length - 1)
143
			   {
144
				// ... go to the next mc label and end the function
145
                                mc.gotoAndStop(mc.currentLabels[i+1].name);
146
                                return;
147
                           }
148
                     }
149
              }
150
	      public function onTick(timerEvent:TimerEvent):void
151
	      {
152
		    gameClock.addToValue(25);
153
		    if(Math.random()<0.01)
154
		    {
155
			var randomY:Number = Math.floor(Math.random() * 300) + 50;
156
			var newEnemy:Enemy = new Enemy(400,randomY);
157
			army.push(newEnemy);
158
			addChild(newEnemy);
159
			gameScore.addToValue(10);
160
		    }
161
		    laser.x = mouseX;
162
		    laser.y = mouseY;
163
164
		    var gadjetHasBeenHit:Boolean = false;
165
		    var hasCollided:Boolean;
166
			
167
		    if(downKeyPressed)
168
		    {
169
			gadjet.moveABit(0, 2);
170
		    }
171
		    else if(upKeyPressed)
172
		    {
173
			gadjet.moveABit(0, -2);
174
		    }
175
		    else if(leftKeyPressed)
176
		    {
177
			gadjet.moveABit(-2, 0);
178
		    }
179
		    else if(rightKeyPressed)
180
		    {
181
			gadjet.moveABit(2, 0);
182
		    }
183
		    else if(spaceBarPressed)
184
		    {
185
			canFire = true;
186
		     }
187
		     // for each enemy that is in the enemy array
188
		     for each(var enemy:Enemy in army)
189
		     {
190
			// move the enemy
191
			enemy.moveDownABit();
192
			// if we collided with the enemy
193
			if(PixelPerfectCollisionDetection.isColliding(gadjet,enemy,this,true))
194
			{
195
			     // stop counting every 25 miliseconds
196
			     gameTimer.stop();
197
			     gadjetHasBeenHit = true; // set this value to true
198
			}
199
			if(gadjetHasBeenHit) // if true
200
			{
201
			      coinsArray.length = 0; // the array lenght of the coins will be equal to 0
202
			      army.length = 0; // the army lenght too
203
			      dispatchEvent(new GadjetEvent(GadjetEvent.DEAD)); // when this event is dispatched the GameOverScreen is displayed
204
			}
205
		     }
206
		    for each(var enemy:Enemy in army)
207
		    {
208
			if(PixelPerfectCollisionDetection.isColliding(jail,enemy,this,true))
209
			{
210
			     removeChild(enemy);
211
			     goToNextLabel(jail.Jail2);
212
			}
213
		    }
214
		    if(PixelPerfectCollisionDetection.isColliding(laser,enemy,this,true))
215
		    {
216
			enemy.visible = false;
217
			trace("You collided with the blade");
218
			return;
219
		    }
220
		    // if gadjet is moving right
221
		    if(gadjetIsMoving)
222
		    {
223
			backG.moveBackground();
224
			grass.moveGrass();
225
			tile.moveTile();
226
		    }
227
		}
228
		public function update(e:Event):void
229
		{
230
		    if(canFire == true)
231
		    {
232
			trace("The two statements are true");
233
			var newBullet:Bullet;
234
			newBullet = new Bullet(gadjet.x, gadjet.y);
235
			bulletsArray.push(newBullet);
236
			mcGameStage.addChild(newBullet);
237
                        newBullet.firebullet(); /* as long as this function is executing inside an enter frame's event
238
                        it will make the bullet look moving  */
239
                        // this is the Bullet's class firebullet() function 
240-
                        public function firebullet():void
240+
                        /*public function firebullet():void
241
                        {
242
                           x = x + 5; // this makes the bullet look moving when using an ENTER_FRAME event
243-
                        } 
243+
                        }*/ 
244
		    } 
245
		}
246
		public function getFinalScore():Number
247
		{
248
			return gameScore.currentValue;
249
		}
250
		public function getFinalClockTime():Number
251
		{
252
			return gameClock.currentValue;
253
		}
254
	}
255
}