Solved

Adding an event listener to a loader in Actionscript 3

Posted on 2010-08-27
19
238 Views
Last Modified: 2013-11-11
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
Comment
Question by:richo22
  • 9
  • 8
  • 2
19 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 33548447
how are you handling this part?
 The final button click removes the SWF from the stage

code snippet.
0
 
LVL 1

Accepted Solution

by:
eXpertZeuS earned 250 total points
ID: 33548500
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
 

Author Comment

by:richo22
ID: 33548728
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
 

Author Comment

by:richo22
ID: 33548732
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
 
LVL 1

Expert Comment

by:eXpertZeuS
ID: 33550010
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
 

Author Comment

by:richo22
ID: 33552397
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 33556347
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
 
LVL 39

Assisted Solution

by:blue-genie
blue-genie earned 250 total points
ID: 33556349
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 33556351
argh sorry about the double post.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:richo22
ID: 33556484
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
 

Author Comment

by:richo22
ID: 33557312
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 33557579
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
 

Author Comment

by:richo22
ID: 33564388
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 33565442
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
 

Author Comment

by:richo22
ID: 33566446
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 33566599
yeah its just your external files, odd.
make sure you've uploaded the right version.
0
 

Author Comment

by:richo22
ID: 33572526
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 33574711
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
 

Author Closing Comment

by:richo22
ID: 33605420
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now