Listen events from loaded SWF?
I've a simple application with one movie loading another SWF in the same domain. I can access vars and functions in the loaded SWF but can't listen events from a button; receiving the run-time error: Error #1009: Cannot access a property or method of a null object reference
The Linkage properties for the button are set
Main
var assetLoader:Loader = new Loader();
assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadingComplete);
asset开发者_Python百科Loader.load(new URLRequest("home.swf");
function loadingComplete(evt:Event):void {
...
var asset:MovieClip = assetLoader.content as MovieClip;
asset.homeTrace("function in loaded SWF");
trace("var in loaded SWF:", asset.lastFrame);
// Error #1009
asset.enterApp.addEventListener(MouseEvent.CLICK, homeButtons);
...
}
Home
var lastFrame:Boolean = false;
function homeTrace(p1:String) {
trace(p1);
}
Thanks in advance
I've implemented a solution, not what I wanted but is working fine. As i'm not able to add listeners for buttons in loaded movie, I added a general one and used a switch to discriminate the sender object:
asset.addEventListener(MouseEvent.CLICK, homeButtons);
...
function homeButtons(evt:MouseEvent):void {
switch(evt.target.toString()) {
case "[object Send]":
nextScreen(evt);
break;
case "[object Enter]":
tmpEnter(evt);
break;
}
}
You post a regular solution, but if you'll look deeper you could find solution how to add event listeners here and read about whole architecture here.
Regards
Eugene
The problem could be that you are casting the 'asset' as a MovieClip
, it could well be a Sprite
. Best to put the whole thing in a try .. catch
or if
just in case anyway, and cast it to the safe *
type to avoid compile time errors (or if you wanted to go the whole home use an interface and merge the two applicationDomain
contexts, but thats an expansive topic)
function loadingComplete(evt:Event):void {
...
var asset:* = assetLoader.content;
if(asset != null){
try{
connectModule(asset);
}catch(err:Error){
trace("Error accessing module functions", err.getStackTrace());
}
}else{
trace("No module");
}
}
function connectModule(module:*):void{
trace("Using typeof", typeof(module), "using constructor", module.constructor, "has the function?", module['homeTrace'] != null);
module.homeTrace("Function load in swf");
...
}
The trace
statements in connectModule
should give you some extra debugging information to help you determine what type of object is loaded and whether or not it has the function you are attempting to access.
Let me know if this works for you.
精彩评论