Note that there are some explanatory texts on larger screens.

plurals
  1. POTo pass a parameter to event listener in AS3 the simple way... does it exist?
    primarykey
    data
    text
    <p>Expected / pseudo example:</p> <pre><code>stage.addEventListener(MouseEvent.CLICK, onClick.someWayToPassParameters(true, 123, 4.56, "string")); function onClick(e:MouseEvent):void { trace("Received " + someWayToRetrieveParameters().b/i/n/s + "."); } </code></pre> <p>For many years (3~4), on every website, forum, blog, wherever I searched, people tell me there's no simple way to do this. They usually suggest to:</p> <ul> <li><p><em>Add the listener to a dynamic object, where you can set the value to an extra property and reference it (e.target.property / e.currentTarget.property) in the function.</em></p> <p>Not all classes are dynamic. It won't work on a Sprite, for example.</p></li> <li><p><em>Extend the class of the object with a custom class to add property or to just make it dynamic.</em></p> <p>You'll have to create a whole new tweak class everytime.</p></li> <li><p><em>Use an anonymous function as the event handler.</em></p> <p>There's no reference (and it's ugly). To remove the listener to free resources you're forced to do it from inside the function itself with arguments.callee.</p></li> <li><p><em>Call another function, using the parameter, inside the event handler.</em></p> <p>And where in the event handler call does the parameter go?</p></li> <li><p><em>Keep the event handler in the same scope as the parameter.</em></p> <p>Breach to a total semantic mess.</p></li> <li><p><em>Encapsulate both event handler definition and addEventListener call in a function receiving the target and parameters.</em></p> <p>It can mix scopes, but it's a close one. You have to be careful, though.</p></li> </ul> <p>...Among many other suggested workarounds.</p> <p>All I want is just to <strong>pass an argument</strong> to the event handler so I can use it inside its function, just like any normal function!</p> <p>Am I asking for too much?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload