• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Adding an event listener to a loader in Actionscript 3

I'm building an application in Flash CS5 which loads an external SWF and displays it on the stage. Once the external SWF is loaded and displayed on the stage, the user clicks through a series of buttons in the loaded SWF. The final button click removes the SWF from the stage and another object (this time from the library) is then displayed and the program moves on.

I've tried adding an event listener to the loader so that it can respond to the event REMOVED_FROM_STAGE. However when the external SWF is removed from the stage, nothing happens.

I've tried two aproaches but neither produce a result.

mcLoader.contentLoaderInfo.addEventListener(Event.REMOVED_FROM_STAGE, movieRemoved);

mcLoader.addEventListener(Event.REMOVED_FROM_STAGE, movieRemoved);

Can anyone help?
0
richo22
Asked:
richo22
  • 9
  • 8
  • 2
2 Solutions
 
blue-genieCommented:
how are you handling this part?
 The final button click removes the SWF from the stage

code snippet.
0
 
eXpertZeuSCommented:
What you need to do is to dispatch an event from the external swf.
Assuming that your external swf is named "myExternalClip.swf" and assuming that it contains a button named "ImDone" (the last button that you mentioned) you can do the following:

Go to the myExternalClip.fla, go to the actionscript editor and add normally an event listener for the button that you want to signal the end of this clip, in this case the ImDone button.

ImDone.addEventListener(MouseEvent.CLICK, superIamDone);

then add the superIamDone function (ofcourse you can name the function whatever you want:)  :

function superIamDone(event):void
{
      //enter here whatever code you need to do what the button does PLUS the code below.
      dispatchEvent(new Event("Done"));
}

this last line will dispatch an event similar to mouse or other events that can be "heard" by anyone listening for such an event.

Now go to the parent fla, the one that loads the external URL and add code that 1st will load the external clip and then it will do something when it will "hear" the Done message from this external clip.

Load the external clip as follows:
First define a couple of variables
var myLoader:Loader;
var currentMC: MovieClip;

So the myLoader will be a Loader object, and the currentMC a MovieClip object. Now you populate the loader first like this:

myLoader = new Loader();
myLoader.load("myExternalClip.swf");
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

now write code for the loadComplete function like this:

function loadComplete(event)
{
    currentMC  = event.target.content;  //you populate the currentMC
    addChild(currentMC); //you se it to stage
    currentMC.addEventListener("trainingDone", DoneHandler);//and you have this child wait for the   //"Done" message
      
}


and finally add the doneHandler function code:

function doneHandler(event):void
{
      removeChild(currentMC);
      //whatever else you want to happen after the removal of the external swf.
}

and you are done :)
0
 
richo22Author Commented:
Thanks for getting back to me. I've altered the code as you suggest. The external swf loads and performs OK, but when I hit the finish button the external swf is not removed from the stage. The parent application doesn't seem to be hearing the event from the external swf.

I've included the code below:

//This is the code on the finish button on the external SWF

finishButton.addEventListener(MouseEvent.CLICK, closeMovie);

function closeMovie(e:MouseEvent):void
{
      dispatchEvent(new Event("Done"));
}


//This is the code on the parent file

var mcLoader:Loader = new Loader();
var currentMC:MovieClip;
var mcUrl:URLRequest = new URLRequest("alien.swf");

mcLoader.load(mcUrl);
mcLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

function loadComplete(event)
{
      currentMC = event.target.content;      //you populate the currentMC
      currentMC.x = 0;
      currentMC.y = 0;
      addChild(currentMC);      //you set it to stage
      currentMC.addEventListener("Done", doneHandler);
}


function doneHandler(event):void
{
      removeChild(currentMC);
      //More things to happen here
}

Can you see why it might not be working?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
richo22Author Commented:
In answer to blue-genie's question, I was removing the external swf with the following code on its finish button:


finishButton.addEventListener(MouseEvent.CLICK, closeMovie)

function closeMovie(e:MouseEvent):void
{
      parent.removeChild(this);      
}
0
 
eXpertZeuSCommented:
add the following line here:

function doneHandler(event):void
{
      trace("I made it here"); //<----------------this line
      removeChild(currentMC);
      //More things to happen here
}

this line will let you know whether you ever enter in the function doneHandler. If you do you will see the message printed on the output tab.
If you do not, check for typos.
0
 
richo22Author Commented:
OK, the external swf is still not removed from the stage, so I've made up a small program to test the code. So this program contains only this code:

var mcLoader:Loader = new Loader();
var currentMC:MovieClip;
var mcUrl:URLRequest = new URLRequest("External.swf");
var startButton:StartButton = new StartButton();

startButton.x = 300;
startButton.y = 300;
addChild(startButton);

startButton.addEventListener(MouseEvent.CLICK, loadSWF);

function loadSWF(e:MouseEvent):void
{
      removeChild(startButton);
      mcLoader.load(mcUrl);
      mcLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

      function loadComplete(event)
      {
            currentMC = event.target.content;
            currentMC.x = 0;
            currentMC.y = 0;
            addChild(currentMC);
            currentMC.addEventListener("Done", doneHandler);
      }
}

function doneHandler(event):void
{
      trace("I made it to here");
      removeChild(currentMC);
}


And here's the relevant code on the Finish Button on the external swf, named "External.swf"

finishBtn_1.addEventListener(MouseEvent.CLICK, closeMovie);

function closeMovie(e:MouseEvent):void
{
      trace("The finish button has been clicked");
      dispatchEvent(new Event("Done"));
}

Here's what happens:

1.  "External.swf" loads OK and is placed on the stage.
2.  "External.swf' does everything it's supposed to do.
3.  When the finish button on "External.swf" is clicked, "The finish button has been clicked" is output, so    no problem there.

However, the program never moves into the function doneHandler, as no trace comes up "I have made it to here."

Thus there is a problem communicating the event "Done".

Can you see the problem or have any other ideas?

Thanks in anticipation.
0
 
blue-genieCommented:
if i may jump in. your code does work
check that you're not getting any compile errors.
I've just replicated your code and the trace ("i have made it to here") pops up
i've attached my sample.
dispatcher.zip
0
 
blue-genieCommented:
if i may jump in. your code does work
check that you're not getting any compile errors.
I've just replicated your code and the trace ("i have made it to here") pops up
i've attached my sample.
dispatcher.zip
0
 
blue-genieCommented:
argh sorry about the double post.
0
 
richo22Author Commented:
Thank you for jumping in! And for demonstrating that the code does in fact work. I was starting to worry about my sanity.

I've downloaded your file and run it on my computer, and it works. I then removed your buttons and substituted them for a couple of Illustrator buttons that I'm using, and it still works.

But when I run my original program, which consists of a movieclip made up of Illustrator vector files, I cannot get "I have made it to here". I'll deconstruct everything and build it back up to see if I can find an error.

Are you aware of Illustrator vector files causing unexpected errors in program operation?

And thanks again for testing the code.
0
 
richo22Author Commented:
I've done some work on this problem and I've come to the following conclusions.

1.  I can make up a main program and externalSWF program using simple buttons made in Flash and the code works perfectly.
2.  I can then substitute the simple Flash buttons for custom made Illustrator buttons and the code works perfectly.
3.  I then put the complete movieclip into the externalSWF file (using the custom Illustrator buttons that previously worked) and the code no longer works.

Does this make any sense?
0
 
blue-genieCommented:
can you upload the fla of your externalSwf.

i'm not sure i understand what you mean by "put the complete movieclip into the externalSWF"  if you're doing what i think you're doing, it might be a pathing issue, but upload it so we can have a look.
0
 
richo22Author Commented:
Thanks for your offer. I've uploaded the fla files.

Main Test 5.fla works OK.
Main Test 6.fla does not work.
Flash-Test.zip
0
 
blue-genieCommented:
i think the reason your code is not working is because you test6.fla doesnt' work
compile it and run it. are you not getting any error messages?



0
 
richo22Author Commented:
My Main Test 6.fla compiles and runs OK on my machine. No error messages when I compile it.

The Start, Next, Back and Again buttons all work fine. The Finish button also works, but I only get "Finish Button clicked". I don't get "I made it to here".

Have you been able to run Main Test 5.fla?
0
 
blue-genieCommented:
yeah its just your external files, odd.
make sure you've uploaded the right version.
0
 
richo22Author Commented:
I'll get back to this later today, but just so you can see how the swf are behaving, I've attached them.
ExternalSWFTest5.swf
ExternalSWFTest6.swf
Main-Test-5.swf
Main-Test-6.swf
0
 
blue-genieCommented:
that is odd, i don't have time to investigate right now, behind on some work, as soon as I load your files, the error pops up again, whether through a browser or in the IDE>
0
 
richo22Author Commented:
Thanks to both of you for your help. I still haven't been able to get my external swf to dispatch an event back to the main program, but I've picked up some very useful knowledge along the way. I have notice some of my movieclips that rely on imported Illustrator vector files sometimes do odd things.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 9
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now