1 Reply Latest reply on Jul 22, 2009 6:14 PM by Thali-Hoo

    Critique my code & .swf - comments welcome

    keithwoods Level 1

      Hi there!

       

      After 2 months when I first posted on this forum, which was followed by a period of intensively learning Flash and AS3, I've finally completed the project I set out to do.

       

      If any of you would be kind enough to have a skim over my code and comment on how I could make it more efficient or add any little nip, tucks and tricks would be greatly apprecited.  Rather than give you a long winded, boring explanation of the final .swf, you can take a look at it here (www.keithwoods.co.uk) and see it yourself....although it is a little abstract so be open-minded!

       

      Here's my code...any comments welcome.....

       

      Document Class:
      package
      {
          // import necessary classes
          import flash.display.MovieClip;
          import flash.display.DisplayObject;
          import flash.events.MouseEvent;
          import flash.events.Event
          import fl.transitions.Tween;
          import fl.transitions.easing.*;
          import fl.transitions.TweenEvent;
          import Ball;
         
          /*#*#*#*#*#*#*#*#*#*#*    Document Class for Ego Game      #*#*#*#*#*#*#*#*#*#**/
          public class EgoGame extends MovieClip
          {
              public var textIntro:textInt = new textInt();
              public var egoIntro:EgoIntro=new EgoIntro();
              public var ball:Array = new Array(0,1,2,3);
              public var zone:Array = new Array();
              public var zoneFull:Array = new Array(false, false, false, false, false, false, false, false, false);     
              public var fullResetBtn:Logo = new Logo();
              public var fullResetBtnIntro:Tween;
              public var ballResetBtn:BallReset = new BallReset();
              public var musicPlayer:MusicPlayer;
              public var musicPlayerIntro:Tween;
              public var currentBall;
         
              /*#*#*#*#*#*        Constructor method for EgoGame Document Class      #*#*#*#*#**/
              public function EgoGame()
              {
                  // variables to be used when adding zones to stage
                  var xRow1 = 210;
                  var xRow2 = 210;
                  var xRow3 = 210;
                  var yPos = 105;
                 
                  // zone creation and add to stage
                  for (var i:uint = 0; i<=8; i++)
                  {
                      zone[i] = new Zone();           
                     
                      // Row 1 of zones
                      if (i <= 2)
                      {
                          zone[i].x = xRow1;
                          zone[i].y = yPos;
                          xRow1 += 305;
                         
                      }
                      // Row 2 of zones
                      else if (i >= 3 && i <= 5)
                      {
                          zone[i].x = xRow2;
                          zone[i].y = yPos + 155;
                          xRow2 += 305;
                      }
                      // Row 2 of zones
                      else if (i >= 6 && i <= 8)
                      {
                          zone[i].x = xRow3;
                          zone[i].y = yPos + 310;
                          xRow3 += 305;
                      }
                     
                      zone[i].name = "zone[" + i + "]";
                      stage.addChild(zone[i]);           
                  }
                 
                  // Full reset button add to stage
                  stage.addChild(fullResetBtn);
                  fullResetBtn.scaleX = 0.3;
                  fullResetBtn.scaleY = 0.3;
                  fullResetBtn.x = 1000;
                  fullResetBtn.y = 40;
                  fullResetBtn.alpha = 0;
                  fullResetBtn.buttonMode = true;
                  fullResetBtn.addEventListener(MouseEvent.MOUSE_OVER, fullResetVisible);
                  fullResetBtn.addEventListener(MouseEvent.MOUSE_OUT, fullResetInvisible);
                  fullResetBtn.addEventListener(MouseEvent.MOUSE_DOWN, fullReset);
                     
                  // ball creation and add to stage
                  ball[0]=new Ball_Blu();
                  ball[1]=new Ball_Yel();
                  ball[2]=new Ball_Red();
                  ball[3]=new Ball_Gre();
                  for(var j:uint = 0; j<=3; j++)
                  {
                      ball[j]._ballPlaced[j] = false;
                      stage.addChild(ball[j]);
                      ball[j].scaleX = 0.85;
                      ball[j].scaleY = 0.85;
                      ball[j].x=Math.round(Math.random() * 600 + (stage.stageWidth - 825));
                      ball[j].y=Math.round(Math.random()* 45 +(stage.stageHeight + 100));
                      ball[j].name = "ball[" + j + "]";
                      ball[j].addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                      ball[j].addEventListener(Event.ENTER_FRAME, playVideo);
                      ball[j].addEventListener(MouseEvent.ROLL_OVER, ballResetVisible);
                      ball[j].addEventListener(MouseEvent.ROLL_OUT, ballResetInvisible);
                  }
                 
                  stage.addChild(textIntro);
                  textIntro.alpha = 0.9;
                  textIntro.x=stage.stageWidth / 2;
                  textIntro.y=stage.stageHeight / 2;
                  textIntro.addEventListener(MouseEvent.MOUSE_DOWN, textIntroRemove);
                 
              } /*#*#*#*#*#*    END: Constructor method for EgoGame Document Class        #*#*#*#*#**/
             
                  private function textIntroRemove(event:MouseEvent):void
                  {
                      textIntro.removeEventListener(MouseEvent.MOUSE_DOWN, textIntroRemove);
                      stage.removeChild(textIntro);
                      // After text introduction....the introduction animation starts
                      stage.addChild(egoIntro);
                      egoIntro.x=525;
                      egoIntro.y=325;
                      egoIntro.addEventListener(MouseEvent.MOUSE_DOWN, egoStart);
                      // used dispatchEvent(new Event("introFinished")); in EgoIntro animation @ end of timeline
                      egoIntro.addEventListener("introFinished", onIntroFinished);
                     
                  }// #*#*#*    end of textIntroRemove function
             
                  private function egoStart(event:MouseEvent):void
                  {
                      egoIntro.gotoAndPlay("Start");
                      egoIntro.removeEventListener(MouseEvent.MOUSE_DOWN, egoStart);
                  }// #*#*#*    end of egoStart function
                 
                  private function onIntroFinished(event:Event = null):void
                  {
                      egoIntro.removeEventListener(MouseEvent.MOUSE_DOWN, egoStart);
                      stage.removeChild(egoIntro);

                      for(var j:uint = 0; j<=3; j++)
                      {
                          ball[j].introTweenFunc();
                      }
                     
                      musicPlayer = new MusicPlayer();
                      stage.addChild(musicPlayer);
                      musicPlayer.x = 10;
                      musicPlayer.y = 625;
                      musicPlayer.alpha = 0;
                      musicPlayer.addEventListener(MouseEvent.ROLL_OVER, musicPlayerVisible);
                      musicPlayer.addEventListener(MouseEvent.ROLL_OUT, musicPlayerInvisible);
                     
                      fullResetBtnIntro = new Tween(fullResetBtn, "alpha", Regular.easeIn, 0, 1, 1, true);
                      fullResetBtnIntro.addEventListener(TweenEvent.MOTION_FINISH,fullResetBtnIntro2);
                     
                      musicPlayerIntro = new Tween(musicPlayer, "alpha", Regular.easeIn, 0, 1, 1, true);
                      musicPlayerIntro.addEventListener(TweenEvent.MOTION_FINISH,musicPlayerIntro2);
                  }// #*#*#*    end of onIntroFinished function
                 
                  private function fullResetBtnIntro2(event:Event):void
                  {
                      var fullResetBtnIntro2:Tween = new Tween(fullResetBtn, "alpha", Regular.easeOut, 1, 0, 1, true);
                  }// #*#*#*    end of fullResetBtnIntro2 func
                 
                  private function musicPlayerIntro2(evetn:Event):void
                  {
                      var musicPlayerIntro2:Tween = new Tween(musicPlayer, "alpha", Regular.easeOut, 1, 0, 4, true);       
                  }// #*#*#*    end of musicPlayerIntro2 func

      Document Class Code continued...

      ActionScript Code:
      private function zoneFill(event:MouseEvent):void
                  {
                      zoneFillLoop: for (var i:uint = 0; i<=8; i++)
                      {
                          var index:Number = event.target.name.substring(5,6);

                          if(event.target.hitTestObject(zone[i]) && zoneFull[i] == false && event.target._ballPlaced[index]== false)
                          {
                              event.target.x = zone[i].x;
                              event.target.y = zone[i].y;
                              event.target.scaleX = 1;
                              event.target.scaleY = 1;
                              zoneFull[i] = true;
                              event.target._ballPlaced[index] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
                              return; // Completely exits function once reached
                          }
                      }
                      event.target.x=Math.random() * 900 + 50;
                      event.target.y=Math.random()* 55 + 550;
                      event.target._ballPlaced[index] = false;
                      event.target.scaleX = 0.85;
                      event.target.scaleY = 0.85;
                      event.target.unlockBall(event);
                  }//  #*#*#*  end of zoneFill function

                  private function playVideo(event:Event):void
                  {
                      // ball[0] = Blu     ball[1] = Yel     ball[2] = Red     ball[3] = Gre
                      if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3])
                      {
                          ball[0].gotoAndPlay("BluYelRedGre");
                          ball[1].gotoAndPlay("BluYelRedGre");
                          ball[2].gotoAndPlay("BluYelRedGre");
                          ball[3].gotoAndPlay("BluYelRedGre");
                      }
                      else if (ball[0]._ballPlaced[0] == false && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3])
                      {
                          ball[1].gotoAndPlay("YelRedGre");
                          ball[2].gotoAndPlay("YelRedGre");
                          ball[3].gotoAndPlay("YelRedGre");
                      }
                      else if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] == false && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3])
                      {
                          ball[0].gotoAndPlay("BluRedGre");
                          ball[2].gotoAndPlay("BluRedGre");
                          ball[3].gotoAndPlay("BluRedGre");
                      }
                      else if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] == false && ball[3]._ballPlaced[3])
                      {
                          ball[0].gotoAndPlay("BluYelGre");
                          ball[1].gotoAndPlay("BluYelGre");
                          ball[3].gotoAndPlay("BluYelGre");
                      }
                      else if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3]  == false)
                      {
                          ball[0].gotoAndPlay("BluYelRed");
                          ball[1].gotoAndPlay("BluYelRed");
                          ball[2].gotoAndPlay("BluYelRed");
                      }
                      else if (ball[0]._ballPlaced[0] == false && ball[1]._ballPlaced[1] == false && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3])
                      {
                          ball[2].gotoAndPlay("RedGre");
                          ball[3].gotoAndPlay("RedGre");
                      }
                      else if (ball[0]._ballPlaced[0] == false && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] == false && ball[3]._ballPlaced[3])
                      {
                          ball[1].gotoAndPlay("YelGre");
                          ball[3].gotoAndPlay("YelGre");
                      }
                      else if (ball[0]._ballPlaced[0] == false && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3] == false)
                      {
                          ball[1].gotoAndPlay("YelRed");
                          ball[2].gotoAndPlay("YelRed");
                      }
                      else if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] == false && ball[2]._ballPlaced[2] == false && ball[3]._ballPlaced[3])
                      {
                          ball[0].gotoAndPlay("BluGre");
                          ball[3].gotoAndPlay("BluGre");
                      }
                      else if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] == false && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3]  == false)
                      {
                          ball[0].gotoAndPlay("BluRed");
                          ball[2].gotoAndPlay("BluRed");
                      }
                      else if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] == false && ball[3]._ballPlaced[3]  == false)
                      {
                          ball[0].gotoAndPlay("BluYel");
                          ball[1].gotoAndPlay("BluYel");
                      }
                      else if (ball[0]._ballPlaced[0] == false && ball[1]._ballPlaced[1] == false && ball[2]._ballPlaced[2] == false && ball[3]._ballPlaced[3])
                      {
                          ball[3].gotoAndPlay("Gre");
                      }
                      else if (ball[0]._ballPlaced[0] == false && ball[1]._ballPlaced[1] == false && ball[2]._ballPlaced[2] && ball[3]._ballPlaced[3]  == false)
                      {
                          ball[2].gotoAndPlay("Red");
                      }
                      else if (ball[0]._ballPlaced[0] == false && ball[1]._ballPlaced[1] && ball[2]._ballPlaced[2] == false && ball[3]._ballPlaced[3]  == false)
                      {
                          ball[1].gotoAndPlay("Yel");
                      }
                      else if (ball[0]._ballPlaced[0] && ball[1]._ballPlaced[1] == false && ball[2]._ballPlaced[2] == false && ball[3]._ballPlaced[3]  == false)
                      {
                          ball[0].gotoAndPlay("Blu");
                      }
                  }//  #*#*#*  end of playVideo function       
                 
                  private function ballResetInvisible(event:MouseEvent):void
                  {
                      ballResetBtn.alpha = 0;
                  }// #*#*#*    end of ballResetInvisible function*/     
                 
                  private function ballResetVisible(event:MouseEvent):void
                  {
                      currentBall = event.target;
                     
                      if (currentBall._ballPlaced[currentBall.name.substring(5,6)])
                      {
                          stage.addChild(ballResetBtn);
                          ballResetBtn.alpha = 1;
                          ballResetBtn.x = currentBall.x + 150;
                          ballResetBtn.y = currentBall.y - 75;
                          ballResetBtn.addEventListener(MouseEvent.ROLL_OVER, ballResetActive)
                          ballResetBtn.addEventListener(MouseEvent.MOUSE_DOWN, ballResetPressed)
                      }
                  }// #*#*#*    end of ballResetVisible function
                     
                  // function included so button doesn't disappear when mouse rolls over it
                  private function ballResetActive(event:MouseEvent):void
                  {
                      ballResetBtn.buttonMode = true;
                      ballResetBtn.alpha = 1;
                  }// #*#*#*    end of ballResetActive function
             
                  private function ballResetPressed(event:MouseEvent):void
                  {
                      for(var i:Number=0; i<=8; i++)
                      {
                          if(currentBall.hitTestObject(zone[i]))
                          {
                              zoneFull[i] = false;
                              currentBall._ballPlaced[currentBall.name.substring(5,6)] = false;
                              currentBall.gotoAndPlay(1);
                              currentBall.x=Math.round(Math.random() * 600 + (stage.stageWidth - 825));
                              currentBall.y=Math.round(Math.random()* 45 +(stage.stageHeight + 100));
                              currentBall.scaleX = 0.85;
                              currentBall.scaleY = 0.85;
                              currentBall.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                              currentBall.unlockBall(event);
                              ballResetBtn.alpha = 0;
                          }
                      }
                  }// #*#*#*    end of ballResetPressed function
                             
                  private function fullResetVisible(event:MouseEvent):void
                  {
                      var fullResetVisTween:Tween = new Tween(fullResetBtn, "alpha", Strong.easeInOut, 0, 0.5, 0.5, true);
                  }// #*#*#*    end of fullResetVisible func
                  private function fullResetInvisible(event:MouseEvent):void
                  {
                      var fullResetInvTween:Tween = new Tween(fullResetBtn, "alpha", Strong.easeInOut, 0.5, 0, 1, true);
                  }// #*#*#*    end of fullResetVisible func
                 
                 
                  private function fullReset(event:Event):void
                  {           
                      for (var i:uint = 0; i<=8; i++)
                      {
                          zoneFull[i] = false;
                      }
                     
                      for (var j:uint = 0; j<=3; j++)
                      {
                          ball[j]._ballPlaced[j] = false;
                          ball[j].gotoAndPlay(1);
                          ball[j].x=Math.round(Math.random() * 600 + (stage.stageWidth - 825));
                          ball[j].y=Math.round(Math.random()* 45 +(stage.stageHeight + 100));//ball[j].randomY1;
                          ball[j].scaleX = 0.85;
                          ball[j].scaleY = 0.85;
                          ball[j].addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                          if(ball[j].introTween.isPlaying)
                          {
                              ball[j].introTween.stop();
                              ball[j].myTweenX.stop();
                          }
                          else if(ball[j].myTweenX.isPlaying && ball[j].myTweenY.isPlaying)
                          {
                              ball[j].myTweenX.stop();
                              ball[j].myTweenY.stop();
                          }
                          ball[j].unlockBall(event);
                      }
                  }// #*#*#*    end of fullReset function
                 
                  private function musicPlayerVisible(event:MouseEvent):void
                  {
                      var musicPlayerVisTween:Tween = new Tween(musicPlayer, "alpha", Strong.easeInOut, 0, 1, 1, true);
                  }// #*#*#*    end of musicPlayerVisible function
                 
                  private function musicPlayerInvisible(event:MouseEvent):void
                  {
                      var musicPlayerInvisTween:Tween = new Tween(musicPlayer, "alpha", Strong.easeInOut, 1, 0, 1, true);
                  }// #*#*#*    end of musicPlayerInvisible function
                 
          } /*#*#*#*#*#*#*#*#*#*#*        END: Document Class for Ego Game        #*#*#*#*#*#*#*#*#*#**/
      }// end of package

      Ball Class

      ActionScript Code:
      package {
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          //import flash.display.DisplayObject;
          import flash.events.Event;
          import fl.transitions.Tween;
          import fl.transitions.easing.*;
          import fl.transitions.TweenEvent;
          //import EgoGame;

          /*#*#*#*#*#*#*#*#*#*#*    Document Class for Ball.as        #*#*#*#*#*#*#*#*#*#**/
          public class Ball extends MovieClip {

              public var _ballPlaced:Array=new Array  ;
              public var myTweenX:Tween;
              public var myTweenY:Tween;
              public var introTween:Tween;
              public var xBoundary:Array=new Array(50,1000);
              // index of xBoundary is 0 - 1 (or the length of vals - 1). Code below picks randomly one of the Array values above.
              public var randomXBoundary = (xBoundary[int(Math.random() * xBoundary.length)]);
              var randomTime=Math.random()*20+20;
              public var randomY1:Number = Math.round(Math.random() * 45 + 570);
              public var randomY2:Number = Math.round(Math.random() * 45 + 570);

              /*#*#*#*#*#*        Constructor method for Ball.as Class        #*#*#*#*#**/
              public function Ball() {
                  this.buttonMode=true;
                  this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                  this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
              }/*#*#*#*#*#*      END: Constructor method for Ball.as Class    #*#*#*#*#**/

              public function introTweenFunc(event:TweenEvent=null):void {
                  introTween=new Tween(this,"y",Elastic.easeOut,this.y,randomY1,10,true);
                  tweenXfunc();
                  introTween.addEventListener(TweenEvent.MOTION_FINISH, tweenYfunc);
              }// #*#*#*    end of introTweenFunc function
              public function tweenXfunc(event:TweenEvent=null):void {
                  // if/else if conditions to swap around the x boundary the ball is heading towards
                  if (randomXBoundary==50) {
                      randomXBoundary=randomXBoundary+950;
                  } else if (randomXBoundary == 1000) {
                      randomXBoundary=randomXBoundary-950;
                  }
                  myTweenX=new Tween(this,"x",Regular.easeInOut,this.x,randomXBoundary,randomTime,true);
                  myTweenX.addEventListener(TweenEvent.MOTION_FINISH,tweenXfunc);
              }// #*#*#*    end of tweenX function
              public function tweenYfunc(event:TweenEvent=null):void {
                  myTweenY = new Tween(this, "y", Regular.easeInOut, this.y, randomY2, 3, true);
                  myTweenY.addEventListener(TweenEvent.MOTION_FINISH,tweenY2func);
              }// #*#*#*    end of tweenY function
              public function tweenY2func(event:TweenEvent=null):void {
                  myTweenY = new Tween(this, "y", Regular.easeInOut, this.y, randomY1, 3, true);
                  myTweenY.addEventListener(TweenEvent.MOTION_FINISH,tweenYfunc);
              }// #*#*#*    end of tweenY2 function

              private function dragIt(event:MouseEvent):void {
                  this.startDrag();
                  this.parent.setChildIndex(this, this.parent.numChildren-1);
                  if(introTween.isPlaying)
                  {
                      introTween.stop();
                      myTweenX.stop();
                  }
                  else if(myTweenX.isPlaying && myTweenY.isPlaying)
                  {
                      //trace("myTweenX & myTweenY is playing");
                      myTweenX.stop();
                      myTweenY.stop();
                  }
              }//  #*#*#*  end of dragIt function

              private function dropIt(event:MouseEvent):void {
                  this.stopDrag();
              }//  #*#*#*  end of dropIt function

              public function lockBall(event:Event):void {
                  if (_ballPlaced[this.name.substring(5,6)]==true) {
                      this.removeEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                      this.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
                      this.buttonMode=false;
                  }
              }//  #*#*#*  end of lockBall function

              public function unlockBall(event:Event):void {
                  if (_ballPlaced[this.name.substring(5,6)]==false) {
                      this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                      this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
                      this.buttonMode=true;
                      tweenXfunc();
                      tweenYfunc();
                  }
              }//  #*#*#*  end of lockBall function
          } /*#*#*#*#*#*#*#*#*#*#*        END: Document Class for Ball.as   #*#*#*#*#*#*#*#*#*#**/
      }// end of package


      The End!