Reputation: 2147
This might be a simple question, but am trying to access a button inside an external swf file that I loaded. Problem is every time I try to access this button I get an error saying that it's null!
Any ideas why this is happening? I tried searching for similar posts and I did find some, but this problem still persists
Here is my code:
public class DocumentClass extends Sprite
{
public var loader:Loader;
public var swfFile:URLRequest;
public var container:MovieClip;
public function DocumentClass()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
loader = new Loader();
swfFile = new URLRequest("swfs/TestScreen.swf");
loader.load(swfFile);
addChild(loader);
button.addEventListener( MouseEvent.CLICK, onClickReturnMainMenu ); // PROBLEM HERE
}
private function onClickReturnMainMenu( event:MouseEvent ):void
{
trace("TEST-TEST");
}
Thanks =D
EDIT: here is the edited code
public class DocumentClass extends Sprite
{
public var loader:Loader;
public var swfFile:URLRequest;
public var container:MovieClip;
public function DocumentClass()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
loader = new Loader();
swfFile = new URLRequest("swfs/TestScreen.swf");
loader.load(swfFile);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
}
private function onClickReturnMainMenu( event:MouseEvent ):void
{
trace("ASDSADSADSAD");
//dispatchEvent( new ButtonEvent( ButtonEvent.MAINMENU ) );
}
private function onLoadComplete(evt:Event):void
{
addChild(loader);
var button:SimpleButton = loader.content.button
button.addEventListener(MouseEvent.CLICK, onClickReturnMainMenu);
}
Now this should work, code-wise I think its correct. The problem is that loader cant find the required button. There is indeed a button in the loaded movieClip with an instance name of "button", I double checked that...Any suggestion guys?
EDIT2 finally got it working thanks to this http://www.kirupa.com/forum/showthread.php?301313-Accessing-MC-instances-inside-loaded-SWF basically i was just a layer too high and this is why i couldn't access anything...SO STUPID OF ME...well atleast its over now. Thanks for the help guys =D
Upvotes: 1
Views: 1204
Reputation: 1397
You've got 2 problems here...
You first need to add an Event.COMPLETE
listener to loader.contentLoaderInfo
:
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
Then you need to make an event handler:
protected function onLoadComplete(evt:Event):void
{
var button:SimpleButton = loader.content.button;
button.addEventListener(MouseEvent.CLICK, onClickReturnMainMenu);
}
That should do it for you, assuming your button is in the root of the loaded movie with an instance name of "button".
Upvotes: 3