Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.MovieClip;
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- import com.game.Ship;
- import com.game.Bullet;
- import com.game.Asteroid;
- import flash.text.TextField;
- import flash.text.TextFormat;
- public class Main extends MovieClip
- {
- // score and character stuff
- var playerScore:Number = 0;
- var score:TextField = new TextField();
- var scoreformat:TextFormat = new TextFormat();
- // asteroid stuff
- //how much time before another enemy is made
- var AsteroidTime:int = 0;
- //how much time needed to make an enemy
- //it should be more than the shooting rate
- //or else killing all of the enemies would
- //be impossible :O
- var AsteroidLimit:int = 16;
- // movement stuff
- public var isLeft:Boolean;
- public var isRight:Boolean;
- public var isUp:Boolean;
- public var isDown:Boolean;
- // bullet stuff
- //how much time before allowed to shoot again
- var cTime:int = 0;
- //the time it has to reach in order to be allowed to shoot (in frames)
- var cLimit:int = 12;
- //whether or not the user is allowed to shoot
- var shootAllow:Boolean = true;
- private var _ship:Ship;
- public function Main()
- {
- //this movieclip will hold all of the bullets
- var BulletContainer:MovieClip = new MovieClip();
- addChild(BulletContainer);
- // constructor code
- trace("hello");
- stage.addEventListener(KeyboardEvent.KEY_DOWN, downHandler);
- stage.addEventListener(KeyboardEvent.KEY_UP, upHandler);
- addEventListener(Event.ENTER_FRAME, update);
- createShip();
- createScoreField();
- }
- private function createScoreField()
- {
- score.text = "Score: " + playerScore;
- score.x = 20;
- score.y = 800;
- scoreformat.color = 0x00FF00;
- scoreformat.size = 40;
- score.setTextFormat( scoreformat );
- stage.addChild(score);
- }
- private function createShip()
- {
- _ship = new Ship();
- _ship.setUp(350, 800, this);
- addChild(_ship);
- }
- private function update(e:Event)
- {
- if (cTime < cLimit)
- {
- cTime++;
- }
- else
- {
- shootAllow = true;
- cTime = 0;
- }
- //adding enemies to stage
- if (AsteroidTime < AsteroidLimit)
- {
- //if time hasn't reached the limit, then just increment
- AsteroidTime++;
- }
- else
- {
- //defining a variable which will hold the new enemy
- var newAsteroid = new Asteroid();
- //making the enemy offstage when it is created
- newAsteroid.y = -1 * newAsteroid.height;
- //making the enemy's x coordinates random
- //the "int" function will act the same as Math.floor but a bit faster
- newAsteroid.x = int(Math.random()*(stage.stageWidth - newAsteroid.width));
- //then add the enemy to stage
- addChild(newAsteroid);
- //and reset the enemyTime
- AsteroidTime = 0;
- }
- }
- private function upHandler(e:KeyboardEvent)
- {
- if (e.keyCode == 37)
- {
- isLeft = false;
- }
- else if (e.keyCode == 39)
- {
- isRight = false;
- }
- else if (e.keyCode == 38)
- {
- isUp = false;
- }
- else if (e.keyCode == 40)
- {
- isDown = false;
- }
- }
- private function downHandler(e:KeyboardEvent)
- {
- if (e.keyCode == 37)
- {
- isLeft = true;
- }
- else if (e.keyCode == 39)
- {
- isRight = true;
- }
- else if (e.keyCode == 38)
- {
- isUp = true;
- }
- else if (e.keyCode == 40)
- {
- isDown = true;
- }
- else if (e.keyCode == 32 && shootAllow)
- {
- //making it so the user can't shoot for a bit
- shootAllow = false;
- //declaring a variable to be a new Bullet
- var newBullet:Bullet = new Bullet();
- //changing the bullet's coordinates
- newBullet.x = _ship.x + 17 - newBullet.width / 2;
- newBullet.y = _ship.y;
- //then we add the bullet to stage
- BulletContainer.addChild(newBullet);
- }
- }
- }// end of class
- }// end of package
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement