Search code examples
actionscript-3flashrobotlegs

Unable to access children in movie clip


Inside flash cs6 I have drawn a flash movieclip in which I set export settings as abc.Gameboard. Inside gameboard I have a bunch of pieces (symbol:Piece) which I export as abc.Piece - both base class set to MovieClip and with class files. The piece has frame labels like hit, over etc.. My problem is accessing the pieces in code so I can eg. gotoAndPlay("mine") - at the moment the event only fires once which is the last piece on the board. I can set the frame action on this last piece but would like to figure out how to do same for each piece.

I add a gameboard to the stage like so

var gb:Gameboard = new Gameboard();
gb.name = "gb001";
contextView.addChild(gb);

Then

contextView.addEventListener(Event.ADDED, thingAdded);

private function thingAdded(event:Event):void
{
    var type:String = event.target.toString();
    switch(type)
    {
        // this runs only once - i want it to run for each piece that is inside the symbol
        case "[object Piece]":

        var p:MovieClip = event.target as Piece;
        p.gotoAndPlay("mine");
        break;
    }
}

or if there's a better way that would be great.. this looks pretty clunky

Edit: Bit more info about how I'm trying to build the gameboard Draw a collection of shapes in illustrator - mask it (Gameboard region). Import into Flash as Graphic. Convert graphic to several movie clip symbols (So JSFL can drill down and access masked pieces) - run JSFL script & create 00's of pieces. Then I set export settings on Piece and Gameboard and add Gameboard to the contextView.


Solution

  • I actually wrote an entire article about this once. The ADDED event should fire once for every DisplayObject that gets added. Are you sure that you're not using ADDED_TO_STAGE, which does not bubble? If you're using ADDED_TO_STAGE, then you need to set the useCapture flag to true to get it to fire for all children.

    If you want to involve RobotLegs in the process, probably the better way is to simply create a "marker" Class for each specific button that you want to have behave in a different way, then register a mediator for each Class that will manage the behvior. Robotlegs already has the hooks built in to listen for ADDED_TO_STAGE and do this.

    However, you could also consider using the Flash IDE for what it's for, which is putting stuff on stage. In that case, your GameBoard instance will be ready in the constructor of your main document Class for you to do whatever you want with it.

    MPO is that logic that is outside Gameboard shouldn't know or care how it works internally, and honestly it probably shouldn't even be GameBoard's responsibility to handle simple stuff like button over states and things. That should be up to the button itself. If the buttons don't need to toggle or anything beyond what SimpleButton handles, you can just declare the button instances as Button in the library instead of MovieClip and get all that stuff for free instead of coding it yourself.

    Part of being a good coder is in being able to figure out ways not to code everything.