Monday 1 February 2010

Stage 2: Meeting the design brief.

So now it was time to code the dam thing stage by stage.

The setup:

I decided to find out what packages were and how to use them.  When using them i found my code to be cleaner and more simplified as each of my .AS files started with the same code. quoted from Adobe's website packages simply "Allow you to bundle class definitions together in a way that facilitates code sharing and minimizes naming conflicts."
package
{
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.ui.Mouse;
    import flash.media.Sound;
    import flash.media.SoundChannel;


    The imports are to tell flash certain facilities. E.g i have a sound effect playing upon HitObject, simple enough to code but with packages you have to tell flash that you want sound to be imported otherwise it will not work.

   
   
   
    public class Asteroid extends MovieClip
    {
        public var army:Array;
        public var enemy:Enemy;
        public var ship:Ship;
        public var gameTimer:Timer;
        public var snd:Sound = new Die();
        public var my_date:Date = new Date();




Here are my variables they don't differ from declaring variables without packages apart from the word public.
Packages can have declared variables in either a "private" or a "public" function. I used public functions because i want my variables to be accessed from anywhere inside my game, using a private function would only make to available to the class i am using it in.



Asteroid game in which the user controls a spaceship to a.) avoid the asteroids or b.) shoot them.

 I decided on choice A. as i felt it would be easier to code. To make random enemies appear on the stage required a timer function. In writing every certain milliseconds create an instance of Enemy on the stage. These of which the player would have to navigate around to avoid game over.


public function onTick( timerEvent:TimerEvent ):void
        {
            if ( Math.random() < 0.1 )
            {
                var randomX:Number = Math.random() * 400;
                var newEnemy:Enemy = new Enemy( randomX, -15 );
                army.push( newEnemy );
                addChild( newEnemy );
                GameScore.addToValue(1);
            }
            ship.x = mouseX;
            ship.y = mouseY;
           
            for each ( var enemy:Enemy in army )
            {
                enemy.moveDownABit();
                if ( ship.hitTestObject( enemy ) ) 




The math.random crates an enemy at a random point on the stage, but what does the army bit do? Army is obviously not an inbuilt flash function but it is however an array. So instead of having code for each asteroid i wish to appear, i put the value for the instance of the asteroid inside an array like so...



public var army:Array;





Have a 2d top down view of the game.

This was simple and required the design to create this field of view. The way in which my game has been designed makes it look as if the ship is moving through an asteroid field. At this point i also decided weather to use mouse control or keyboard control. I tried both methods out with code for each ans decided that the mouse movement made for a more addictive fast past style game play, exactly what i hoped to achieve.


ship = new Ship();
            addChild( ship );
            ship.x = mouseX;
            ship.y = mouseY;



Create a scoring system or timer based on the users progress.

I found this to be quite difficult, so to make thing easier for myself and if i messed the code up i could see easily where it had gone wrong i created new action-script files (.as) for my scoring system and counter. I also then decided to do this for the players ship and the Asteroids(Enemy's) . The irony was that i had now made my game fail to run, because it was looking for code that wasn't there. I used the internet to search for a solution and worked my way through following what seemed to be the general layout for a flash project which was indeed to create a flash project folder, Have one main .FLA file and the code all on separate in .AS files under a "classes" folder.

For the scoring system itself dynamic text was required so that the code could as default set its value to 0 and for every second the player survives add 1 to the score.

public function addToValue( amountToAdd:Number ):void
        {
            currentValue = currentValue + amountToAdd;
            updateDisplay();

        }

        public function reset():void
        {
            currentValue= 0;
            updateDisplay();

        }


External influence:


This was the hardest part. Till now i hadnt gave much thoguht about what data i wanted to include or even what i wanted to change.  Eventually i decided that i could change the speed in which the asteroids fell depending on the time of day on the users computer.
trace(my_date.hours);
            if(my_date.hours<7||my_date.hours>21) {
gameTimer = new Timer(15 );
} else {
gameTimer = new Timer( 8  );
}
            Simple. Ok not very original but at least i had got it to work!


 Errors:

Through development i didn't encounter too many errors, i think this was largely due to the use of packages, i didn't get name conflicts. However the speed and amount of enemies that were falling created chaos when coded individually this led me to putting the code into an array.



I also wondered why my sound effect wasn't working until i realised that flash is stupid and i was being stupid because i hadn't included the "import" function at the top of my .as file.



Lastly:
I realised that the mouse was appearing over the movieclip of the players ship so i simply needed to hide the mouse cursor during gameplay like so..
import flash.ui.Mouse;
            Mouse.hide();

I thought it was also a good idea to have it show again upon the game over message showing.

Mouse.show();












No comments:

Post a Comment