Skip navigation
LeFroglet
Currently Being Moderated

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

Apr 7, 2013 10:19 AM

Tags: #flash #builder #movieclip

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?

 
Replies
  • Currently Being Moderated
    Apr 7, 2013 11:07 AM   in reply to LeFroglet

    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)

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 7, 2013 11:44 PM   in reply to LeFroglet

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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2013 5:21 AM   in reply to LeFroglet

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2013 5:59 AM   in reply to LeFroglet

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2013 6:21 AM   in reply to LeFroglet

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 7:35 AM   in reply to LeFroglet

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2013 8:25 AM   in reply to LeFroglet

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 12:20 AM   in reply to LeFroglet

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2013 5:16 AM   in reply to LeFroglet

    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

        }

    }*/

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points