Question

I am trying to to attach a function with parameters to the timer but it says "unrelated type function" is there any way to get around this??

code example:

var redoTimer:Timer = new Timer(50);

redoTimer.addEventListener(TimerEvent.TIMER, saySomething("helloo"));
redoTimer.start();

this wont seem to work but is there a way to pass on arguments???

thanks Matthy

Was it helpful?

Solution

You can't really do that, the closest equivalent would be using a small inline function to wrap your own function:

var redoTimer:Timer = new Timer(50);

redoTimer.addEventListener(TimerEvent.TIMER, function(e:Event):void { saySomething("helloo") } );
redoTimer.start();

OTHER TIPS

The second parameter to the addEventListener function should be a function. Your code actually executes the method saySomething("helloo") and tries to use its return value as the second parameter to the addEventListener and hence the error.

Also, the event listener function should accept one and only one argument of type flash.events.Event. They can have optional arguments with default values if you want to call them explicitly from your code though.

This is possible with a roundabout approach. For the event handler, use a function that returns a nested anonymous function.

private var textFieldA:TextField = new TextField;
private var textFieldB:TextField = new TextField;

public function setParameterizedTextWhenTextFieldsAreClicked ():void {
    addChild(textFieldA);
    textFieldA.text = 'Text field A';
    textFieldA.addEventListener(MouseEvent.CLICK, showCustomMessage("One"));

    addChild(textFieldB);
    textFieldB.text = 'Text field B';
    textFieldB.y = 20;
    textFieldB.addEventListener(MouseEvent.CLICK, showCustomMessage("Two"));
    // NOTE: We must use strongly referenced listeners because weakly referenced 
    // listeners **will get garbage collected** because we're returning
    // an anonymous function, which gets defined in the global namespace and  
    // thus, the garbage collector does not have anything pointing to it.
}

private function showCustomMessage (message:String):Function {
    // NOTE: You can store the following function to a class variable
    // to keep it in memory, which would let you use weakly referenced 
    // listeners when using this as an event handler. Many people 
    // would find that awkward. I would discourage that.
    return function (e:MouseEvent):void {
        var textField:TextField = e.target as TextField;
        textField.text = message; // "message" argument is available because 
                                  // this function's scope is kept in memory.
    }
}

Bear in mind, the use of anonymous functions and reliance on function scope being kept in memory seem to present garbage collection complications.

Out of the box: it only takes 2 extra lines of elegant code to solve this ancient puzzle.

stage.addEventListener(MouseEvent.CLICK, onClick(true, 123, 4.56, "string"));
function onClick(b:Boolean, i:int, n:Number, s:String):Function {
  return function(e:MouseEvent):void {
    trace("Received " + b + ", " + i + ", " + n + " and " + s + ".");
  };
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top