Search code examples
actionscript-3flashflash-cs5movieclipdisplayobject

AS3 can't find the correct way to gotoAndStop a movieclip instance in as3


I have a movieclip "achtergrond" from my library which I put on stage with a function like this:

function set_game ()
        {
            oefNr = 4;
            var bg:achtergrond = new achtergrond();
            bg.x = 0;
            bg.y = 0;
            bg.name = "bg";
            bg.gotoAndStop ("uit");
            addChild (bg);
            set_next ();
        }

The movieclip contains 2 frames "aan" and "uit" and it starts on the frame "uit". Further in my game I want to set the frame to "aan" while a sound is playing, like this:

    function playSnd ():void
    {
        getChildByName("bg").gotoAndStop("aan");
        snd = new Sound(new URLRequest("phonetic_" + curArr[curSnd] + ".mp3"));
        cnl = snd.play();
        cnl.addEventListener (Event.SOUND_COMPLETE, completeSnd);
    }

But for the life of me I can't find the correct way to do this. Flash keeps going on about displayObjects and other things, and I have no clue why I can't address my movieclip. Actually, I have a clue, but no more than that. I don't understand this part of Flash very well yet.


Solution

  • The answer is that if I try to access my clip like this: this["bg"] or getChildByName("bg") I am referring to the DisplayObject. This does not have all the methods of a MovieClip, like the gotoAndStop I need in this case.

    I declared a new variable:

    var movie:MovieClip;
    

    Then I cast my DisplayObject as a MovieClip and put it into the var movie:

    function set_game ()
            {
                oefNr = 4;
                var bg:achtergrond = new achtergrond();
                bg.x = 0;
                bg.y = 0;
                bg.name = "bg";
                bg.gotoAndStop ("uit");
                addChild (bg);
                movie = this.getChildByName("bg") as MovieClip;
                set_next ();
            }
    

    Now I can use MovieClip-specific methods like gotoAndStop:

    function playSnd ():void
        {
            movie.gotoAndStop("aan");
            snd = new Sound(new URLRequest("phonetic_" + curArr[curSnd] + ".mp3"));
            cnl = snd.play();
            cnl.addEventListener (Event.SOUND_COMPLETE, completeSnd);
        }
    

    Answer inspired by this answer