package { // Public image on Flickr taken by nate254 copied on 2007-12-09 // http://www.flickr.com/photos/natespix/1410562812/ // Youtube video! // http://youtube.com/watch?v=zWUsnyZ6JwA&feature=related import com.whirled.FlowAwardedEvent; import com.whirled.WhirledGameControl; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.media.Sound; import flash.media.SoundTransform; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.utils.getTimer; [SWF(width="600", height="425")] public class DepthCharge extends Sprite { // Whirled properties /* Game Control */ protected var _control:WhirledGameControl; protected var _version:TextField = new TextField; // Game properties protected var _gameEndTime:Number = new Number(); protected var _t:Date; protected var _t_sonar:Date; protected var _gameBoard:Sprite = new Sprite(); protected var _gameBoardMask:Sprite = new Sprite(); //protected var _scoreBoardTimer:Timer = new Timer(500, 0); protected var _scoreBoardTime:TextField = new TextField(); protected var _scoreBoardScore:TextField = new TextField(); protected var _scoreBoardHits:TextField = new TextField(); protected var _debugText:TextField = new TextField(); protected var _button:Sprite = new Sprite(); protected var _score:Number = new Number(); protected var _subsSunk:Number = new Number(); protected var _shipHits:Number = new Number(); protected var _lastTime:int; protected var _bonusTime:Number = new Number(); // Keeps track of moving the ship protected var _moveShip:int; // Objects in play protected var _ptboat:Sprite = new Sprite(); //protected var _sub:Sprite = new Sprite(); protected var _mine:Sprite = new Sprite(); protected var _mines:Array = new Array(); //protected var _subs:Array = new Array(); protected var _sub_data:Array = new Array(); protected var _depthCharges:Array = new Array(6); protected var _charge_data:Array = new Array(); protected var _nsubs:Number = new Number(); // Sounds [Embed(source="sub_hit.mp3")] protected static const SUBHIT :Class; protected var _sub_hit :Sound; [Embed(source="ship_hit.mp3")] protected static const SHIPHIT :Class; protected var _ship_hit :Sound; [Embed(source="drop_charge2.mp3")] protected static const DROPCHG :Class; protected var _drop_charge :Sound; [Embed(source="sonar_blip.mp3")] protected static const SONAR :Class; protected var _sonar :Sound; // Sound transform protected var _transform_soft :SoundTransform; protected var _transform_loud :SoundTransform; // array.splice(start,numdel) deletes elements out of an array // array.push(obj) puts an object into the array public function DepthCharge() { // Preload sound files _sub_hit = (new SUBHIT() as Sound); _ship_hit = (new SHIPHIT() as Sound); _drop_charge = (new DROPCHG() as Sound); _sonar = (new SONAR() as Sound); _transform_soft = new SoundTransform(); _transform_soft.volume = 0.4; _transform_loud = new SoundTransform(); _transform_loud.volume = 3.0; // Main program starts here // Listener for an unload event root.loaderInfo.addEventListener(Event.UNLOAD, handleUnload); // Comment to create standalone version of game // Uncomment to create a Whirled compatible version _control = new WhirledGameControl(this); // Graphic reference // x : left/right // y : up/down // Version 1: // Time limit is about 2 minutes : 90 ticks (120 sec) // max 6 depth charges going at one time // ship can move left/right // mines move up to hit our ship // mines that hit our ship reduce score 50% // graveyard counts ships at bottom // max 4 subs at random depths/speeds // deep subs more points than shallow ones // +30 points for each sub sunk // Score 500 pts, add 45 ticks (60 sec) // Score board 0-34 // Ship 35-75 // Sea 75 to 400 //var gameBoard:Shape = new Shape(); _gameBoard.graphics.beginFill(0xFFFFFF); _gameBoard.graphics.drawRect(0,0,600,425); _gameBoard.graphics.beginFill(0xAADDEE) _gameBoard.graphics.drawRect(0,75,600,325); addChild(_gameBoard); // Create a _gameBoardMask area that is above // the subs on the right hand side. Playing // standalone this problem does not exist, but // within Whirled the play area is extended and // subs and stuff can be seen beyond the edge _gameBoardMask.graphics.beginFill(0xFFFFFF); _gameBoardMask.graphics.drawRect(600,0,100,425); // Add this child at the end var scoreBoardFrame:Shape = new Shape(); scoreBoardFrame.graphics.beginFill(0xEEEEEE); scoreBoardFrame.graphics.drawRect(0,0,600,35); addChild(scoreBoardFrame); // Draw thin line for scoreboard and surface/sea var gameBorders:Shape = new Shape(); gameBorders.graphics.lineStyle(1.0,0x000000); gameBorders.graphics.moveTo(0,34); gameBorders.graphics.lineTo(600,34); gameBorders.graphics.lineStyle(1.0,0x333333); gameBorders.graphics.moveTo(0,74); gameBorders.graphics.lineTo(600,74); // Bottom scoreboard gameBorders.graphics.moveTo(0,400); gameBorders.graphics.lineTo(600,400); addChild(gameBorders); // Scoreboard time var timeFormat:TextFormat = new TextFormat(); timeFormat.size = 24; _scoreBoardTime.autoSize = TextFieldAutoSize.LEFT; _scoreBoardTime.defaultTextFormat = timeFormat; _scoreBoardScore.defaultTextFormat = timeFormat; timeFormat.size = 15; _debugText.defaultTextFormat = timeFormat; _debugText.x = 0; _debugText.y = 375; _debugText.autoSize = TextFieldAutoSize.LEFT; addChild(_debugText); _scoreBoardTime.text = "0:00"; _scoreBoardScore.x = 520; _score = 0; _scoreBoardScore.text = padzero(_score.toString(), 5); addChild(_scoreBoardTime); //addChild(_scoreBoardScore); timeFormat.size = 15; timeFormat.bold = true; _scoreBoardHits.x = 0; _scoreBoardHits.y = 400; _scoreBoardHits.defaultTextFormat = timeFormat; _scoreBoardHits.autoSize = TextFieldAutoSize.LEFT; _scoreBoardHits.text = "You: 0 Them: 0"; addChild(_scoreBoardHits); // Show version info timeFormat.size = 6; _version.text = "0.23"; _version.defaultTextFormat = timeFormat; _version.autoSize = TextFieldAutoSize.LEFT; _version.x = 600 - _version.width; _version.y = 400 - _version.height; addChild(_version); // Draw Start Button drawButton(); //trace(stage.width); //trace(stage.height); // Draw ship // TODO: Ship needs to be tightly packed up against // the zero axis of x and y _ptboat.graphics.lineStyle(1.0,0x000000); _ptboat.graphics.moveTo(20,38); _ptboat.graphics.lineTo(80,38); _ptboat.graphics.lineTo(90,24); _ptboat.graphics.lineTo(76,24); _ptboat.graphics.lineTo(76,21); _ptboat.graphics.moveTo(76,24); _ptboat.graphics.lineTo(63,24); _ptboat.graphics.lineTo(63,20); _ptboat.graphics.lineTo(60,20); _ptboat.graphics.lineTo(60,22); _ptboat.graphics.lineTo(55,22); // main bridge _ptboat.graphics.lineTo(55,16); _ptboat.graphics.lineTo(45,16); _ptboat.graphics.lineTo(45,22); _ptboat.graphics.lineTo(40,22); _ptboat.graphics.lineTo(40,20); _ptboat.graphics.lineTo(37,20); _ptboat.graphics.lineTo(37,24); //_ptboat.graphics.lineTo(); _ptboat.graphics.lineTo(30,24); _ptboat.graphics.lineTo(30,21); _ptboat.graphics.lineTo(33,21); _ptboat.graphics.lineTo(27,21); _ptboat.graphics.moveTo(30,24); _ptboat.graphics.lineTo(10,24); _ptboat.graphics.lineTo(20,38); _ptboat.x = int(300 - (_ptboat.width / 2.0)); _ptboat.y = 35; addChild(_ptboat); // initialize depthcharges var i:int; for (i = 0; i < 6; i++) { var depthCharge:Shape = new Shape(); var xx:int = 0; var yy:int = 0; depthCharge.graphics.lineStyle(1.0,0x000000); depthCharge.graphics.moveTo(xx+0,yy-3); depthCharge.graphics.curveTo(xx+4,yy+1,xx+0,yy+6); depthCharge.graphics.curveTo(xx-4,yy+1,xx+0,yy-3); depthCharge.x = i * 20 + 260; depthCharge.y = 10; _depthCharges[i] = depthCharge; //sendLocalChat("i"+_depthCharges[i].x+","+_depthCharges[i].y); addChild(_depthCharges[i]); //_depthcharge.x = 200; //_depthcharge.y = 100; //addChild(_depthcharge); } //_depthCharges[3].y = 100; //for (i = 0; i < 10; i++) { // createSub(); //} // Start timer //trace(_t.getTime()); // Clock update every 500ms //_scoreBoardTimer.addEventListener(TimerEvent.TIMER, scoreBoardUpdate); //stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); //stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); //trace(_depthcharge.height); //trace(_depthcharge.width); // Add MASK addChild(_gameBoardMask); } /** MAIN GAME LOOP */ // This is fired as many times as possible protected function gameLoop(event :Event) :void { var timeElap:Number; // Compute time from last frame timeElap = (getTimer() - _lastTime) / 100; moveCharges(timeElap*2); moveSubs(timeElap*2); if (_moveShip) { moveShip(); } moveMines(timeElap*2); scoreBoardUpdate(); // If there are less than 4 subs on the board create one if (_sub_data.length < 4) { createSub(); } _lastTime = getTimer(); } /** END MAIN GAME LOOP */ // Move the ship if keys are held down private function moveShip() :void { if (_ptboat.x > 2 && _moveShip < 0) { _ptboat.x = _ptboat.x + _moveShip; } if (_ptboat.x < (580 - _ptboat.width) && _moveShip > 0) { _ptboat.x = _ptboat.x + _moveShip; } } // We are shooting! public function dropCharge(side :int) :void { var i:int; var slot:int = -1; // find available slot for (i = 0; i < 6; i++) { if (_depthCharges[i].y == 10) { slot = i; break; } } // We tried to drop a depth charge, but they are all // currently being used! if (slot == -1) { sendLocalChat("Hold yer skivvies Captain! Wait for more ammo!"); return; } _drop_charge.play(0,1,_transform_soft); //sendLocalChat("Firing depth charge #"+slot); var xx:int; _depthCharges[slot].x = _ptboat.x + (_ptboat.width * side) + 10; _depthCharges[slot].y = 75; } // Sub released a mine! public function launchMine(startx :int, starty :int) :void { var mine:Shape = new Shape(); // initialize mines mine.graphics.lineStyle(1.0,0x000000); mine.graphics.drawCircle(0,0,3.0); mine.graphics.drawCircle(0,0,0.5); // spines mine.graphics.drawCircle(0,-4,1.0); mine.graphics.drawCircle(0,4,1.0); mine.graphics.drawCircle(3,-3,1.0); mine.graphics.drawCircle(3,3,1.0); mine.graphics.drawCircle(-3,-3,1.0); mine.graphics.drawCircle(-3,3,1.0); mine.x = startx; mine.y = starty; addChild(mine); var mine_data:Object = new Object(); var speed:Number; speed = 2.0 * Math.random(); if (speed < 1.0) { speed = 1.0; } mine_data = {mine: mine, speed: speed}; _mines.push(mine_data); } public function createSub() :void { // create a sub at a random depth and random speed and direction // assign points based on depth/speed // play area is 75 to 400 // With flash, subs can move through each other var depth:int; var speed:int; var pts:int; var direc:int; var tries:int; var rnd:Number; tries = 0; // Try up to 10 times to place a new sub while (tries < 10) { tries = tries + 1; // Random depth depth = 100 + (250 * Math.random()); speed = 9 * Math.random(); // The first 5 subs cannot go top speed _nsubs = _nsubs + 1; if (_nsubs < 5) { if (speed>4) { speed = 4.0; } } if (speed < 1) { speed = 1; } rnd = Math.random() //trace(rnd); if (rnd>0.5) { direc = 1; } else { direc = -1; } pts = int((speed * 3) + ((depth - 100)/10) * 1.0); // Draw submarine // TODO : redraw so that sub is tightly packed up against // x side 0 and y top 0 // body var sub:Shape = new Shape(); //trace("NEW height:"+sub.height); //trace("NEW width:"+sub.width); //trace("stop here"); var sub_score:TextField = new TextField(); var sub_score_fmt:TextFormat = new TextFormat(); sub_score_fmt.bold = true; sub_score.defaultTextFormat = sub_score_fmt; sub_score.text = pts.toString(); sub.graphics.lineStyle(2.0,0x000000); sub.graphics.moveTo(10,20); sub.graphics.curveTo(3,29,10,38); sub.graphics.lineTo(90,38); sub.graphics.curveTo(97,29,90,20); sub.graphics.lineTo(40,20); sub.graphics.lineTo(40,10); sub.graphics.lineTo(30,10); sub.graphics.lineTo(30,20); sub.graphics.lineTo(10,20); // draw propeller sub.graphics.moveTo(95,29); sub.graphics.lineTo(99,29); sub.graphics.moveTo(99,26); sub.graphics.lineTo(99,33); // periscope sub.graphics.moveTo(33,10); sub.graphics.lineTo(33,6); sub.y = depth; if (direc == 1) { sub.x = -sub.width + 5; sub.scaleX = -1; sub_score.x = sub.x - sub.width + 52; sub_score.y = sub.y + 18; } else { sub.x = 594; // - sub.width; sub_score.x = sub.x + 28; sub_score.y = sub.y + 18; } // Check _subs array for overlap var occupied_spaces:int; var i:int; occupied_spaces = 0; for (i = 0; i < _sub_data.length; i++) { if (sub.hitTestObject(_sub_data[i].sub)) { occupied_spaces = occupied_spaces + 1; } } if (occupied_spaces == 0) { trace("Adding sub..."); addChildAt(sub, getChildIndex(_gameBoardMask)); addChildAt(sub_score, getChildIndex(_gameBoardMask)); //_subs.push(sub); // add sub to array for tracking var sub_data:Object = new Object(); var mine:int = 1; // Each sub only can fire one mine, likely to be fired right under the ship position sub_data = {sub: sub, subTxt: sub_score, speed: speed, depth: depth, pts: pts, mine: mine}; _sub_data.push(sub_data); //trace(sub_data.speed); //trace(sub_data.pts); //trace(_sub_data); // Sub width is 94.5 - it is hard coded below! //trace("Sub width:"+sub.width); //sendLocalChat("Sub width:"+sub.width); // Success return; } //trace(sub.width); //trace("Sub height:" + sub.height); // Overlapping object, try again } } public function drawButton() :void { var textLabel:TextField = new TextField(); _button.graphics.clear(); _button.graphics.beginFill(0x99DD99); // green _button.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH _button.graphics.endFill(); var txtFormat:TextFormat = new TextFormat(); txtFormat.size = 24; textLabel.text = "Start!"; textLabel.setTextFormat(txtFormat); textLabel.x = 10; textLabel.y = -5; textLabel.selectable = false; _button.addChild(textLabel) _button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownButtonHandler); _button.x = 510; _button.y = 5; addChild(_button); sendLocalChat("Version: "+_version.text); sendLocalChat("Game is loaded. Hit the 'Start!' button to begin."); //sendLocalChat("Please note score is handicapped 50% until the subs can return fire!"); //sendLocalChat("Game controls: j - move left, l - move right"); //sendLocalChat("m - drop depth charge left, . - drop depth charge right"); _sonar.play(0,1,_transform_soft); } public function mouseDownButtonHandler(event :MouseEvent) : void { startGame(); } public function startGame(): void { trace("Starting Game..."); _button.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownButtonHandler); // Are we connected to Whirled? if (_control && _control.isConnected()) { _control.registerListener(this); _control.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); _control.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); } else { stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); } removeChild(_button); addChild(_scoreBoardScore); _scoreBoardTime.text = "2:00"; _t = new Date(); _t_sonar = new Date(); _score = 0; _subsSunk = 0; _shipHits = 0; _moveShip = 0; _nsubs = 0; _gameEndTime = 120.9999; _bonusTime = 0; //_scoreBoardTimer.start(); // This is fired every chance it gets or 30 FPS _lastTime = getTimer(); _gameBoard.addEventListener(Event.ENTER_FRAME, gameLoop); //Puts a yellow boarder on things? //stage.focus = _gameBoard; } public function endGame(): void { _gameBoard.removeEventListener(Event.ENTER_FRAME, gameLoop); // Are we connected to Whirled if (_control && _control.isConnected()) { _control.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); _control.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler); } else { stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler); } // Award bonus for sunk ships if (_subsSunk > 0) { var bonus:Number; bonus = _subsSunk * 30; sendLocalChat("Score bonus: "+bonus); _score = _score + bonus; _scoreBoardScore.text = padzero(_score.toString(), 5); } // Award flow on final score if (_control && _control.isConnected()) { _control.addEventListener(FlowAwardedEvent.FLOW_AWARDED, flowAwarded); _control.endGameWithScores([ _control.getMyId() ] , [ int(_score) ], WhirledGameControl.TO_EACH_THEIR_OWN); } sendLocalChat("Game Over! Please go to the Lobby."); //_scoreBoardTimer.reset(); } /* * The game is over and flow has been awarded. */ public function flowAwarded (event :FlowAwardedEvent) :void { sendLocalChat("You recieved "+event.amount+" flow."); //control.localChat("[DEBUG] Performance Rate: "+event.percentile+"%"); //control.removeEventListener(FlowAwardedEvent.FLOW_AWARDED, flowAwarded); //_control.playerReady(); } // Sub movement based on the fractional time protected function moveSubs(timeElap :Number) :void { var i:int; var p:Number; var r:Number; var posdiff:Number; for (i = 0; i < _sub_data.length; i++) { _sub_data[i].sub.x = _sub_data[i].sub.x - (timeElap * _sub_data[i].sub.scaleX * _sub_data[i].speed); _sub_data[i].subTxt.x = _sub_data[i].subTxt.x - (timeElap * _sub_data[i].sub.scaleX * _sub_data[i].speed); //_sub_data[i].subTxt.text = _sub_data[i].sub.x.toString(); // Do we fire a mine? Limit subs to 6 mines as well. if (_mines.length < 6 && _sub_data[i].mine == 1) { // as we get near the ptboat, posdiff goes to 1 posdiff = (600 - Math.abs(_sub_data[i].sub.x - _ptboat.x)) / 600; p = Math.random() * posdiff; r = Math.random(); //sendLocalChat(p.toString()); var xpos:Number; xpos = _sub_data[i].sub.x+(_ptboat.width/2); if (_sub_data[i].sub.scaleX == -1) { xpos = _sub_data[i].sub.x; } if (p>0.9 || r > 0.999) { launchMine(xpos,_sub_data[i].sub.y); // don't let it shoot more than 1! _sub_data[i].mine = 0; } } // Shrink the sub on the right or it seems to fly off the board //if (_sub_data[i].sub.x > 600 && _sub_data[i].sub.scaleX == -1) //{ // _sub_data[i].sub.width = 94.5 - (_sub_data[i].sub.x - 600); // // Check to see if we intersected with depthcharges // Destroy subs that move off the playing area if ((_sub_data[i].sub.x < -_sub_data[i].sub.width) || (_sub_data[i].sub.x > 94.5 + 600)) { // Remove the child from the display removeChild(_sub_data[i].sub); removeChild(_sub_data[i].subTxt); // Destroy the data for the sub _sub_data.splice(i,1); } } //_debugText.text = _sub_data.length.toString(); } protected function moveMines(timeElap :Number) :void { var i:int; for (i = 0; i < _mines.length; i++) { _mines[i].mine.y = _mines[i].mine.y - (timeElap * _mines[i].speed * 0.6); // give the mines some X wobble _mines[i].mine.x = _mines[i].mine.x + ((Math.random() - Math.random()) * 1.5); _mines[i].mine.rotation = _mines[i].mine.rotation + (Math.random() * 2.0); // Test if we hit the ship, if so, score is cut by 50% if (_mines[i].mine.hitTestObject(_ptboat)) { sendLocalChat("You were struck by a mine!"); _ship_hit.play(); _mines[i].mine.visible = false; _mines[i].mine.y = 70; _shipHits = _shipHits + 1; // update score _score = int(_score * 0.5); _scoreBoardScore.text = padzero(_score.toString(), 5); } // If mine hits the surface and doesn't hit the ship, delete it if (_mines[i].mine.y < 75) { removeChild(_mines[i].mine); _mines.splice(i,1); } } } protected function moveCharges(timeElap :Number) :void { var i:int; for (i = 0; i < _depthCharges.length; i++) { // move only ones that are not on the ScoreBoard in the default position if (_depthCharges[i].y != 10) { _depthCharges[i].y = _depthCharges[i].y + (timeElap * 2.0); } // Check intersection of charges and subs // Logic: only match the first sub it matches var s:int; for (s = 0; s < _sub_data.length; s++) { // If our charge hit a sub, SCORE! if (_depthCharges[i].hitTestObject(_sub_data[s].sub)) { // Add it to our score! var add_pts:Number; add_pts = _sub_data[s].subTxt.text.valueOf(); _score = _score + add_pts; _subsSunk = _subsSunk + 1; _scoreBoardScore.text = padzero(_score.toString(), 5); _sub_hit.play(0,1,_transform_loud); // set the charge to be reset _depthCharges[i].y = 450; // remove sub we destroyed removeChild(_sub_data[s].sub); removeChild(_sub_data[s].subTxt); _sub_data.splice(s,1); // fall out of this for loop break; } } // if charge drops beyond the bottom, reset it if (_depthCharges[i].y > 400) { //sendLocalChat("Depth? "+_depthCharges[i].y); //sendLocalChat("Height? "+_depthCharges[i].height); _depthCharges[i].y = 10; _depthCharges[i].x = i * 20 + 260; } } } public function padzero(num :String, leng :int): String { var xx:String = new String(); if (num.length < leng) { xx = padzero("0" + num, leng); } else { xx = num; } return xx; } public function scoreBoardUpdate(): void { // update the scoreboard time var t_now:Date = new Date(); var t_diff:Number = new Number(); var min:Number = new Number(); var sec:Number = new Number(); t_diff = t_now.getTime() - _t.getTime(); t_diff = t_diff / 1000.0; trace(t_diff); if (t_diff > _gameEndTime) { if (_score > 500 && _gameEndTime < 131.0) { _gameEndTime = _gameEndTime + 60.0; _bonusTime = 1; // Exit before the actually exiting the game return; } endGame(); trace("Game ends"); } // Get minutes and seconds for display t_diff = _gameEndTime - t_diff; min = int(t_diff / 60.0); sec = int(t_diff - (min * 60.0)); if (_bonusTime) { _scoreBoardTime.text = padzero(min.toString(),1)+":"+padzero(sec.toString(),2) + " BONUS TIME"; } else { _scoreBoardTime.text = padzero(min.toString(),1)+":"+padzero(sec.toString(),2); } _scoreBoardHits.text = "You: "+_subsSunk.toString()+" Them: "+_shipHits.toString(); //_mine.rotation = _mine.rotation + 5; // Do a sonar blip every so often if (t_now.getTime() - _t_sonar.getTime() > 5000.0) { _sonar.play(0,1,_transform_soft); _t_sonar = new Date(); } } // Starts ship movement and allows depth charges to be dropped private function keyDownHandler(event :KeyboardEvent):void { //trace("keyDownHandler: " + event.keyCode); //trace("ctrlKey: " + event.ctrlKey); //trace("keyLocation: " + event.keyLocation); //trace("shiftKey: " + event.shiftKey); //trace("altKey: " + event.altKey); //_debugText.text = event.keyCode.toString(); //sendLocalChat("keyDown:"+event.keyCode); // If the scoreBoardTimer is not running, the game isn't // running - ignore keystrokes //if (! _scoreBoardTimer.running) if (! _gameBoard.hasEventListener(Event.ENTER_FRAME)) { return; } // Move left (j:74 or a:65 or <-:37) if (event.keyCode == 74 || event.keyCode == 65 || event.keyCode == 37) { // Only set if currently not moving if (_moveShip == 0) { _moveShip = -1; } } // Move right(l:76 or d:68 or ->:39) if (event.keyCode == 76 || event.keyCode == 68 || event.keyCode == 39) { // Only set if currently not moving if (_moveShip == 0) { _moveShip = 1; } } // Fire left (z:90, m:77, u:85 or q:81) if (event.keyCode == 90 || event.keyCode == 77 || event.keyCode == 85 || event.keyCode == 81) { dropCharge(0); } // Fire right (c:67, .:190, e:69 or o:79) if (event.keyCode == 67 || event.keyCode == 190 || event.keyCode == 69 || event.keyCode == 79) { dropCharge(1); } } // Use this to stop ship movement private function keyUpHandler(event :KeyboardEvent):void { //sendLocalChat("keyUp: " + event.keyCode); // STOP: Move left (j:74 or a:65 or <-:37) if (event.keyCode == 74 || event.keyCode == 65 || event.keyCode == 37) { // Only unset if currently moving if (_moveShip == -1) { _moveShip = 0; } } // STOP: Move right(l:76 or d:68 or ->:39) if (event.keyCode == 76 || event.keyCode == 68 || event.keyCode == 39) { // Only unset if currently moving if (_moveShip == 1) { _moveShip = 0; } } } /* This is called when your game is unloaded. */ protected function handleUnload (event :Event) :void { _gameBoard.removeEventListener(Event.ENTER_FRAME, gameLoop); if (_control && _control.isConnected()) { _control.unregisterListener(this); _control.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); _control.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); } else { stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler); } } protected function sendLocalChat(msg :String) :void { if (_control && _control.isConnected()) { _control.localChat(msg); } else { _debugText.text = msg; } } } }