0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
silvith
  • 290
  • 2
  • 14

2 Answers2

0

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

Community
  • 1
  • 1
silvith
  • 290
  • 2
  • 14
  • the easier way [if assets are not created dynamically] is to reference items with brackets such as this["bg"].stop() or if you want type hinting MovieClip(this["bg"]).stop() is usually faster to write, but only works if you didn't create the asset dynamically. However, if you did, you probably have them stored in a class variable anyway and don't need to locate it through another movieclip – Daniel MesSer Dec 11 '12 at 14:39
  • @DanielMesSer As you can see, the asset is created dynamically in the function set_game(). This bit of your comment I don't understand: you probably have them stored in a class variable anyway and don't need to locate it through another movieclip. Could you explain that? – silvith Dec 13 '12 at 09:49
  • It's a bit hard to write descriptive stuff in a comment, but I'll try. What I mean is that since you create them dynamically you usually do it inside some kind of class and then you have option to store them in member variables or arrays or tables or similar things. Such as inside a class: `var myBubbles:Array = [new Bubble(), new Bubble(), new Bubble()]`. And then doing stuff like `myBubbleEmitter.addChild(myBubbles[0])`. `myOtherBubbleEmitter.addChild(myBubbles[1]);` Thus you really dont care about the names of dynamically created stuff since you access them in another way. Makes sense? – Daniel MesSer Dec 13 '12 at 13:57
-1
bg.name = "bg";

actually many times this code doesn't work properly. So, instead of setting name, you should get real instance name of your "achtergrond" object and than you have to use this instance name.

Another Solution:

I think achtergrond is single object so why do you use it with "getChildByName"? Use it like bg.gotoAndStop();.

kvasirr
  • 154
  • 3
  • I have never had problems with names, though I am aware they can occur. And I already tried your other solution, which did not work. I answered my own question, check it out if you are interested. – silvith Dec 11 '12 at 13:17