Skip to main content
Inspiring
October 24, 2009
Question

How do I interact with a loaded swf?

  • October 24, 2009
  • 3 replies
  • 1721 views

Hi,

I have a "lesson" that I'm building that will display a series of "pages" based on an XML file. I have simple navigation buttons that work to page through the XML file. but I'd like to use load an external SWF file with buttons to use as my navigation panel. Everything is AS 3.0. The code to load the navigation movie looks like this:

// load navigator
   navLoader = new Loader;
   navLoader.contentLoaderInfo.addEventListener(Event.INIT, handleInit);
   navLoader.x = 50;
   navLoader.y = 200;
   lessonSprite.addChild(navLoader);
   navLoader.load( new URLRequest("Navigator1.swf"));

The loaded navigation movie has two buttons. How do I add an event listener to those buttons so that they call a function in the parent movie so I can detect when the user clicks on the button named "GoNext" in the loaded movie?  I've tried several ways but I've had no luck. Could someone please point me in the right direction? This navigation panel is only one application of this sort of communication that I'll need in other parts of this type of lesson so I really need to figure out how to detect user interaction interaction with the loaded movie. Guess I need to know how to call a function in the parent movie from the loaded movie.

Thanks,

Peter H.

This topic has been closed for replies.

3 replies

October 24, 2009

Hey Pete,

The answer that you seek and the simplest one as well will be from the MouseEvent. that  you will most likely have applied to your loaded swf.

As long as both swfs are in the same domain, you wont have any issues with security and therefor, the MouseEvent. which bubbles up, will be able to reach your swf file which has loaded it.

You can then write your code in your loaded swf to simply listen to the loadedMovieClip  by targeting the contents of navLoader just like this,

navLoader.content.addEventListener(MouseEvent.MOUSE_DOWN,onDown)

protected function onDown(me:MouseEvent){

}

now providing you have given your two buttons names in your loaded swf, you will be able to perform addition tasks by receiving the name of the target.

protected function onDown(me:MouseEvent){

var btnClip:Movie= me.target as MovieClip

          var btnName=btnClip.name

if(btnName==(button1)){

     //do the following

}else{

       //it was button two

}

}

button1 was just a random name in this case .

This will do the trick

Pete47Author
Inspiring
October 24, 2009

iFezec

That is an interesting approach. If I understand it, you are saying I can basically attach a generic event listener to the loaded movie and listen for all mouse events. In your example, it is specifically listening for the MOUSE_DOWN event.

In this case, any clickable object in the navigation movie would generate a MOUSE_DOWN event that I could respond to in the parent movie.

I'm going to give that a try later today to see how it behaves. It could be very useful in another area of this project.

Thank you!

PeterH

Inspiring
October 24, 2009

You didn't understand me. Accessing the clip by name inside another clip is not a good idea because if you ( in later stage of developing ) deside to change the name of the clip a code will stop working.

Better way is to have a class in the loaded clip that rise an event and all other clips listen for this event

Pete47Author
Inspiring
October 24, 2009

Ned and Kalisto,

Thank you both for your replies. In this case, they both worked. I was trying to use Ned's approach last night but couldn't for some reason get the syntax right.

However, I learned about casting to a movieClip and using the getChildByName function when I used Kalisto's  approach.

Wonderful! My new function that works as desired appears below.

public function handleInit(_event:Event){
   var navTool:* = navLoader.content;
  
   //This worked:
   //MovieClip(navTool).getChildByName("GoNext").addEventListener(MouseEvent.CLICK, navButtonClicked);
  
   //This worked:
   navTool.GoNext.addEventListener(MouseEvent.CLICK, navButtonClicked);
  }

Inspiring
October 24, 2009

Hi,

I'm glad that I was able to help you. Only to note that is a good idea to work with the enabled strict mode of compiller.

Inspiring
October 24, 2009

Hi, you can rise an event from loaded swf and catch it into loader class

Pete47Author
Inspiring
October 24, 2009

Ok, thanks thanks for the reply. That is good to know.

But now, how? Can I reference my "GoNext" button directly through dot notation and assign an event listener to the button with a corresponding function in the parent movie? That would be my preferred method but if that is possible, I've not been able to figure out the syntax.

Or must I dispatch my own custom event somehow?

Thanks

Peter

Inspiring
October 24, 2009

Yes, something similar

Use this an example

var ldr: Loader = new Loader()
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete);
addChild( ldr );
ldr.load( new URLRequest( "btn.swf" ) );
function onComplete( evnt )
{
MovieClip( ldr.content ).getChildByName( "btn").addEventListener( MouseEvent.CLICK, onClick );
}

function onClick( evnt )
{
trace("click");
}

btn.swf have a instanse of button named 'btn'

Note 2 things

     1. Casting to movieClip of content.

     2. Accessing the object in stage is with method getChildByName

This is because compiler in strict mode will give an errors

BUT using this aproach is not a good idea - will work but is coding hell. better way is to dispatch an event from loaded clip using dispatch method and catching it into loader clip as I say in my first post