Wednesday, June 1, 2011

Reflection On Hens, Roosters and Eggs

Finally, a night of sleep without the dark sceptre of Foghorn Dark Fowl looming in the recesses of my sleep deprived mind.  Woo hoo!  Now, for the summary...

After focusing so intently on Action Script the last couple of weeks, symbols and tweens feel like topics from the distant past, which is okay since they don't require the mind numbing amount of logic analysis that code demands.  (I did have to test myself to make sure I remembered how to do a tween.)

Thinking the game was due today, I put my game to rest last night, and once having done so cannot even remotely entertain the prospect of a "fowl" game resurrection.  Given that, I am now resisting the urge to just add those extra roosters, dang it, or similarly, to modify the makehens/foxeatshens functions code to make it more concise, or to add in text, or music, or speed up the fox, or make the bug motion more erratic, or make the bugs avoid the hens and roosters, or make the worms only crawl through the ground area, or, or, or....you get the idea.

I approached this project with dust all over the geeky, engineering-trained part of my brain.  This was a mistake is one way, a blessing in another.  The blessing came in the game's evolution and the free symbol creation and what those symbols eventually came to inspire.  The curse was the resulting fuzzy form and unfocused logic that I had going into the game coding part of the project. That and the fact that I didn't know the code language well at all contributed to a more frustrating experience than I should have had.  Once I had my unclear picture of the game, I should have taken the time to really delve into the logical game progression, complete with flow charting so any oddities in loops or conditions could be analysed before getting lost in the code.  While my fuzzy approach did work out okay, I'm sure it took me longer than if I had been more organized.  For the next game:  creatively brainstorm during the flash file phase, THEN logically analyse and organise BEFORE starting the action script!   (But always be open to new ideas.)

Important concepts learned during this project include variables as one item storage containers, global vs local variables, arrays as several item storage containers, manipulation of variables through the use of functions, calling functions, employing loops and counters and using conditional statements.  The strategy of where to place certain code based on your desired outcome is likely the most important concept reviewed.  So, until the next project...have a great summer and sabbatical.  (I'll be animating!)

game:  hen and rooster pair team up to eat bugs and avoid fox.  if all bugs are eaten, the hen lays an enormous egg and the pair gets 100 points.  if the fox catches the hen, she becomes his dinner.  there are three hens.

Tuesday, May 31, 2011

Hens & Roosters Code

//Hens & Roosters


package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.ui.Mouse;
    import flash.media.Sound;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
   
    public class FirstGame02 extends MovieClip
    {
        //Declare Global Variables
        var hennypenny:MovieClip;
        var lusciouslulu:MovieClip;
        var chickiechacha:MovieClip;
        var seymour:MovieClip;
        var henrythe8th:MovieClip;
        var madmax:MovieClip;
        var hatching:MovieClip;
        var fox:MovieClip;
        var fatassworm:MovieClip;
        var effinfly:MovieClip;
        var beetlebug:MovieClip;
        var flowers:MovieClip;
        var foreground:MovieClip;
        var sky:MovieClip;
        var vx:Number;
        var vy:Number;
        var tempHen = MovieClip;
        var myHenArray = new Array;
        var tempRooster = MovieClip;
        var myRoosterArray = new Array;
        var tempBug = MovieClip;
        var myBugArray:Array = new Array;
        var myBugArrayWorm:Array = new Array;
        var myBugArrayFly:Array = new Array;
        var myBugArrayBeetle:Array = new Array;
        var hensclucking:Sound;
        var roostercall:Sound;
        var henlaying:Sound;
       
        public function FirstGame02()//CONSTRUCTOR FUNCTION
        {
            //Instantiate Variables
            hennypenny = new HennyPenny;
            lusciouslulu = new LusciousLulu;
            chickiechacha = new ChickieChaCha;
            seymour = new Seymour;
            henrythe8th = new HenryThe8th;
            madmax = new MadMax;
            hatching = new Hatching;
            fox = new Fox;
            fatassworm = new FatAssWorm;
            effinfly = new EffinFly;
            beetlebug = new BeetleBug;
            flowers = new Flowers;
            foreground = new Foreground;
            sky = new Sky;
            vx = 20;
            vy = 20;
            hensclucking = new HensClucking;
            roostercall = new RoosterCall;
            henlaying = new HenLaying;
           
           
           
            //Hide the mouse
            Mouse.hide();
           
           
            //Set up the stage
            stage.scaleMode = StageScaleMode.EXACT_FIT;
            addChild(sky);
            addChild(foreground);
            addChild(flowers);
            addChild(fox);
           
            sky.x = stage.stageWidth * .45;
            sky.y = stage.stageHeight * .385;
            foreground.x = stage.stageWidth * .4;
            foreground.y = stage.stageHeight * .45;
            flowers.x = stage.stageWidth * .7;
            flowers.y = stage.stageHeight * .4;
            hatching.x = stage.stageWidth * .5;
            hatching.y = stage.stageHeight * .5;
            fox.x = stage.stageWidth*.5;
            fox.y = stage.stageHeight*.1;
            fox.scaleX = .20;
            fox.scaleY = .20;
            makeBugs();
            makeHens();
            makeRoosters();
            // Event Listeners
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
           
        }//end public function
       
       
           
       
        // Event Handlers
        function onEnterFrame(event:Event):void
        {
            // PLAYER Hen and Rooster CONTROLLED BY MOUSE
           
            hennypenny.x = mouseX;
            lusciouslulu.x = mouseX;
            chickiechacha.x = mouseX;
            tempRooster.y = mouseY;
           
            //Stage Boundary Collisions
            if(fox.x > stage.stageWidth || fox.x < 0)
            {
                vx = -vx;
                hensclucking.play();
            }
            if(fox.y > stage.stageHeight || fox.y < 0)
            {
                vy = -vy;
                hensclucking.play();
            }
           
            //check for rooster/fox collisions
            if(tempRooster.hitTestObject(fox))
            {
                vx = -vx;
                vy = -vy;
                roostercall.play();
            }

            //move stuff
            fox.x += vx;
            fox.y += vy;
           
            moveBugs();
            eatBugs();
            foxEatsHens();
                       
        }// end onEnterFrame function
       
        function makeBugs():void
        {
            for (var i:int = 30; i>=20; i--)
            {
                tempBug = new FatAssWorm;
                tempBug.y = (Math.random()*stage.stageHeight);
                tempBug.x = (Math.random()*stage.stageWidth);
                tempBug.scaleX = .1;
                tempBug.scaleY = .1;
                tempBug.vx = (Math.random()* 15);
                tempBug.vy = (Math.random()* 15);
                addChild(tempBug);// display the contents of tempBug on the stage
                myBugArray.push(tempBug);//puts the contents of each tempBug into myBug Array
               
            }//end for
           
            for (var j:int = 20; j>=10; j--)
            {
                tempBug = new EffinFly;
                tempBug.y = (Math.random()*stage.stageHeight);
                tempBug.x = (Math.random()*stage.stageWidth);
                tempBug.scaleX = .1;
                tempBug.scaleY = .1;
                tempBug.vx = (Math.random()* 15);
                tempBug.vy = (Math.random()* 30);
                addChild(tempBug);// display the contents of tempBug on the stage
                myBugArray.push(tempBug);//puts the contents of each tempBug into myBugArray
               
            }//end for
           
            for (var k:int = 10; k>=0; k--)
            {
                tempBug = new BeetleBug;
                tempBug.y = Math.floor(Math.random()*stage.stageHeight);
                tempBug.x = Math.floor(Math.random()*stage.stageWidth);
                tempBug.scaleX = .1;
                tempBug.scaleY = .1;
                tempBug.vx = (Math.random()* 10);
                tempBug.vy = (Math.random()* 20);
                addChild(tempBug);// display the contents of tempBug on the stage
                myBugArray.push(tempBug);//puts each tempBug into myBugArray
               
            }//end for
           
        }//end makeBugs
       
        function makeHens(): void
        {
            myHenArray.push(hennypenny);
            myHenArray.push(lusciouslulu);
            myHenArray.push(chickiechacha);
           
           
            var Hen:MovieClip;
           
            Hen = myHenArray[myHenArray.length - 1];
            Hen.y = stage.stageHeight*.85;
            Hen.scaleX = .3;
            Hen.scaleY = .3;
            tempHen = Hen;
            addChild(tempHen);
           
        }//end makeHens
       
       
        function foxEatsHens (): void
        {
            for (var i:int = myHenArray.length-1; i>0; i--)
            {
                if((fox).hitTestObject(myHenArray[i]))
                {
                    vx = -vx;
                    vy = -vy;
                    removeChild(myHenArray[i]);
                    myHenArray.splice(i,1);
                    myHenArray[i - 1].x = stage.stageWidth - fox.x;
                    myHenArray[i - 1].y = stage.stageHeight*.85;
                    myHenArray[i - 1].scaleX = .3;
                    myHenArray[i - 1].scaleY = .3;
                    //trace(myHenArray.length);
                    addChild(myHenArray[i - 1]);
                    //trace(myHenArray[0]);
                    hensclucking.play();
               
                }//end hen/fox collision test
               
            }//end for
           
                if (myHenArray.length == 1)
                {
                    if((fox).hitTestObject(myHenArray[0]))
                    {
                        removeChild(myHenArray[myHenArray.length - 1]);
                        myHenArray.splice(myHenArray.length-1,1);
                        removeChild(tempRooster);
                        trace("GAME OVER");
                    }
                   
                }
               
                   
        }//end foxEatsHens
       
           
        function makeRoosters(): void
        {
           
            myRoosterArray.push(seymour);//puts hen into myHenArray
            myRoosterArray.push(henrythe8th);
            myRoosterArray.push(madmax);
           
            var Rooster:MovieClip;
           
                Rooster = myRoosterArray[myRoosterArray.length - 1];
                Rooster.x = stage.stageWidth*.15;
                Rooster.scaleX = .3;
                Rooster.scaleY = .3;
                addChild(Rooster);
                tempRooster = Rooster;
               
        }//end makeRoosters
       
       
        function moveBugs(): void
        {
            var bug:MovieClip;
           
            for (var i:int = myBugArray.length-1; i>=0; i--)
            {
                bug = myBugArray[i];
                bug.x += tempBug.vx;
                bug.y += tempBug.vy;
           
                if (bug.x  > (stage.stageWidth - (bug.width/2)))
                {
                    bug.x = stage.stageWidth - (bug.width/2+1);
                    tempBug.vx = - tempBug.vx;
                }
                if (bug.x < (0 + (bug.width/2+1)))
                {
                    bug.x = 0 + (bug.width/2+1);
                    tempBug.vx = - tempBug.vx;
                }
                if (bug.y  > (stage.stageHeight - (bug.height/2)))
                {
                    bug.y = stage.stageHeight - (bug.height/2);
                    tempBug.vy = -tempBug.vy;
                }
                if (bug.y < (0 + (bug.height/2+1)))
                {
                    bug.y = 0 + (bug.height/2+1);
                    tempBug.vy = - tempBug.vy;
                }
            }//end for
           
        }//end moveBugs
       
        function eatBugs(): void
       
        {
            //var sound:Sound = new SoundSubmarine();
            var bug:MovieClip;// = event.target as MovieClip;
            for (var i:int = myBugArray.length-1; i>=0; i--)
            {
                bug = myBugArray[i];
                if (myHenArray[myHenArray.length - 1].hitTestObject(bug) || tempRooster.hitTestObject(bug))
                {

                    trace("YUMMY!!!");
                    //hensclucking.play();
                    removeChild(myBugArray[i]);
                    myBugArray.splice(i,1);
                }
               
                if (myBugArray.length == 0)           //score and add egg hatching
                {
                    //play sound
                    addChild(hatching);
                    trace("YOU HATCHED AN EGG!!!  SCORE:");
                    trace(100*(i+1));
                    henlaying.play();
                    henlaying.play();
                    henlaying.play();
                    //removeChild(hatching);
                    //makeBugs();
               
                } //end score/egg if
               
            }//end for
           
        }// end eatBugs
       
    }// end class
   
} // end package
      

A Fowl

In the interest of maintaining my sanity, the rest of my hair that hasn't been clenched out it bouts of frustration, and to prevent the dangerous lobbing of laptops, my game has been declared finished.  The accompanying reflective essay will come tomorrow.

https://sites.google.com/a/sou.edu/dmf203temple/swftemple/FirstGame02.swf?attredirects=0

Hens & Roosters...and an Egg

https://sites.google.com/a/sou.edu/dmf203temple/swftemple/FirstGame02.swf?attredirects=0