18 Replies Latest reply: Apr 10, 2013 5:16 AM by moccamaximum RSS

    How do I access a MovieClip's timeline in another Class.

    LeFroglet Community Member

      This has been troubling me for awhile and can't seem to find the correct answer.

       

      Yea so how I access a MovieClip's timeline when using that MovieClip in another class?

        • 1. Re: How do I access a MovieClip's timeline in another Class.
          moccamaximum Community Member

          MovieClip(root).anotherClassInstance.gotoAndPlay("someFrameLabel");

           

          for this to work both classes (the calling and the called) must be added to the DisplayList

          (use Event.ADDED_TO_STAGE for this)

          • 2. Re: How do I access a MovieClip's timeline in another Class.
            LeFroglet Community Member

            Okay so let's say my MovieClip is called "Run" and the class I'm in is called "Main"

             

            MovieClip(Run).run.gotoAndPlay("1");

             

            Would that be right?

            • 3. Re: How do I access a MovieClip's timeline in another Class.
              moccamaximum Community Member

              MovieClip(root).Run.gotoAndPlay(1);

              • 4. Re: How do I access a MovieClip's timeline in another Class.
                LeFroglet Community Member

                Ah I see Erm so let's say this is my main class. Would this be correct on how to access it.

                 

                 

                package

                {

                    import flash.display.*;

                 

                    public class Main extends Sprite

                    {

                        private var _run:Run;

                 

                        public function Main()

                        {

                 

                            addChild(_run);

                            MovieClip(root).Run.gotoAndStop(1);

                            _run.x = stage.stageWidth / 2;

                            _run.y = stage.stageHeight /2;

                        }

                    }

                }

                • 5. Re: How do I access a MovieClip's timeline in another Class.
                  moccamaximum Community Member

                  there is no need to call a child of the main class via root.

                   

                  in your case you would write simply:

                   

                  _run.gotoAndStop(1);

                   

                   

                  say you have a class called button.as, and you wanted to play the _run instance from there if you click it, the you would this syntax:

                   

                  ...

                  public class button extends Sprite

                      {

                   

                   

                          public function button()

                          {

                   

                             addEventListener(MouseEvent.CLICK, startRun);

                   

                          }

                   

                            public fucntion startRune(e:MouseEvent):void{

                               MovieClip(root)._run.gotoAndStop(1);

                            }

                      }

                   

                  ...

                   

                  Okay so let's say my MovieClip is called "Run"

                  TIP:

                  Be clear about the differences of classes, instances and instance names in as3.

                   

                  Run is the class

                  _run is the instance(object)

                  the descriptor of the instance and the name (what it is called) are not the same

                  if you don`t assign a name to an instance flash/FB will do that automatically for you (for example "instance123"), but the problem is, the moment you generate multiple instances the descriptior alone becomes useless to reference an object

                  • 6. Re: How do I access a MovieClip's timeline in another Class.
                    LeFroglet Community Member

                    I tried that but I must be doing something else wrong, I think this is what I need to know the most in FB as I imagine MC's are needed in all games. I've only made small one's where everything can go in one class.

                     

                     

                    Okay this my main Class.

                     

                    package

                    {

                        import flash.display.*;

                         

                        public class Classy extends Sprite

                        {

                            private var _run:Run;

                           

                            public function Classy()

                            {

                     

                                 //By doing this the MC appears on stage but it just cycles through all the different animations.

                                _run = new Run();

                                addChild(_run);

                                _run.x = 275;

                                _run.y = 200;

                     

                                 //If i try this it doesn't regonise the _run as a MC, as it's to the one with the timeline.

                                  _run.gotoAndStop(1);

                                  

                                   //This causes it to go to debug mode.

                                   MovieClip(root).gotoAndStop(1);

                            }

                        }

                    }

                     

                     

                    And this is my other class with the MC.

                     

                    package

                    {

                        import flash.display.*;

                       

                        public class Hero extends Sprite

                        {

                            [Embed(source="../swfs/movement3.swf", symbol="Run")]

                            private var Run:Class;

                           

                            private var _run:MovieClip = new Run();

                           

                            public var vx:int = 0;

                            public var vy:int = 0;

                           

                            public function Hero()

                            {

                                this.addChild(idle);

                               

                            }

                           

                        }

                    }

                     

                    Sorry I think I'm being a tard >_< ..If you could just show me on that how to access it, I would be so greatful, as I asked around on Newgrounds and no one seemed to respond or give a definate answer. Where as I thought everyone would know how to do it and show and tell.

                    • 7. Re: How do I access a MovieClip's timeline in another Class.
                      moccamaximum Community Member

                      1.if you work with classes that have or access other timelines don`t extend Sprite, but extend MovieClip;

                      A Sprite object is similar to a movie clip, but does not have a timeline.

                       

                      I don`t really understand why you would instantiate Run in two different classes.

                      What is Hero? Is that your player figure and has it a running state or sth?

                      What are you trying to achieve?

                      Also its hard to see if you abbreviated code in your classes (deliberately left it out) or if its missing.

                      A line like this

                      this.addChild(idle);

                       

                      can never work in the context you show without having idle somewhere defined.

                      Attach a screenshot that shows the timeline of your "Run"- Symbol.

                      • 8. Re: How do I access a MovieClip's timeline in another Class.
                        LeFroglet Community Member

                        That was meant to say "this.addChild(_run)"

                         

                        I'm trying to achieve the fact that I want to make a bigger game and having my character/enemie's etc in a different class would shrink down code in the main class.

                         

                        "Hero" is just a class which has my main character, The embedded file has around 24 different animation states.

                         

                        I just want to access those from another Class.

                        • 9. Re: How do I access a MovieClip's timeline in another Class.
                          moccamaximum Community Member

                          Attach a screenshot that shows the timeline of your "Run"- Symbol.

                          At the moment I have too few information to give you helpful advise.

                          How do you achieve the animations of your Hero-Character? Do you use labeled frames?

                          Or a special game framework like Flixel, CitrusEngine? Do you have stop fames/switches on the timeline of your Run symbol to keep the chjaracter from looping through all animations?

                          Do you use spritesheets?

                          Why don`t you add an instance of hero to your document instead of a Run instance?

                           

                          My original advise, that is nowhere to be found in the code you showed:

                           

                          for this to work both classes (the calling and the called) must be added to the DisplayList

                          (use Event.ADDED_TO_STAGE for this)

                           

                          if you don`t make sure the instance is present at stage, a call lieke gotoandStop... will never work.

                           

                          addChild(_run) is not enough to ensure run is actually added to stage when you call gotoAndStop..

                          (use Event.ADDED_TO_STAGE for this)

                           

                          stands

                          • 10. Re: How do I access a MovieClip's timeline in another Class.
                            LeFroglet Community Member

                            Sorry yea had a break from AS3 for a couple of weeks mind seems to have gone blank, but yea I'll try it the way you originally said and get back to you, thanks for your help.

                            • 11. Re: How do I access a MovieClip's timeline in another Class.
                              LeFroglet Community Member

                              Okay.

                               

                              I used ADDED_TO_STAGE and got it working

                               

                              This is my main Class.

                               

                              package

                              {

                                  import flash.display.*;

                                  import flash.events.Event;

                                 

                                  [SWF(width="550", height="400",

                                  backgroundColor="#FFFFFF", frameRate="60")]

                                 

                                  public class Game extends Sprite

                                  {

                                      private var _hero:Hero;

                                     

                                      public function Game()

                                      {

                                          _hero = new Hero(stage);

                                          stage.addChild(_hero);

                                          _hero.x = 275;

                                          _hero.y = 200;

                                      }   

                                  }

                              }

                               

                              My hero Class:

                               

                              package

                              {

                                  import flash.display.*;

                                  import flash.events.Event;

                                  import flash.events.KeyboardEvent;

                                  import flash.ui.Keyboard;

                                 

                                 

                                  public class Hero extends MovieClip

                                  {

                                      [Embed(source="../swfs/movement3.swf", symbol="Idle")]

                                      private var Idle:Class;

                                     

                                      private var idle:MovieClip = new Idle();

                                     

                                      public var vx:int = 0;

                                      public var vy:int = 0;

                                     

                                     //Stage reference

                                      private var _stage:Object;

                                     

                               

                                      //Movement variables

                                      public var leftPressed:Boolean = false;

                                      public var rightPressed:Boolean = false;

                                      public var upPressed:Boolean = false;

                                      public var downPressed:Boolean = false;

                                     

                                      public function Hero(stage:Object)

                                      {

                                          _stage = stage;

                                          this.addChild(idle);

                                          this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

                                         

                                      }

                                      private function addedToStageHandler(event:Event):void

                                      {

                                          startGame();

                                          idle.gotoAndStop(1);

                                          this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

                                      }

                                      public function startGame():void

                                      {

                                          _stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

                                          _stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

                                          _stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

                                      }

                                      public function enterFrameHandler(event:Event):void

                                      {

                                            

                                         //Move the hero

                                          idle.x += vx;

                                          idle.y += vy;

                                         

                               

                                          //Move 8 directions with different animations.

                                          if(upPressed)

                                          {

                                              if(rightPressed)

                                              {

                                                  idle.gotoAndStop(6);

                                                  vy = -4;

                                                  vx = 4;

                                              }

                                              else if(leftPressed)

                                              {

                                                  idle.gotoAndStop(7);

                                                  vy = -4;

                                                  vx = -4;

                                              }

                                              else

                                              {

                                                  idle.gotoAndStop(5);

                                                  vy = -4;

                                              }

                                          }

                                          else if(leftPressed)

                                          {

                                              if(upPressed)

                                              {

                                                  idle.gotoAndStop(7);

                                                  vy = -4;

                                                  vx = -4;

                                              }

                                              else if(downPressed)

                                              {

                                                  idle.gotoAndStop(8);

                                                  vy = 4;

                                                  vx = -4;

                                              }

                                              else

                                              {

                                                  idle.gotoAndStop(3);

                                                  vx = -4;

                                              }

                                             

                                          }

                                          else if(rightPressed)

                                          {

                                              if(upPressed)

                                              {

                                                  idle.gotoAndStop(6);

                                                  vy = -4;

                                                  vx = 4;

                                                 

                                              }

                                              else if(downPressed)

                                              {

                                                  idle.gotoAndStop(9);

                                                  vy = 4;

                                                  vx = 4;

                                              }

                                              else

                                              {

                                                  idle.gotoAndStop(2);

                                                  vx = 4;            

                                              }

                                          }

                                          else if(downPressed)

                                          {

                                              if(rightPressed)

                                              {

                                                  idle.gotoAndStop(9);

                                                  vy = 4;

                                                  vx = 4;

                                              }

                                              else if(leftPressed)

                                              {

                                                  idle.gotoAndStop(8);

                                                  vy = 4;

                                                  vx = -4;

                                              }

                                              else

                                              {

                                                  idle.gotoAndStop(4);

                                                  vy = 4;

                                              }

                                          }

                                         

                                      }

                                      public function keyDownHandler(event:KeyboardEvent):void

                                      {

                                          if (event.keyCode == Keyboard.LEFT)

                                          {

                                              leftPressed = true;

                                          }

                                          else if (event.keyCode == Keyboard.RIGHT)

                                          {

                                              rightPressed = true;

                                             

                                          }

                                          else if (event.keyCode == Keyboard.UP)

                                          {

                                              upPressed = true;

                                             

                                          }

                                          else if (event.keyCode == Keyboard.DOWN)

                                          {

                                              downPressed = true;

                                          }

                                      }

                                      public function keyUpHandler(event:KeyboardEvent):void

                                      {

                                          if(event.keyCode == Keyboard.RIGHT)

                                          {

                                              vx = 0;

                                              rightPressed = false;

                                              idle.gotoAndStop(21);

                                             

                                          }

                                          else if(event.keyCode == Keyboard.UP)

                                          {

                                              vy = 0;

                                              upPressed = false;

                                              idle.gotoAndStop(1);

                                          }

                                          else if(event.keyCode == Keyboard.LEFT)

                                          {

                                              vx = 0;

                                              leftPressed = false;

                                              idle.gotoAndStop(20);

                                          }

                                          else if(event.keyCode == Keyboard.DOWN)

                                          {

                                              vy = 0;

                                              downPressed = false;

                                              idle.gotoAndStop(19);

                                          }

                                      }

                                     

                                     

                                  }

                              }

                               

                               

                              Thanks for you help and pateince, i know the main class doesn't have an ADDED_TO_STAGE but my "hero" moves and appears as he should. Thanks again.

                              • 12. Re: How do I access a MovieClip's timeline in another Class.
                                moccamaximum Community Member

                                Congrats for your persistence!

                                 

                                if you will later on in your project use a external preloader to load your game:

                                this line in your document class will not work any longer:

                                 

                                stage.addChild(_hero);

                                 

                                just to give you a heads up

                                • 13. Re: How do I access a MovieClip's timeline in another Class.
                                  LeFroglet Community Member

                                  Oh really how come?

                                   

                                  Do you mind me asking but are you a "Flash Developer"?

                                  • 14. Re: How do I access a MovieClip's timeline in another Class.
                                    moccamaximum Community Member

                                    The contructor of your Game will be immediately called when the preloader starts to load it.

                                    a swf can only acces the highest Object in the hierarchiy (the stage object), if it is part of the display list,

                                    which will not be the fact when you start preloading your Game.swf, but only when you finished.

                                    So the compiler will throw en error.

                                     

                                    Do you mind me asking but are you a "Flash Developer"?

                                    I use FlashDevelop for coding.

                                    • 15. Re: How do I access a MovieClip's timeline in another Class.
                                      LeFroglet Community Member

                                      Ah really, if I create my own Internal Preloader will everything be good?

                                       

                                       

                                      I mean to you have a proffesional job doing Flash coding etc. Just wondered as you've been a member since 2008.

                                      • 16. Re: How do I access a MovieClip's timeline in another Class.
                                        moccamaximum Community Member

                                        Ah really, if I create my own Internal Preloader will everything be good?

                                        Good luck with that ;-)

                                        I mean to you have a proffesional job doing Flash coding etc. Just wondered as you've been a member since 2008

                                        Yes, working professionally with flash since 2007

                                        • 17. Re: How do I access a MovieClip's timeline in another Class.
                                          LeFroglet Community Member

                                          Ah congrats , it's what I want to do ..eventually. Hopefully by the end of the year or sooner.

                                           

                                           

                                          Quick question is it possible to fire "bullet's" in 8 directions with keyboard. I can get the character to move in all 8 directions with different animations etc

                                          but when i use the same principles of coding for firing bullets, he still can only do up down left and right. I read it might be something to do with pressing the

                                          RIGHT, UP and SPACE key at the same time.(well three keys at once)

                                          • 18. Re: How do I access a MovieClip's timeline in another Class.
                                            moccamaximum Community Member

                                            Here is a KeyManager class that should do the trick (not from me):

                                             

                                            package com.snepo.utils

                                            {   

                                                /**

                                                * @author Andrew Wright

                                                */

                                               

                                                import flash.display.Stage;

                                                import flash.events.*;

                                                import flash.utils.*;

                                               

                                                public class KeyManager extends EventDispatcher

                                                {

                                                   

                                                    protected static var stage : Stage;

                                                    protected static var enabled : Boolean = true;

                                                    protected static var pressedKeys : Dictionary = new Dictionary();

                                                    protected static var _sharedKeyManager : KeyManager;

                                                   

                                                    public static function init ( stage : Stage ) : KeyManager

                                                    {

                                                        if ( !sharedKeyManager ) _sharedKeyManager = new KeyManager ( stage );

                                                        return sharedKeyManager;

                                                    }

                                                   

                                                    public static function get sharedKeyManager ( ) : KeyManager

                                                    {

                                                        return _sharedKeyManager;

                                                    }

                                                   

                                                    public function KeyManager ( stage : Stage ) : void

                                                    {

                                                        stage.addEventListener ( KeyboardEvent.KEY_DOWN, handleKeyDown );

                                                        stage.addEventListener ( KeyboardEvent.KEY_UP, handleKeyUp );

                                                       

                                                        KeyManager.stage = stage;

                                                       

                                                        super ( this );

                                                    }

                                                   

                                                    protected function handleKeyDown ( evt : KeyboardEvent ) : void

                                                    {

                                                        pressedKeys [ evt.keyCode ] = true;

                                                       

                                                        dispatchEvent ( evt.clone() as KeyboardEvent );

                                                    }

                                                   

                                                    protected function handleKeyUp ( evt : KeyboardEvent ) : void

                                                    {

                                                        delete pressedKeys [ evt.keyCode ];

                                                       

                                                        dispatchEvent ( evt.clone() as KeyboardEvent );

                                                    }

                                                   

                                                    public function keysPressed ( keys : * ) : Boolean

                                                    {

                                                        var validCombo : Boolean = true;

                                                       

                                                        if ( !(keys is Array) ) keys = [keys];

                                                       

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

                                                        {

                                                            if ( !pressedKeys [ keys[i] ] ) return false;

                                                        }

                                                       

                                                        return true;

                                                    }

                                                   

                                                    public static function keysPressed ( keys : * ) : Boolean

                                                    {

                                                        return sharedKeyManager.keysPressed ( keys );

                                                    }

                                                           

                                                }

                                            }

                                             

                                            //USAGE

                                            /*import flash.ui.Keyboard;

                                            import flash.events.KeyboardEvent;

                                            import com.snepo.utils.KeyManager;

                                             

                                            var km : KeyManager = KeyManager.init( stage );

                                            km.addEventListener( KeyboardEvent.KEY_DOWN, checkForCombo );

                                               

                                            function checkForCombo ( evt : KeyboardEvent ) : void

                                            {

                                                if ( KeyManager.keysPressed( [ Keyboard.UP, Keyboard.SPACE ] ) )

                                                {

                                                    //put your code here

                                                }

                                            }*/