• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Access a parent movie (root) for and attached movie

Participant ,
Nov 17, 2012 Nov 17, 2012

Copy link to clipboard

Copied

I attach a movie with:

exitapp_btn.addEventListener(MouseEvent.MOUSE_DOWN, fexitapp);

function fexitapp(event:MouseEvent) {

    var mc:exitapp_mc=new exitapp_mc();

    mc.x=320;

    mc.y=210;

    addChild(mc);

}

I would like the loaded movie to exit and move the parent movie to frame 4 (Is it possible?) . I currently have this:

import flash.system.fscommand;

nope_mc.addEventListener(MouseEvent.CLICK, cancelunloadexitb);

yep_mc.addEventListener(MouseEvent.CLICK, unloadexitb);

function cancelunloadexitb(e:MouseEvent):void {

    this.parent.removeChild(this);

}

function unloadexitb(e:MouseEvent):void {

    this.parent.removeChild(this);

  //I guess the code woul go here

}

TOPICS
ActionScript

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 17, 2012 Nov 17, 2012

Sure, but I'd still say stick to the "parent controls the child" concept.

Parent frame 1 with child (library clip) on screen:

stop();

// child calls this passing a reference of itself

function RemoveFunction(clip:Object):void

{

     // any child calling this will be removed and advance

     // this parent timeline to frame 4

     removeChild(DisplayObject(clip));

     gotoAndStop(4);

}

parent frame 4:

stop();

trace("Parent frame 4");

library MovieClip frame 90:

stop();

trace("Child now trying to run RemoveFunc

...

Votes

Translate

Translate
LEGEND ,
Nov 17, 2012 Nov 17, 2012

Copy link to clipboard

Copied

As in the real world, parents should control children, or at least collaborate. You should put a listener on your child and send the parent an event when you wish that to happen. The child removing itself from a parent can become a debugging nightmare.

You don't NEED to use an event for this but it can centralize a lot of what happens inside your app so I prefer this route. Otherwise yes you may run any public method on a parent from a child via MovieClip(this.parent).functionName();.

I'll demo the event version however:

Demo Source Here

Main FLA frame 1:

// import my child

import ChildClass;

import flash.events.Event;

stop();

trace("Creating child class (drawing a red circle for a visual)");

// I made it extend sprite because I assume yours is visual

// and it automatically supports events

var myChild:ChildClass = new ChildClass();

// add listener for my custom event

myChild.addEventListener("RemoveMe",_removeHandler);

// add to display list so event happens

addChild(myChild);

// function which receives event from child

function _removeHandler(e:Event):void

{

    trace("Parent received event: " + e.type);

   

    if (e.type == "RemoveMe")

    {

        // remove child, go to frame 4

        removeChild(DisplayObject(e.currentTarget));

        gotoAndStop(4);

    }

}

Main FLA frame 4:

trace("Parent - I am on frame 4, there should be no circle");

ChildClass.as external class file:

package 

{

    import flash.display.Sprite;

    import flash.utils.setTimeout;

    import flash.events.Event;

   

    public class ChildClass extends Sprite

    {

        public function ChildClass()

        {

            trace("Child created, drawing circle, waiting 5 seconds to send event to parent");

           

            // draw a quick red circle

            var c:Sprite = new Sprite();

            c.graphics.beginFill(0xFF0000,1);

            c.graphics.drawCircle(52,52,50);

            c.graphics.endFill();

            addChild(c);

           

            setTimeout(_sendEvent, 5000);

        }

       

        protected function _sendEvent():void

        {

            // comm with parent who's listening for this

            this.dispatchEvent(new Event("RemoveMe"));

        }

    }

}

Trace output:

Creating child class (drawing a red circle for a visual)

Child created, drawing circle, waiting 5 seconds to send event to parent

Parent received event: RemoveMe

Parent - I am on frame 4, there should be no circle

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 17, 2012 Nov 17, 2012

Copy link to clipboard

Copied

Hello Sinious:

Thnk you for your time.

Wow. that sure is a long route to accomplish somethig that seems very simple (removing an attached clip and pushing the main movie one frame)

I would not like to use an external class in this project. Is it possible to accomplish this through a shorter route? Any shortcut way to do this?

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2012 Nov 17, 2012

Copy link to clipboard

Copied

Sure, but I'd still say stick to the "parent controls the child" concept.

Parent frame 1 with child (library clip) on screen:

stop();

// child calls this passing a reference of itself

function RemoveFunction(clip:Object):void

{

     // any child calling this will be removed and advance

     // this parent timeline to frame 4

     removeChild(DisplayObject(clip));

     gotoAndStop(4);

}

parent frame 4:

stop();

trace("Parent frame 4");

library MovieClip frame 90:

stop();

trace("Child now trying to run RemoveFunction() on parent");

// send reference of this clip to function

Object(this.parent).RemoveFunction(this);

I'm using very generic types like Object and DisplayObject so it works from Sprites, MovieClips, Loaders and anything else that extends a DisplayObject.

trace:

Child loaded, playing..

Child now trying to run RemoveFunction() on parent

Parent frame 4

Example File

If you really want the child to do it there's a second file (#2) that has the child do all the work.

The child MUST exist on the frame you want the parent to land on to remove itself or perform code.

Child on frame 90:

stop();

trace("Child now changing parent to frame 4, then removing self");

MovieClip(this.parent).gotoAndStop(4);

MovieClip(this.parent).removeChild(this);

Edit:

Note: If you load a child .swf via a Loader you'll need to consider that the clip is nested inside the .content property of that loader, making you have to parent out a bit more. Like if your code in your original post was from a SWF loaded via a Loader then to access the parent:

myBtn.addEventListener(MouseEvent.CLICK, _removeThisClip);

function _removeThisClip(e:MouseEvent):void

{

     // I'm inside a loader, double parent out

     MovieClip(this.parent.parent).gotoAndPlay(4);

     MovieClip(this.parent.parent).removeChild(this.parent);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 17, 2012 Nov 17, 2012

Copy link to clipboard

Copied

Sinious THAN YOU for your time and great examples. Based on your examples I managed to implement the solution.

Thank you

Att.,

Edwin

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2012 Nov 17, 2012

Copy link to clipboard

Copied

LATEST

You're welcome and good luck!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines