views:

1353

answers:

2

Basically I have this function:

private function clickURL(url:String):Function{
    trace("Function has been instantiated");
    return function(event:MouseEvent):void{
     trace("Function has been run");
     ExternalNavigation.newBrowserWindow(url);
    }
}

The purpose of this function is to take an url, put the url in another function and pass that function back so I can just type:

urlButton.addEventListener(MouseEvent.CLICK, clickURL("http://test.com"));

The function clickURL will return the function with the event parameter back to the addEventListener function. In that way I specify what url which will be opened when you press the button.

Here's the output of what happens when you use it:

//Function has been instantiated

The internatl function never gets run when you click the button. So I thought I'd try it out with a fake event to be sure I didn't miss anything.

var clickTest:Function = clickURL("http://stackoverflow.com");
clickTest(new MouseEvent(MouseEvent.CLICK));

Here's the output:

//Function has been instantiated
//Function has been run

As you can see, both functions are run. Have anyone got an idea to why this is working and not with the addEventListener?

Regards Z

A: 

Hi there, one problem I see right away is that the variable url only exists when the function is initialized as a listener, but not when it is triggered through the mouse event. So it will actually be receiving a null value on execution. (or throw an error)

As you demonstrated, the listener is setup and executing as expected when the event is fired, so if your button isn't triggering the event properly, perhaps there is an issue with the button?

Tyler Egeto
sorry to contradict, but the variable url is available, when the function is called, because as3 supports closures ... you can read more here, for example: http://www.transcendentaxis.com/dthompson/blog/archives/19 .... greetz
back2dos
No need to apologize, I totally did not know that. Thanks for the link! That's a very important thing to know, it could cause all kinds of garbage collection head aches. Thanks again.
Tyler Egeto
There is nothing wrong with the button, though. It works if I use a normal callback function.
A: 

this should work without a problem ...

do you get any traces if you do urlButton.addEventListener(MouseEvent.CLICK, trace); and click the button?

edit:

i really can't reproduce it ... here's some code that does work perfectly:

package {
    import flash.display.*;
    import flash.events.*;
    public class Main extends Sprite {
     private var urlButton:Sprite;
     public function Main():void {
      this.addChild(this.urlButton = new Sprite());
      this.urlButton.graphics.beginFill(0xFF00FF);
      this.urlButton.graphics.drawRect(0, 0, 200, 50);
      urlButton.buttonMode = true;
      urlButton.addEventListener(MouseEvent.CLICK, clickURL("http://test.com"));
     }
     private function clickURL(url:String):Function{
      trace("Function has been instantiated");
      return function(event:MouseEvent):void{
       trace("Function has been run");
      }
     }  
    }
}

could you maybe post/upload a minimal setup, where it does not work?

back2dos
[MouseEvent type="click" bubbles=true cancelable=false eventPhase=3 localX=-7.5 localY=6.65 stageX=496 stageY=435 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]That's what I get when I trace it.
I can't reproduce the error either...i did a simple set up and it is working for me. Created a Main file, added a sprite, add mouseevent listener, and literally copied the clickURL method. It works as expected.
goliatone