views:

299

answers:

3

I have a custom class that embeds a movie clip. When I try to use parent class's such as gotoAndStop(). It ignores the method. I would like to know what I am doing wrong. below is my code. I call the method in my constructor Thanks!

game object class

package com.objects 
{
    import com.eapi.IGameObject;
    import flash.display.MovieClip;
    /**
     * ...
     * @author Anthony Gordon
     */
    public class GameObject extends MovieClip implements IGameObject
    {


        public function GameObject() 
        {

        }

    }

}

hero class. gotoAndStop() in the constructor

package com.objects 
{
    import flash.display.MovieClip;
    import flash.events.*;
    /**
     * ...
     * @author Anthony Gordon
     */
    [Embed(source='../../../bin/Assets.swf', symbol='Hero')]
    public class Hero extends GameObject
    {   
        private var aKeyPress:Array;
        private var jumpDisabled:Boolean = false;

        public function Hero() 
        {
            gotoAndStop(1) ///<----------------Doesnt stop. Just keeps playing
            wY = 150;
            wX = 90;
            speed = .5;
            aKeyPress = new Array();
            TheGame.sr.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
            TheGame.sr.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
        }

        private function keyDownListener(e:KeyboardEvent):void {
            //trace("down e.keyCode=" + e.keyCode);         
            aKeyPress[e.keyCode]=true;
        }

        private function keyUpListener(e:KeyboardEvent):void {
            //trace("up e.keyCode=" + e.keyCode);
            aKeyPress[e.keyCode]=false;
        }

        override public function UpdateObject():void
        {
            Controls();
            updatePosition();
        }

        private function Controls():void
        {

            if (aKeyPress[38])//Key press up
                ;//dy -= speed;         
            else if (aKeyPress[40])//Key press down
                dy += speed;

            if (aKeyPress[37])//left
            {
                dx -= speed;
            }
            else if (aKeyPress[39])//Right
            {
                dx  += speed;
            }

            if (aKeyPress[32]){//space
                jump();
            }


        }//End Controls

        private function jump():void
        {
            if (!jumpDisabled)
            {
                if (onGround)
                {
                    gotoAndStop(10);
                    dy = -15;
                    jumpDisabled = true;
                }
            }
            else
            {
                jumpDisabled = false;               
            }
        }
    }

}
+1  A: 

You should add a listener to know when its added to stage and initialize your clip there.

public function Hero() {
    if ( stage ) _init( );
    else addEventListener(Event.ADDED_TO_STAGE, _init );
}

private function _init( e:Event = null ):void {
    removeEventListener( Event.ADDED_TO_STAGE, _init );
    gotoAndStop(1) ///<----------------Doesnt stop. Just keeps playing
    wY = 150;
    wX = 90;
    speed = .5;
    aKeyPress = new Array();
    TheGame.sr.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
    TheGame.sr.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
}

Also, i think this reading Order of operations by senocular would be rather interesting. It explains frame execution in timeline objects and object creation.

goliatone
I am a little confused on what the order of operations have to do with telling a movieclip to stop. Also, Hero is not the main class so it does not have reference to the stage property. I did store the stage reference in a constant called TheGame.sr . But anyways, that method didn't work
numerical25
A: 

I noticed you're still using

[Embed(source='../../../bin/Assets.swf', symbol='Hero')]

Since you're using flash develop (but same goes for flex) there's no reason you can't use SWCs Although I can't tell with 100% certainty that that is what is causing this issue, once you start using SWCs it will make a world of difference. Not only will you no longer need to use the embed code, things like gotoandplay and extending classes will work better, and you will be able to see the classes and objects within.

SWFs are not meant to be used this way, and sooner you stop using them for things they are not meant for, the the easier it will be.

Daniel
There is really *nothing* wrong with importing library elements from a SWF file.
poke
Sooo, is it possible to control the playhead of a movieclip from inside a custom class ??
numerical25
well for starters, you would have known right away that that was the issue if you had used the add to library option instead of using embed.while some might argue that there is "nothing wrong' with using embed, I would say it's a lot like coding with notepad. Flash develop doesn't know what you're editing, so it cannot provide much help in terms of code hints. If you add it to library, the class structure is visible, which makes extending classes a breeze.
Daniel
A: 

Figure it out. I forgot that my MovieClip was encapuslated in another movieclip. So i had to call the movieclip that was inside of the Hero symbol

numerical25