Tutorials
How to Pass Additional Parameters to AS3 Event Handler Functions
I've recently made the leap to AS3, and one of the very first questions I wondered about was how I could pass extra arguments to event listener functions. From what I understand, event listener functions in AS3 can only have one parameter, which is for the event object that gets passed when the event is triggered. I did some searching and found an article by Rich Schupe that explains a few different methods on how one can pass arguments with events in AS3. He focuses mainly on teaching the reader how to create a custom event class in order to address the issue at hand. But as I've mentioned earlier, he does provide other methods as well (that do not require the creation of a custom class). And if you continue reading through the article's comments, Jonathan Ross offers up another simple alternative that involves nesting the event listener function within a function and passing the arguments through the containing function:
function someFunction(arg1, arg2, arg3...):returnType
{
// Use the arguments anyhwere within the function body
eventSource.addEventListener(EventType.EVENT_NAME, listenerFunction);
function listenerFunction(e:EventType):void
{
// do something
}
}
someFunction(arg1, arg2, arg3...);
Example:
function initButtons(_btn:SimpleButton, greeting:String):void
{
_btn.addEventListener(MouseEvent.CLICK, greetPerson);
function greetPerson(e:MouseEvent):void
{
trace(greeting);
}
}
initButtons(_btn1, "Hi, John!");
initButtons(_btn2, "Hi, Mary!");
initButtons(_btn3, "Hi, Linda!");
Which method to use really depends on your knowledge in AS3 and the requirements of your project.
-
Creating Optional Parameters In As3
When a function has parameters, you don't always need to pass arguments to them. You can create parameters that are optional. To make a parameter optional, assign a value to it when it is created. This value will become the parameter's default...
-
As3 Dragging And Dropping Tutorial - As3 Startdrag() And As3 Stopdrag()
Exercise File: as3-drag-and-drop.fla - Flash CS4/CS5 as3-drag-and-drop.fla - Flash CS3 In this tutorial, we're going to learn how to create simple AS3 drag and drop functionality using the AS3 startDrag() and AS3 stopDrag() methods. The exercise file...
-
As3: Enabling The User To Submit The Contents Inside An Input Text Field
In this lesson, we're going to learn how to add some ActionScript 3 functionality that will let a user submit the contents of an input text field by clicking on a button or pressing a keyboard key. We'll create a simple example of a Flash movie...
-
How To Create An As3 Event Listener
Part 1: Introduction to AS3 event handling Part 2: How to create an AS3 event listener Part 3: The AS3 event object by Alberto Medalla Lecturer, Ateneo de Manila University In this next part of the Flash AS3 Event Handling tutorial series, we're going...
-
Preloading In Actionscript 3.0 Part 2
Exercise Files: Preloader02_Start.fla bird.jpg candles.jpg We've learned about the basics of preloading in Part 1 of Preloading in ActionScript. In this lesson, we'll apply the same concepts to make a picture gallery that has the ability to preload...
Tutorials