actionscript 3 - How to pass object, MouseEvent.CLICK and function to trigger -


i want pass function object, const of type mouseevent.click , function trigger. in case:

my class assistant:

    public static function addeventlistenerto(obj:object, mouseeventconst:string, functintotrigger:function) { obj.addeventlistener(mouseeventconst, functintotrigger:function); } 

and class engine invokes

assistant.addeventlistenerto(deck,"mouseevent.click",showobject); 

please give me advice how make work. thanks.

in code provide there 1 compiler error (the 1 tahir ahmed pointed in second comment).

fixing removing second :function in first code block:

public static function addeventlistenerto     (obj:object, mouseeventconst:string, functintotrigger:function)  {     obj.addeventlistener(mouseeventconst, functintotrigger); } 

will let code compile. (i wrapped method signature avoid scrollbar, not required make compile.)

the other major problem configuration error (or maybe typo): 1 mouseevent.click. (the 1 tahir ahmed pointed in first comment)

looking @ documentation defined have value "click" (a string literal following as3 convention of lowercase constant name). pass method can either put in reference constant writing mouseevent.click (without "s around it) or reach same goal passing value writing "click". using reference prevent mistyping because compiler checks it, first approach should preferred.

so calling method should this:

assistant.addeventlistenerto(deck, mouseevent.click, showobject); 

if want know why version didn't work should read simple introduction as3 events , eventdispatchers. short hint: if deck dispatch event has type property set "mouseevent.click" listener fired.

while @ it, improve quality of code major things:

the first 1 avoiding getting runtime errors , prefering compile time errors: not every instance of type object has method called addeventlistener. in current code, when pass instance assistant.addeventlistenerto first parameter, doesn't have method (e.g. {} or instance of type array), error thrown while swf displayed , might stop displaying , might show error message user. if type of parameter ieventdispatcher instead, compiler tell you passed incompatible instance.

the second 1 names , conventions, helps other developers read code (an having more fun helping you).

  • what called mouseeventconst called event type in as3, provides better name parameter, being string nobody stops passing contants of other event types event
  • the functiontotrigger called listener (or event listener)
  • the first letter of parameter names should lower case

so if have written static method this:

import flash.events.*;  public class assistent{      public static function addeventlistenerto         (dispatcher:ieventdispatcher, eventtype:string, listener:function)     {         dispatcher.addeventlistener(eventtype, listener);     } } 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -