3 Replies Latest reply on Nov 11, 2011 5:00 PM by kglad

    Activate & Deactivate Functions!

    McbainGames11

      Hey,

       

      Class 'Main' is my document class:

       

      Well i have falling objects and i have 2 functions to activating and deactivating which isn't going so well.

      My deactivate function doesn't work very well with resetting everything back to normal.

      Things my deactive function doesn't do:

      -When i call my deactivate function it only pauses the object and when i activate again it will un-pause. << I think this is because i havn't reset my arrays and has them stored for next time.

       

      Simply, what i want to do is to do the opposite in my class to make the class 'Stop Running'

       

      So how do i reset my arrays and anything else that needs resetting to deactivate my class,Here is my code , just look for my deactivate function near the top of the code:

       

      package

      {

       

          import flash.display.MovieClip;

       

          import flash.events.Event;

       

          import flash.events.MouseEvent;

       

          import flash.events.TimerEvent;

       

          import flash.utils.Timer;

       

          import flash.display.DisplayObject;

       

          public class Main extends MovieClip

          {

       

              public var enemyA:Array = [];

       

              public var birdA:Array = [];

       

              public var MAX_ENEMIES:int = 10;

       

              public var gap:int = 100;

       

              public var timer:int = 0;

       

              public var time_limit:int = 10;

       

              public var TimerSpeed:uint = 1000;

       

              public var MyTimer:Timer = new Timer(TimerSpeed);

       

              public var TheBird:BirdChar = new BirdChar();

       

              public function Main()

              {

       

                  // Constructor Code

       

              }

              public function activateGameControls()

              {

       

                  createEnemyF();

       

                  createBirdF();

       

                  this.addEventListener(Event.ENTER_FRAME,loopF);

       

                  MyTimer.addEventListener(TimerEvent.TIMER,TimerHandler);

       

                  MyTimer.start();

       

              }

              public function deactivateGameControls()

              {

       

                  if (getChildByName("enemy_mc")!=null)

                  {

                 

                  removeChild(getChildByName("enemy_mc"));

                 

                  }

                 

                  addChild(TheBird);

       

                  removeChild(TheBird);

                 

                  this.removeEventListener(Event.ENTER_FRAME, loopF);

       

                  MyTimer.removeEventListener(TimerEvent.TIMER, TimerHandler);

                 

                  var birdA:Array = [];

                 

                  var enemyA:Array = [];

       

              }

              public function createEnemyF()

              {

       

                  for (var i:int = 0; i < MAX_ENEMIES; i++)

                  {

       

                      var enemy:DisplayObject = new Enemy();

       

                      enemy.x = Math.random() * stage.stageWidth - enemy.width;

       

                      enemy.y =  -  i * (gap + enemy.height) - 30.65;

       

                      addChild(enemy);

       

                      //trace(enemy.x, enemy.y);

       

                      enemyA.push(enemy);

                     

                      enemy.name = "enemy_mc";

       

                  }

       

              }

              public function createBirdF()

              {

       

                  TheBird.x = 270.95;

       

                  TheBird.y = 350.95;

       

                  birdA.push(TheBird);

       

                  addChild(TheBird);

       

              }

              public function loopF(event:Event)

              {

       

                  updateBirdPositionsF();

                 

                  updateEnemyPositionsF();

       

              }

              public function updateEnemyPositionsF()

              {

       

                  for (var i:int = 0; i < enemyA.length; i++)

                  {

       

                      var enemy:DisplayObject = enemyA[i] as DisplayObject;

       

                      //trace("this is your enemy #" + i + ": " + enemy + ", do whatever with him.");

       

                      enemy.y +=  2;

       

                      if (enemy.hitTestObject(TheBird))

                      {

       

                          //trace("BAAAM! Enemy #" + i + " hit whatever he was supposed to hit.");

       

                          gotoAndPlay(5);

       

                          MyTimer.removeEventListener(TimerEvent.TIMER,TimerHandler);

       

                          deactivateGameControls();

       

                          addChild(enemy);

       

                          removeChild(enemy);

       

                      }

       

                  }

       

              }

              public function updateBirdPositionsF()

              {

       

                  TheBird.x = mouseX;

       

              }

              function TimerHandler(event:TimerEvent)

              {

       

                  timer +=  1;

       

                  if ((timer == time_limit))

                  {

       

                      MyTimer.removeEventListener(TimerEvent.TIMER,TimerHandler);

       

                      gotoAndPlay(5);

       

                      deactivateGameControls();

       

                      trace('This level has been completed!');

       

                  }

       

              }

       

          }

       

      }

       

      Cheers!