7 Replies Latest reply on Nov 9, 2011 9:02 PM by relaxatraja

    How to activate something inside the function outside function? - class

    McbainGames11 Level 2

      Hey,

       

      I need to activate some lines of code inside a function because that is where the object name has been made.

       

      Would a boolean work well? If so how exactly would i implement it? I tried booleans but had no success because i am not experienced with them...

       

      Heres some code:

       

      public var myboolean:Boolean = new Boolean();

       

      public function Function_One()

      {

       

      }

       

      public function Function_Two()

      {

       

      var myObject:DisplayObject = new ObjectClass();

      //I want the object name 'myObject to be used in Function_One() but i can't so whats a good method to fix this?

       

      }

       

      I was thinking a boolean could be useful so it could 'set off' my lines of code with an if statement of a boolean. But no Luck..

       

      Or is there a better way to achieve this?

       

      Thanks.

        • 1. Re: How to activate something inside the function outside function? - class
          relaxatraja Level 5

          public var myObject:DisplayObject;

           

          public function Function_One()

          {

               myObject = new ObjectClass();

           

          }

           

          public function Function_Two()

          {

           

          }

          Try this

          • 2. Re: How to activate something inside the function outside function? - class
            McbainGames11 Level 2

            public function Function_One()

            {

             

            }

             

            public function Function_Two()

            {

             

            // I can't move this line of code because what i want to implement the solution i get with this basic code into my real class

            // what i want is to remove the object by using removeChild(myObject); in a function outside of this class...How to do this?

            var myObject:DisplayObject = new ObjectClass();

             

            }

            • 3. Re: How to activate something inside the function outside function? - class
              relaxatraja Level 5

              What I have shown is able to access the variable outside of the function, if you declare the variable with var within a function, then the scope of the variable only within that function. Otherwise give a try like below:

               

              public function Function_Two()

              {

               

              // I can't move this line of code because what i want to implement the solution i get with this basic code into my real class

              // what i want is to remove the object by using removeChild(myObject); in a function outside of this class...How to do this?

              var myObject:DisplayObject = new ObjectClass();

              myObject.name="testmc";

              addChild(myObject);

              }

               

              public function Function_remove(){

                  removeChild(MovieClip(getChildByName("testmc"));

              }

               

              play with getChildByName and getChildAt.

              1 person found this helpful
              • 4. Re: How to activate something inside the function outside function? - class
                McbainGames11 Level 2

                Still can't get it to work, here's my script now what exactly do i do?

                 

                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()

                        {

                 

                            removeChild(MovieClip(getChildByName("enemy_mc"))); //code you suggested...<<

                 

                            addChild(TheBird);

                 

                            removeChild(TheBird);

                 

                            MyTimer.removeEventListener(TimerEvent.TIMER,TimerHandler);

                 

                        }

                        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"; //code you suggested...<<

                 

                            }

                 

                        }

                        public function createBirdF()

                        {

                 

                            TheBird.x = 270.95;

                 

                            TheBird.y = 350.95;

                 

                            birdA.push(TheBird);

                 

                            addChild(TheBird);

                 

                        }

                        public function loopF(event:Event)

                        {

                 

                            updateEnemyPositionsF();

                 

                            updateBirdPositionsF();

                 

                            hitTestF();

                 

                        }

                        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;

                 

                        }

                        public function hitTestF()

                        {

                 

                            /*if (TheBird.hitTestObject(enemy))

                            {

                 

                            gotoAndPlay(5);

                 

                            deactivateGameControls();

                 

                            trace('The Bird Hit Enemy 1');

                 

                            }*/

                 

                        }

                        function TimerHandler(event:TimerEvent)

                        {

                 

                            timer +=  1;

                 

                            updateEnemyPositionsF();

                 

                            if ((timer == time_limit))

                            {

                 

                                MyTimer.removeEventListener(TimerEvent.TIMER,TimerHandler);

                 

                                gotoAndPlay(5);

                 

                                deactivateGameControls();

                 

                                trace('Level One is Completed');

                 

                            }

                 

                        }

                 

                    }

                 

                }

                • 5. Re: How to activate something inside the function outside function? - class
                  relaxatraja Level 5

                  Try with method 1:

                   

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

                   

                  Try with method 2:

                  for (i=this.numChildren-1;i>=0;i--){

                         trace(bg.getChildAt(i).name);  //make sure the name you specified on stage

                         if (this.getChildAt(i).name=="enemy_mc") bg.removeChildAt(i);

                  }

                   

                   

                  place a trace statement and identify first whether it retrieve the movieclip "enemy_mc"

                  • 6. Re: How to activate something inside the function outside function? - class
                    McbainGames11 Level 2

                    It worked(Method 1)! Never thought i would love 2 lines of code this much

                     

                    Cheers, I will remember this method for next time i get into a situation like this!

                     

                    Question Closed.