package{ //import packages needed for the game import flash.display.*; import flash.events.*; import flash.geom.*; import flash.ui.*; //3rd party packages import Key; //import src.enemies.*; //this class represents the flash root, so it must extend the movieclip type public class gameMain extends MovieClip { /* constants used in the game go here */ // new public const gravity:Number = 1; public const friction:Number = .8; // /new const speed:Number = 5; const jump:Number = 20; /* game variables */ public var playerVelocity:Object; public var onGround:Boolean; // new public var enemies:Array; public var bullets:Array; // /new /* objects used in the game */ public var player:MovieClip; public var gameStage:MovieClip; //runs first public function gameMain():void { //initialize any game objects //new player player = new tinyMan(); //new stage gameStage = new stage1(); //new player velocity playerVelocity = new Object(); playerVelocity.x = 0; playerVelocity.y = 0; //misc onGround = false; Key.initialize( stage ); // new //center the window on the player this.x += ( stage.stageWidth / 2 ); // "fudged" so the player can fall on the stage this.y += stage.stageHeight / 2; //create 4 enemies enemies = new Array(); for( var i=0; i < 4; i++ ) { //create a new enemy var newEnemy = new nogoodMan(); //place him in his own area on the stage newEnemy.x = ( i * 230 ) - 150; //add to the stage this.addChild( newEnemy ); //push that enemy into an array so we can reference it later enemies.push( newEnemy ); } //new bullet array bullets = new Array(); // /new //add objects to the stage as needed this.addChild( gameStage ); this.addChild( player ); //start game loop this.addEventListener( 'enterFrame', onEnterFrame ); } //the code within this function will run once every frame public function onEnterFrame( e:Event ):void { updateInput(); // new updateBullets(); updateEnemies(); updatePlayer(); // /new } // new public function updatePlayer():void { //update velocity playerVelocity.y += gravity; playerVelocity.x *= friction; //test to see if the player is on the ground if( gameStage.hitTestPoint(player.x, player.y, true) && playerVelocity.y > 0) { //if it has hit the ground, halt gravity playerVelocity.y = 0; //set on ground to true onGround = true; } else { //hes not on the ground, let the game know this. onGround = false; } //update position player.x += playerVelocity.x; player.y += playerVelocity.y; // new //update stage x & y this.x -= playerVelocity.x; this.y -= playerVelocity.y; // /new } public function updateBullets():void { //loop through every bullet for( var i in bullets ) { //update its position in space bullets[i].x += bullets[i].velocity.x; bullets[i].y += bullets[i].velocity.y; //for each bullet, check to see if it has hit each enemy for( var m in enemies ) { if( bullets[i].hitTestObject( enemies[m] ) ) { //it is has, remove the bullet from the stage this.removeChild( bullets[i] ); //and remove the bullet from the bullets array bullets.splice( i, 1 ); //do the ame for the hit enemy this.removeChild( enemies[m] ); enemies.splice( m, 1 ); } } } } public function updateEnemies():void { //loop through and call their individual update functions for( var i in enemies ) { enemies[i].update(); } } public function fireBullet():void { //push a new bullet into out bullet array var newBullet = new bullet( ); bullets.push( newBullet ); //place that bullet at about the player's center newBullet.x = player.x; newBullet.y = player.y - player.height; //give that bullet a velocity so it moves forward newBullet.velocity.x = 30; //add it to the stage this.addChild( newBullet ); //check to see if there are too many bullets onstage if( bullets.length > 8 ) { //if so, remove the oldest bullet this.removeChild( bullets[0] ); //remove it from the bullets array as well bullets.shift(); } } // /new //handle input public function updateInput():void { if( Key.isDown( Keyboard.LEFT ) ) { playerVelocity.x = -speed; } if( Key.isDown( Keyboard.RIGHT ) ) { playerVelocity.x = speed; } if( Key.isDown( Keyboard.UP ) ) { if( onGround ) { onGround = false; playerVelocity.y = -jump; } } // new if( Key.isDown( Keyboard.SPACE ) ) { //shoot! fireBullet(); } // /new } } }