package { import flash.display.*; import flash.events.*; import flash.text.TextField; public class BalloonPop extends MovieClip { // display objects private var balloons:Array; private var cannonball:Cannonball; private var cannonballDX, cannonballDY:Number; // keys private var leftArrow, rightArrow:Boolean; // game properties private var shotsUsed:int; private var speed:Number; private var gameLevel:int; private const gravity:Number = .05; public function startBalloonPop() { gameLevel = 1; shotsUsed = 0; speed = 6; gotoAndStop("level1"); } public function startLevel() { showGameScore(); // create object arrays findBalloons(); // listen for keyboard stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction); stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction); // look for collisions addEventListener(Event.ENTER_FRAME,gameEvents); } public function findBalloons() { balloons = new Array(); // loop through all display objects for(var i:int=0;i -20) newRotation = -20; // reposition cannon.rotation = newRotation; } public function moveCannonball() { // only move the cannonball if it exists if (cannonball != null) { // change position cannonball.x += cannonballDX; cannonball.y += cannonballDY; // add pull of gravity cannonballDY += gravity; // see if the ball hit the ground if (cannonball.y > 340) { removeChild(cannonball); cannonball = null; } } } // check for collisions public function checkForHits() { if (cannonball != null) { // loop through all balloons for (var i:int=balloons.length-1;i>=0;i--) { // see if it is touching the cannonball if (cannonball.hitTestObject(balloons[i])) { balloons[i].gotoAndPlay("explode"); break; } } } } // key pressed public function keyDownFunction(event:KeyboardEvent) { if (event.keyCode == 37) { leftArrow = true; } else if (event.keyCode == 39) { rightArrow = true; } else if (event.keyCode == 32) { fireCannon(); } } // key lifted public function keyUpFunction(event:KeyboardEvent) { if (event.keyCode == 37) { leftArrow = false; } else if (event.keyCode == 39) { rightArrow = false; } } // cannonball launched public function fireCannon() { if (cannonball != null) return; shotsUsed++; showGameScore(); // create cannonball cannonball = new Cannonball(); cannonball.x = cannon.x; cannonball.y = cannon.y; addChild(cannonball); // move cannon and base above ball addChild(cannon); addChild(cannonbase); // set direction for cannonball cannonballDX = speed*Math.cos(2*Math.PI*cannon.rotation/360); cannonballDY = speed*Math.sin(2*Math.PI*cannon.rotation/360); } // balloons call back to here to get removed public function balloonDone(thisBalloon:MovieClip) { // remove from screen removeChild(thisBalloon); // find in array and remove for(var i:int=0;i