Tyilo
Tyilo

Reputation: 30102

Only add child to current frame

Is it possible to add a child to the current frame only?

I know you can just remove/hide them, and generate/show them again.

But isn't there an easier way?

Upvotes: 1

Views: 1880

Answers (2)

Mattias
Mattias

Reputation: 3907

Old school way:

If you have a timeline you can add a MovieClip as a holder only on that frame. The holder clip only exists on that frame. Once you reach that frame you add the real clip to the holder display list using code:

holder.addChild(new special_clip())

That way it will be removed when you change to another frame. It will however need to be re-created the next time you get to the frame.

Upvotes: 2

Marty
Marty

Reputation: 39456

package
{
    import flash.display.MovieClip;

    public class MyChild extends MovieClip
    {
        // Values
        private var _containingFrame:int = 1;

        /**
         * ...
         * @param e Event.ENTER_FRAME
         */
        private function _check(e:Event):void
        {
            if(parent)
            {
                if(parent.currentFrame != containingFrame)
                {
                    removeEventListener(Event.ENTER_FRAME, _check);
                    parent.removeChild(this);
                }
            }
        }

        /**
         * Getters & Setters
         */
        public function get containingFrame():int{ return _containingFrame; }
        public function set containingFrame(n:int):void
        {
            _containingFrame = n;
            addEventListener(Event.ENTER_FRAME, _check);
        }
    }
}

Just make this the base class of your children. Now when you add children (either via ActionScript or dragging onto the stage and assigning an instance name) you can just go:

myChild.containingFrame = currentFrame;

I'm not confident that there's a simpler way.

Upvotes: 1

Related Questions