Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
mmiller64

Eventlistener for a button in loaded SWF

Recommended Posts

Suppose I have the following loader:

var loader1:SWFLoader=new SWFLoader("myExternal.swf",{name:"myExternalSWF",container:this,onComplete:completeHandler});
loader1.load();

 

How would I set up an event listener to watch for a button press inside the loaded clip?

 

That way I've done this in the days before loaderMax was something like this:

var SWFLoader:Loader;
function loadSWF(url:String):void {
SWFLoader = new Loader();
SWFLoader.load(new URLRequest(url));
SWFLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, SWFLoaded);
}
function SWFLoaded(e:Event):void {
addChild(SWFLoader);
var myclip:MovieClip = new MovieClip();
myclip=MovieClip(SWFLoader.content);
myclip.myBtn.addEventListener(MouseEvent.CLICK, myBtnClick);
function myBtnClick( evt:MouseEvent ) {
	trace("ok");
}
}

 

Trouble is, I cant' figure out the loaderMax equivalent.

 

Thanks!

Mack

Link to comment
Share on other sites

The concept would be the same - listen for the COMPLETE event and then add your listener. Like:

 

var loader1:SWFLoader=new SWFLoader("myExternal.swf",{name:"myExternalSWF",container:this,onComplete:completeHandler});
loader1.load();

function completeHandler(event:LoaderEvent):void {
   var myClip:MovieClip = MovieClip(loader1.rawContent);
   myClip.myBtn.addEventListener(MouseEvent.CLICK, clickHandler);
}

function clickHandler(event:MouseEvent):void {
   trace("clicked");
}

 

If your button is on the root of the subloaded swf, you can even use the SWFLoader's getSWFChild() method to get the button by name, like loader1.getSWFChild("myButton") but if that's confusing, don't worry about it.

Link to comment
Share on other sites

Ah...it was the loader1.rawContent that I needed. I knew the solution would be short and sweet. That's what I love about greensock classes.

 

Thanks, Jack!

Link to comment
Share on other sites

  • 1 month later...

Will the rawContent method help me setting up buttons scripts for the SWFLoaded or ImageLoaded items?

 

I'm trying to add EventListerers to loaded images and swfs that I loaded via the queue

 

var content:ContentDisplay = loader.SWFcontent;

 

name of the loader.SWFcontent.name of the movieclip.addEventListener(MouseEvent.CLICK, btnClickAbout);

 

Does it go like this? Can a developer create an image/non SWF version of the SWFLoader in AS3? Can this be done via the Queue:

 

var loader1:ImageLoader=new ImageLoader("img/button.png",{name:"myMenu_Mome",container:this,onComplete:completeHandler});
loader1.load();

function completeHandler(event:LoaderEvent):void {
   var myClip:MovieClip = MovieClip(loader1.rawContent);
   myClip.myBtn.addEventListener(MouseEvent.CLICK, clickHandler);
}

function clickHandler(event:MouseEvent):void {
   trace("clicked");
}

Link to comment
Share on other sites

I'm totally confused - if you're loading a PNG image, how could it have a button instance nested inside of it? Did you mean a swf?

 

If you want to add a listener to a nested object inside the child swf that you loaded, you should be able to do something like:

 

var loader:SWFLoader = new SWFLoader("child.swf", {onComplete:completeHandler});
loader.load();

function completeHandler(event:LoaderEvent):void {
   var btn:DisplayObject = MovieClip(loader.rawContent).myButton;
   btn.addEventListener(MouseEvent.CLICK, myListener);
}

 

Does that answer your question?

Link to comment
Share on other sites

It does.

 

I can use SWFs instead of PNGs (the assets are text based) I was simply wondering the correct path schema for making any imported object into a clickable button. I guess it's as much about making any imported object into a button as well as making a nested MC of the same imported SWF into a button.

Link to comment
Share on other sites

If you simply want to make the whole thing clickable, that's even easier. In fact, it's one of the reasons SWFLoaders, ImageLoaders, and VideoLoaders create a ContentDisplay object immediately. You can put it wherever you want and attach event listeners before the content even loads.

 

var image:ImageLoader = new ImageLoader("photo.jpg", {width:100, height:100});
addChild(image.content);
image.content.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
   trace("clicked "+event.target.loader+" content");
}

Link to comment
Share on other sites

Coolness. Thanks a ton for helping me be clearer with my questions.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×