Jump to content

addEventListener and function parameters


astralaaron

Recommended Posts

Posted

hi I am trying to understand why the following:function placeIt(thenum){ trace("number is: " + thenum);}EXAMPLE 1:-------var thenumber:int = 2;timer.addEventListener(TimerEvent.TIMER, function(){ placeIt(thenumber) });--------------------EXAMPLE 2:-------timer.addEventListener(TimerEvent.TIMER, function(){ placeIt(2) });--------------------does anyone understand why in example 1 that the parameter doesn't pass through? the trace outputs "number is: 0"but in example 2 trace outputs "number is: 2"

Posted

Is the value of that variable changing anywhere? It looks like it's changing, otherwise the output would be undefined instead of 0.

  • 4 weeks later...
Posted

my only guess is that the context within addEventListener is the object's context, so it can only find object methods and properties. Since thenumber isnt a class property, it cant find it. Im having weird issues with the addEventListener method myself, coming from regular JavaScript.

  • 3 months later...
Posted

yeah, actually it will be like this private function test():void { var thenumber:int = 2; timer.addEventListener(TimerEvent.TIMER, nonamed);} private function nonamed():void { placeIt(thenumber); // now thenumber is unknown} :rolleyes:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...