PDA

View Full Version : Mouse Events and Button Mode in externally loaded swfs



Tsukiyomi
March 31st, 2009, 07:46 PM
I've looked around on this forum and others looking for an answer to this and I haven't found anything thats worked. If I missed the answer on here I apologize for the repeating topic.

I have an external swf I created, I used the Document Class to place assetts from the library onto the stage. I have a few Movie Clips I set the buttonMode to true on and added a few MouseEvents for when they're clicked on or rolled over to load content and make a few draggable.

It works perfectly in that swf, but since most of my assets are in the library and exported on Frame 1 I can't really use an internal preloader on it. So what I did was create an external swf that all it is is a loader that I add to the stage that loads the first swf I created.

That works perfectly for getting it on the stage, but the Movie Clips no longer are recognized as buttons and the mouse events aren't working anymore.

Is there anything I can do to change this?

Here is the code I'm using to load the external swf:


var a:URLRequest = new URLRequest("timeline2.swf");
var b:Loader = new Loader();
b.load(a);
addChild(b)Simple enough.

This is the code I used to add listeners in the external swfs document class:



stage.addEventListener(MouseEvent.MOUSE_UP, stopAllDrags);
//
btn_1968.addEventListener(MouseEvent.CLICK, setYear);
btn_1968.xIndex = 0;
btn_1968.buttonMode = true;
//
btn_1970.addEventListener(MouseEvent.CLICK, setYear);
btn_1970.xIndex = 1;
btn_1970.buttonMode = true;
//
btn_1979.addEventListener(MouseEvent.CLICK, setYear);
btn_1979.xIndex = 2;
btn_1979.buttonMode = true;
//
btn_1979b.addEventListener(MouseEvent.CLICK, setYear);
btn_1979b.xIndex = 3;
btn_1979b.buttonMode = true;
//
btn_1983.addEventListener(MouseEvent.CLICK, setYear);
btn_1983.xIndex = 4;
btn_1983.buttonMode = true;
//
btn_1985.addEventListener(MouseEvent.CLICK, setYear);
btn_1985.xIndex = 5;
btn_1985.buttonMode = true;
//
btn_1991.addEventListener(MouseEvent.CLICK, setYear);
btn_1991.xIndex = 6;
btn_1991.buttonMode = true;
//
btn_1995.addEventListener(MouseEvent.CLICK, setYear);
btn_1995.xIndex = 7;
btn_1995.buttonMode = true;
//
btn_2001.addEventListener(MouseEvent.CLICK, setYear);
btn_2001.xIndex = 8;
btn_2001.buttonMode = true;
//
btn_2008.addEventListener(MouseEvent.CLICK, setYear);
btn_2008.xIndex = 9;
btn_2008.buttonMode = true;Any help would be greatly appreciated.

Xurk
April 1st, 2009, 03:41 AM
Perhaps it might work if you added an event listener to the instance of Loader which calls to a function when loading is complete? In that function, you could use a LocalConnection to execute a function in the externally loaded SWF, which runs the code you listed in your second code box above. I'm not 100% sure if that will work, but at least you know that the code will be executed and at which time it will be executed :)

So, maybe something like this:

var b:Loader = new Loader();
b.contentLoaderInfo.addEventListener(Event.COMPLET E, fSwfLoaded);

var a:URLRequest = new URLRequest("timeline2.swf");
b.load(a);
addChild(b);

var lcOutgoing:LocalConnection = new LocalConnection();

public function fSwfLoaded(e:Event):void {
lcOutgoing.send("lc_datatransfer", "fInitButtons");
}
Then make sure you don't forget to include the following in the external SWF ["timeline2.swf"]:

var lcIncoming:LocalConnection = new LocalConnection();
lcIncoming.connect("lc_datatransfer");
lcIncoming.client = this;
Finally, put all of the code from the second code block of you post in a function called "fInitButtons":

public function fInitButtons():void {
stage.addEventListener(MouseEvent.MOUSE_UP, stopAllDrags);

btn_1968.addEventListener(MouseEvent.CLICK, setYear);
btn_1968.xIndex = 0;
btn_1968.buttonMode = true;

........
}

I hope this helps you out somewhat!