r3x
r3x

Reputation: 2147

Accessing a button inside a loaded swf file?

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

Answers (1)

Josh at The Nerdery
Josh at The Nerdery

Reputation: 1397

You've got 2 problems here...

  1. You're not waiting for the swf to load before you attempt to access the button.
  2. Your method for accessing the button is incorrect.

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

Related Questions