Created
February 23, 2012 19:19
-
-
Save Gi133/1894462 to your computer and use it in GitHub Desktop.
Flash game Assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Graphics; | |
| import flash.display.Sprite; | |
| /** | |
| * Bullets probably need a home, this is their home! | |
| * Currently only one type of ammo but with this class it's possible to add more if needed along with other kinds of munitions. | |
| * On call it will take the gun's muzzle coordinates and set them as starting coords for the bullet. | |
| * VARIABLE INFO: | |
| * range : How far the bullet travels before it vanishes. | |
| * direction: The direction the bullet was fired at. Left is False, Right is True. | |
| **/ | |
| public class Bullets extends Sprite | |
| { | |
| private var range:int = 500; | |
| private var direction:Boolean; | |
| private var distance:int = 0; | |
| private var speed:int = 10; | |
| public var expired:Boolean = false; | |
| public function Bullets() | |
| { | |
| direction = Game.player_direction; | |
| if (direction == true) | |
| { | |
| x = Game.player_X + 33; | |
| } | |
| else if (direction == false) | |
| { | |
| x = Game.player_X; | |
| } | |
| y = Main.STAGE_HEIGHT - 45; | |
| //Placeholder graphics | |
| graphics.lineStyle(1); | |
| graphics.beginFill(0x555555); | |
| graphics.drawCircle(0, 0, 2); | |
| graphics.endFill(); | |
| } | |
| public function update():void | |
| { | |
| //Keep bullet moving. | |
| if (direction == true) | |
| { | |
| x += speed; | |
| } | |
| else | |
| { | |
| x -= speed; | |
| } | |
| distance += speed; | |
| //Check range and remove bullet is necessary. | |
| if (distance >= range) | |
| { | |
| //Destroy Bullet | |
| expired = true; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Sprite; | |
| import flash.events.Event; | |
| public class Entities extends Sprite | |
| { | |
| //Values | |
| public const BASE_HEALTH : int = 30; | |
| public const BASE_DAMAGE : int = 10; | |
| public function Entities() | |
| { | |
| if (this.stage != null) | |
| { | |
| this.onInit(); | |
| } | |
| else | |
| { | |
| this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage); | |
| } | |
| } | |
| protected function onAddedToStage(e:Event):void | |
| { | |
| this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage); | |
| this.onInit(); | |
| this.onDraw(); | |
| } | |
| //Functions below get overridden in classes according to class. | |
| protected function onInit():void{} | |
| protected function onDraw():void{} | |
| protected function onAttack():void{} | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Sprite; | |
| import flash.events.Event; | |
| import flash.text.TextField; | |
| /** | |
| * Class that holds the game. | |
| **/ | |
| public class Game | |
| { | |
| //Constants | |
| private const MAX_MONSTERS:int = 6; //Maximum number of monsters at any given time | |
| //Variables | |
| public var wave:int = 1; | |
| public var kill_count:int = 0; | |
| public var monsters:Vector.<Monster>; | |
| public var bullets:Vector.<Bullets>; | |
| private var human:Player; | |
| public static var player_X:int = 0; | |
| public static var player_direction:Boolean = true; | |
| private var Game_Over:Boolean = false; | |
| //Wave parameters | |
| private const wave_monsters:int = 6; //The number of monsters in each wave. | |
| private var remaining_monsters:int = 6; | |
| private var Left_entry:int = 0; | |
| private var Right_entry:int = Main.STAGE_WIDTH - 10; | |
| private var stage:Sprite; | |
| private var UI:HUD; | |
| public function Game(root:Sprite) | |
| { | |
| stage = root; | |
| UI = new HUD(); | |
| stage.addChild(UI); | |
| human = new Player(); | |
| stage.addChild(human); | |
| //Set up the starting wave. | |
| onWaveStart(); | |
| human.addEventListener("PlayerFired", onPlayerFire); | |
| human.addEventListener("Dead", isDead); | |
| UI.updateHUD(human.health, human.max_health, wave, remaining_monsters, human.experience, human.required_experience, human.level); | |
| } | |
| private function onWaveStart():void | |
| { | |
| //Clear the monsters list and bullets. | |
| monsters = new Vector.<Monster>(); | |
| bullets = new Vector.<Bullets>(); | |
| //New wave has started. | |
| for (var i:int = 0; i < wave_monsters; i++) | |
| { | |
| //Figuring out the entry point and the entry offset for each zombie. | |
| var start_right:Boolean = Boolean (Math.round(Math.random())); | |
| if (start_right == true) | |
| { | |
| var monster:Monster = new Monster(Right_entry, wave); | |
| } | |
| else if (start_right == false) | |
| { | |
| var monster:Monster = new Monster(Left_entry, wave); | |
| } | |
| monsters.push(monster); | |
| } | |
| //Add the monsters on stage. | |
| for (var j:int = 0; j < monsters.length; j++) | |
| { | |
| stage.addChild(monsters[j]); | |
| } | |
| } | |
| public function moveMonsters():void | |
| { | |
| for (var i:int = 0; i < monsters.length; i++) | |
| { | |
| var monster:Monster = monsters[i]; | |
| monster.update(); | |
| } | |
| } | |
| private function onWaveEnd():void | |
| { | |
| //All monsters of current wave are dead. | |
| wave++; | |
| } | |
| private function onPlayerFire(e:Event):void | |
| { | |
| //Create a bullet | |
| var bullet:Bullets = new Bullets(); | |
| stage.addChild(bullet); | |
| bullets.push(bullet); | |
| } | |
| private function updateBullets():void | |
| { | |
| for (var i:int = 0; i < bullets.length; i++) | |
| { | |
| var bullet:Bullets = bullets[i]; | |
| bullet.update(); | |
| if (bullet.expired) | |
| { | |
| stage.removeChild(bullets[i]); | |
| bullets.splice(i,1); | |
| } | |
| } | |
| } | |
| private function detectHit():void | |
| { | |
| for (var b:int = 0; b <bullets.length; b++) | |
| { | |
| for (var m:int = 0; m < monsters.length; m++) | |
| { | |
| if(monsters[m].hitTestObject(bullets[b])) | |
| { | |
| bullets[b].expired = true; | |
| monsters[m].onHit(human.damage); | |
| if(monsters[m].health <= 0) | |
| { | |
| monsters[m].onDeath(); | |
| stage.removeChild(monsters[m]); | |
| monsters.splice(m,1); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| private function waveUpdate():void | |
| { | |
| if ((monsters.length == 0) && (bullets.length == 0)) | |
| { | |
| onWaveEnd(); | |
| onWaveStart(); | |
| } | |
| } | |
| private function isDead(e:Event):void | |
| { | |
| //Display results | |
| var Results:TextField = new TextField; | |
| Results.text = "You have managed to survive for "+wave+" waves! But in the end, you died..."; | |
| stage.addChild(Results); | |
| Results.width = 150; | |
| Results.autoSize = "left"; | |
| Results.background = true; | |
| Results.backgroundColor = 0x808080; | |
| Results.border = true; | |
| Results.wordWrap = true; | |
| Results.x = Main.STAGE_WIDTH/2; | |
| Results.y = Main.STAGE_HEIGHT/2; | |
| human.Animation_State = 3; | |
| human.y = Main.STAGE_HEIGHT - (human.height+45); | |
| Game_Over = true; | |
| } | |
| public function gameUpdate():void | |
| { | |
| //Update the UI. | |
| UI.updateHUD(human.health, human.max_health, wave, remaining_monsters, human.experience, human.required_experience, human.level); | |
| if (Game_Over) | |
| { | |
| stage.dispatchEvent(new Event("PAUSE")); | |
| } | |
| //Update the player. And also make human x position public | |
| human.update(); | |
| player_X = human.x; | |
| player_direction = human.direction; | |
| //Get the new value for remaining number of monsters. | |
| remaining_monsters = monsters.length; | |
| waveUpdate(); | |
| //Update the monsters. Move em about a bit. | |
| moveMonsters(); | |
| //Update bullets | |
| updateBullets(); | |
| detectHit(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Sprite; | |
| import flash.text.TextField; | |
| /** | |
| * This is the Graphical User Interface. | |
| **/ | |
| public class HUD extends Sprite | |
| { | |
| private var health:TextField; | |
| private var experience:TextField; | |
| private var wave:TextField; | |
| private var remaining:TextField; | |
| private var level:TextField; | |
| public var health_value:int = 0; | |
| public var maxhealth_value:int = 0; | |
| public var experience_value:int = 0; | |
| public var needed_xp_value:int = 0; | |
| public var wave_value:int = 0; | |
| public var remaining_value:int = 0; | |
| public function HUD() | |
| { | |
| health = new TextField; | |
| health.text = "Health: "+health_value+"/"+maxhealth_value; | |
| health.x = 25; | |
| this.addChild(health); | |
| experience = new TextField; | |
| experience.text = "Experience: "+experience_value+"/"+needed_xp_value; | |
| experience.x = Main.STAGE_WIDTH/4; | |
| experience.autoSize = "left"; | |
| this.addChild(experience); | |
| wave = new TextField; | |
| wave.text = "Wave: "+wave_value; | |
| wave.x = 25; | |
| wave.y = 15; | |
| this.addChild(wave); | |
| remaining = new TextField; | |
| remaining.text = "Remaining Monsters: "+remaining_value; | |
| remaining.autoSize = "left"; | |
| remaining.x = Main.STAGE_WIDTH-(remaining.width+25); | |
| this.addChild(remaining); | |
| level = new TextField; | |
| level.text = "Level: "; | |
| level.autoSize = "left"; | |
| level.x = 25; | |
| level.y = 27; | |
| this.addChild(level); | |
| } | |
| public function updateHUD(health_imp:int, maxhealth_imp:int, wave_imp:int, remaining_imp:int, experience_imp:int, needed_xp_imp:int, level_imp:int):void | |
| { | |
| health.text = "Health: "+health_imp+"/"+maxhealth_imp; | |
| experience.text = "Experience: "+experience_imp+"/"+needed_xp_imp; | |
| wave.text = "Wave: "+wave_imp; | |
| remaining.text = "Remaining Monsters: "+remaining_imp; | |
| level.text = "Level: "+level_imp; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Sprite; | |
| import flash.events.Event; | |
| import flash.events.TimerEvent; | |
| import flash.ui.Mouse; | |
| import flash.utils.Timer; | |
| public class Main extends Sprite | |
| { | |
| private var game:Game; | |
| private var arena:Map; | |
| private var Ticker:Timer; | |
| public static var STAGE_WIDTH:int; | |
| public static var STAGE_HEIGHT:int; | |
| private function onTicker (e:TimerEvent): void | |
| { | |
| stage.dispatchEvent(new Event("onTicker")); | |
| } | |
| public function Main():void | |
| { | |
| if (stage) init(); | |
| else addEventListener(Event.ADDED_TO_STAGE, init); | |
| } | |
| private function init(e:Event = null):void | |
| { | |
| //Initializing Constants | |
| STAGE_WIDTH = stage.stageWidth; | |
| STAGE_HEIGHT = stage.stageHeight; | |
| removeEventListener(Event.ADDED_TO_STAGE, init); | |
| Ticker = new Timer(1500, 0); | |
| Ticker.addEventListener(TimerEvent.TIMER, onTicker); | |
| Ticker.start(); | |
| game = new Game(this); | |
| arena = new Map(); | |
| addChild(arena); | |
| addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
| addEventListener("PAUSE", removeFrameListener); | |
| addEventListener("RESET", resetListener); | |
| } | |
| private function onEnterFrame(e:Event):void | |
| { | |
| game.gameUpdate(); | |
| } | |
| public function removeFrameListener(e:Event):void | |
| { | |
| removeEventListener(Event.ENTER_FRAME, onEnterFrame); | |
| trace("PAUSE"); | |
| } | |
| public function resetListener(e:Event):void | |
| { | |
| addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
| trace("RESET"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Sprite; | |
| public class Map extends Sprite | |
| { | |
| /** | |
| * Holds map objects | |
| */ | |
| public function Map() | |
| { | |
| //Floor | |
| var Height:int = Main.STAGE_HEIGHT-15; | |
| var Width:int = Main.STAGE_WIDTH; | |
| graphics.lineStyle(1); | |
| graphics.beginFill(000000); | |
| graphics.drawRect(0, Height, Width, 10); | |
| graphics.endFill(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Graphics; | |
| import flash.display.MovieClip; | |
| import flash.events.Event; | |
| import flash.events.EventDispatcher; | |
| import flash.events.TimerEvent; | |
| import flash.utils.Timer; | |
| /** | |
| * Monster class, extends Entities base class. | |
| * This is the class used for the enemies in the game | |
| * Most of the variables are affected according to the wave number to make the game progressively harder. | |
| * | |
| */ | |
| public class Monster extends Entities | |
| { | |
| public var xp_value:int; | |
| public var health:int = BASE_HEALTH; | |
| private var damage:int = BASE_DAMAGE; | |
| private var aggro_range:int = 50; | |
| private var speed:int = 1; | |
| private var Ready:Boolean; | |
| private var Attack_Ticker:Timer; | |
| //Body parts positions relative to torso | |
| private var head_y:int = -10; | |
| private var head_x:int = 5; | |
| private var torso_y:int = 0; | |
| private var torso_x:int = 0; | |
| //Location variables | |
| private var player_X:int; | |
| private var distance:uint; | |
| private var direction:Boolean; | |
| //Animation state: 0 Idle, 1 Moving, 2 Attacking. | |
| public var Animation_State:int = 0; | |
| public var CurrentAnimation:MovieClip = null; | |
| private var IdleAnimation:MovieClip = new Monster_Idle(); | |
| private var MovingAnimation:MovieClip = new Monster_Move(); | |
| private var AttackingAnimation:MovieClip = new Monster_Attack(); | |
| private var AnimationComplete:Boolean = true; | |
| public function Monster(x_coordinate:int, wave:int) | |
| { | |
| //Set Difficulty depending on wave | |
| health = health + wave; | |
| aggro_range += (wave * 2); | |
| speed += wave/2; | |
| Ready = true; | |
| x = x_coordinate; | |
| y = Main.STAGE_HEIGHT - 55; | |
| xp_value = 10+(wave*3); | |
| Attack_Ticker = new Timer(2000, 0); | |
| Attack_Ticker.addEventListener(TimerEvent.TIMER, Reset); | |
| super(); | |
| } | |
| private function resetAnimation(e:Event):void | |
| { | |
| AnimationComplete = true; | |
| } | |
| public function onHit(Player_AttackDamage:int):void | |
| { | |
| //Monster got hit, reduce HP by player's Attack Damage! | |
| health -= Player_AttackDamage; | |
| } | |
| private function Reset(e:Event):void | |
| { | |
| //Reset attack readyness to true. | |
| Ready = true; | |
| Attack_Ticker.reset(); | |
| } | |
| protected override function onInit():void | |
| { | |
| stage.addEventListener("onTicker", wander); | |
| } | |
| public function wander(e:Event):void | |
| { | |
| direction = Boolean( Math.round(Math.random()) ); | |
| } | |
| public function monsterMove():void | |
| { | |
| if (aggro_range > distance) | |
| { | |
| //Inside aggro range | |
| if ((player_X+35) < x) //Check Right | |
| { | |
| if (speed > distance) | |
| { | |
| x -= 1; | |
| } | |
| else | |
| { | |
| x -= speed; | |
| } | |
| } | |
| else if ((player_X-35) > x) //Check Left | |
| { | |
| if (speed > distance) | |
| { | |
| x += 1; | |
| } | |
| else | |
| { | |
| x += speed; | |
| } | |
| } | |
| } | |
| else | |
| { | |
| if (direction == true) | |
| { | |
| //Wander to the Right and not on edge of the map. | |
| if (x < (stage.stageWidth-10)) | |
| { | |
| x ++; | |
| } | |
| if (this.scaleX == 1) | |
| { | |
| this.scaleX = -1; | |
| x += this.width; | |
| } | |
| } | |
| else if (direction == false) | |
| { | |
| //Wander to the Left and not on edge of the map. | |
| if (x > 0) | |
| { | |
| x --; | |
| } | |
| if (this.scaleX == -1) | |
| { | |
| this.scaleX = 1; | |
| x -= this.width; | |
| } | |
| } | |
| } | |
| } | |
| public function onDeath():void | |
| { | |
| //Add the monster's XP value to player's XP | |
| stage.dispatchEvent(new MonsterEvent("onMonsterKill", xp_value)); | |
| } | |
| protected override function onAttack():void | |
| { | |
| //Dispatch an event to tell player that he has been hit. | |
| stage.dispatchEvent(new MonsterEvent("onPlayerHit", damage)); | |
| //Set Readyness to false so the monster will not attack again for a few seconds. | |
| Ready = false; | |
| //Restart the ticker. | |
| Attack_Ticker.start(); | |
| } | |
| public function update():void | |
| { | |
| //Update the distance between player and monster aswell as updating the value on player's location. | |
| player_X = Game.player_X; | |
| distance = Math.abs(player_X - x); | |
| //Check Monster's Health to determine if alive or dead. | |
| if (health <= 0) | |
| { | |
| onDeath(); | |
| } | |
| else | |
| { | |
| //Determine if you are in attack range. Also check the timer so there's no attack spamming or the player is screwed. | |
| if ((distance <= 15) && (Ready == true)) | |
| { | |
| onAttack(); | |
| Animation_State = 2; | |
| } | |
| else | |
| { | |
| monsterMove(); | |
| Animation_State = 1; | |
| } | |
| } | |
| //Animation state stuff. | |
| if (CurrentAnimation != null) | |
| { | |
| removeChild(CurrentAnimation); | |
| } | |
| if (Animation_State == 0) | |
| { | |
| CurrentAnimation = IdleAnimation; | |
| } | |
| else if (Animation_State == 1) | |
| { | |
| CurrentAnimation = MovingAnimation; | |
| } | |
| else if (Animation_State == 2) | |
| { | |
| CurrentAnimation = AttackingAnimation; | |
| } | |
| //Add to stage. | |
| addChild(CurrentAnimation); | |
| y = Main.STAGE_HEIGHT - (this.height+15); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.events.Event; | |
| /** | |
| * New event type to carry arguements/values with it on a nice piggyback ride. | |
| * Used to carry XP from monster to player. | |
| * Or any other kind of value for that matter, the awesomeness of wildcards. | |
| **/ | |
| public class MonsterEvent extends Event | |
| { | |
| public var arg:*; | |
| public function MonsterEvent(type:String, customValue:*=null, bubbles:Boolean=false, cancelable:Boolean=false) | |
| { | |
| super(type, bubbles, cancelable); | |
| this.arg = customValue; | |
| } | |
| public override function clone():Event | |
| { | |
| return new MonsterEvent(type, arg, bubbles, cancelable); | |
| } | |
| public override function toString():String | |
| { | |
| return formatToString("MonsterEvent", "type", "arg", "bubbles", "cancelable", "eventPhase"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.display.Graphics; | |
| import flash.display.MovieClip; | |
| import flash.events.Event; | |
| import flash.events.KeyboardEvent; | |
| import flash.events.TimerEvent; | |
| import flash.ui.Keyboard; | |
| import flash.utils.Timer; | |
| /** | |
| * Player class, extended from Entities base class and tailored to the player character so it's different than the zombies. | |
| * Differences from base function: | |
| * Increased health. | |
| * Score variable. | |
| * Improved attack. | |
| * Direction: True = Right, False = Left | |
| */ | |
| public class Player extends Entities | |
| { | |
| //Player stats. | |
| public var health:int = BASE_HEALTH + 70; | |
| public var max_health:int = 100 | |
| public var damage:int = BASE_DAMAGE + 5; | |
| public var experience:int; | |
| public var required_experience:int = 100; | |
| public var level:int = 1; | |
| private var speed:int = 2; | |
| //Controls | |
| private var leftDown:Boolean = false; | |
| private var rightDown:Boolean = false; | |
| private var spaceDown:Boolean = false; | |
| public var direction:Boolean = true; | |
| private var Attack_Ticker:Timer; | |
| private var Ready:Boolean = true; | |
| //Animation state: 0 Idle, 1 Running, 2 Firing, 3 Dead. | |
| public var Animation_State:int = 0; | |
| public var CurrentAnimation:MovieClip = null; | |
| private var IdleAnimation:MovieClip = new Player_Idle(); | |
| private var RunningAnimation:MovieClip = new Player_Run(); | |
| private var FiringAnimation:MovieClip = new Player_Fire(); | |
| private var DeathAnimation:MovieClip = new Player_Dead(); | |
| public function Player() | |
| { | |
| speed += level; | |
| experience = 0; | |
| x = Main.STAGE_WIDTH/2; | |
| Attack_Ticker = new Timer(500, 0); | |
| Attack_Ticker.addEventListener(TimerEvent.TIMER, Reset); | |
| super(); | |
| } | |
| private function Reset(e:Event):void | |
| { | |
| //Reset attack readyness to true. | |
| Ready = true; | |
| Attack_Ticker.reset(); | |
| } | |
| protected override function onInit():void | |
| { | |
| stage.addEventListener(KeyboardEvent.KEY_DOWN, doKeyDown); | |
| stage.addEventListener(KeyboardEvent.KEY_UP, doKeyUp); | |
| stage.addEventListener("onPlayerHit", onHit); | |
| stage.addEventListener("onMonsterKill", onKill) | |
| } | |
| private function onHit(e:MonsterEvent):void | |
| { | |
| //Player has been hit. Reduce his HP! | |
| health -= e.arg; | |
| if (health <=0) | |
| Animation_State = 3; | |
| } | |
| private function onKill(event:MonsterEvent):void | |
| { | |
| //Grab the XP value from the event. | |
| var Monster_XP:int = event.arg; | |
| experience += Monster_XP; | |
| //Check if the new experience value warants a levelup. | |
| if (experience >= required_experience) | |
| { | |
| onLevelUp(); | |
| } | |
| } | |
| //Keyboard controls | |
| private function doKeyDown(event:KeyboardEvent):void | |
| { | |
| if (event.keyCode == Keyboard.LEFT) | |
| { | |
| leftDown = true; | |
| direction = false; | |
| } | |
| else if (event.keyCode == Keyboard.RIGHT) | |
| { | |
| rightDown = true; | |
| direction = true; | |
| } | |
| if (event.keyCode == Keyboard.SPACE) | |
| { | |
| spaceDown = true; | |
| } | |
| } | |
| private function doKeyUp(event:KeyboardEvent):void | |
| { | |
| if (event.keyCode == Keyboard.LEFT) | |
| leftDown = false; | |
| if (event.keyCode == Keyboard.RIGHT) | |
| rightDown = false; | |
| if (event.keyCode == Keyboard.SPACE) | |
| spaceDown = false; | |
| } | |
| private function playerMove():void | |
| { | |
| if (rightDown == true) | |
| { | |
| if (x < (stage.stageWidth-10)) | |
| { | |
| //Move to the right | |
| x += speed; | |
| Animation_State = 1; | |
| } | |
| } | |
| else if (leftDown == true) | |
| { | |
| if (x > 0) | |
| { | |
| //Move to the left | |
| x -= speed; | |
| Animation_State = 1; | |
| } | |
| } | |
| } | |
| private function playerAttack ():void | |
| { | |
| //Check Readyness | |
| if ((Ready == true) && (spaceDown == true)) | |
| { | |
| //Fire a bullet and flip Readyness to false | |
| this.dispatchEvent(new Event("PlayerFired")); | |
| Ready = false; | |
| Attack_Ticker.start(); | |
| Animation_State = 2; | |
| } | |
| } | |
| public function onLevelUp():void | |
| { | |
| // Reduce experience by the number of required experience to level up, increase the Level by 1 and also increase the required experience for next level. | |
| experience = experience - required_experience; | |
| level ++; | |
| required_experience = ((required_experience +50) * level); | |
| speed = speed + (level/10); | |
| //Increase the player's stats and reset his or her health to full. | |
| max_health += 60; | |
| health = max_health; | |
| damage += 10; | |
| } | |
| public function update():void | |
| { | |
| //Check Direction and adjust the image accordinly. | |
| if((direction == true) && (this.scaleX != 1)) | |
| { | |
| this.scaleX = 1; | |
| x -= this.width; | |
| } | |
| else if ((direction == false) && (this.scaleX != -1)) | |
| { | |
| this.scaleX = -1; | |
| x += this.width; | |
| } | |
| //Move the player. | |
| playerMove(); | |
| playerAttack(); | |
| //Keep track of animation playing and change accordingly. | |
| AnimationUpdate(); | |
| if (health <= 0) | |
| this.dispatchEvent(new Event("Dead")); | |
| } | |
| private function AnimationUpdate():void | |
| { | |
| if (CurrentAnimation != null) | |
| { | |
| removeChild(CurrentAnimation); | |
| } | |
| if (Animation_State == 0) | |
| { | |
| CurrentAnimation = IdleAnimation; | |
| } | |
| else if (Animation_State == 1) | |
| { | |
| CurrentAnimation = RunningAnimation; | |
| } | |
| else if (Animation_State == 2) | |
| { | |
| CurrentAnimation = FiringAnimation; | |
| } | |
| else if (Animation_State == 3) | |
| { | |
| CurrentAnimation = DeathAnimation; | |
| } | |
| addChild(CurrentAnimation); | |
| y = Main.STAGE_HEIGHT - (this.height+15); | |
| if ((Animation_State == 2) && (CurrentAnimation.totalFrames == CurrentAnimation.currentFrame)) | |
| { | |
| Animation_State = 0; | |
| } | |
| else if (Animation_State != 2) | |
| { | |
| Animation_State = 0; | |
| } | |
| } | |
| protected override function onDraw():void | |
| { | |
| AnimationUpdate(); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added new class, still needs testing though!