Passing Arguments Through a Listener Part 4 – Custom Events

January 25th, 2010

(This is part 4 of a series of posts on ways to pass arguments to a function through event listeners. For part one, see here, for part two, see here, and for part three, see here.)

Today sees the last chapter of our quest to pass information to a function through a listener. For this final method we’re going to be looking at creating our own custom events, and  how they can be used to store and retrieve information.
Read the rest of this entry »

Passing Arguments Through a Listener Part 3 – Interim Function

January 22nd, 2010

(This is part 3 of a series of posts on ways to pass arguments to a function through event listeners. For part one, see here, for part two, see here, and for part four, see here.)

For today’s method of passing values to a function through a listener, we’re going to bend the rules a little. The result will be a set of slightly different listeners, but each will eventually call our desired function. At the expense of bending the rules, we’re going to end up with what is probably the best solution short of creating custom events, albeit quite a long-winded one.
Read the rest of this entry »

Passing Arguments Through a Listener Part 2 – Using Variables

January 20th, 2010

(This is part 2 of a series of posts on ways to pass arguments to a function through event listeners. For part one, see here, for part three, , and for part four, .)

So you’ve decided anonymous functions aren’t for you. Congratulations, you’ve decided correctly! But that doesn’t help you on your way to really dynamic listeners. What other options are available to you? Read the rest of this entry »

Passing Arguments Through a Listener Part 1 – Anonymous Function

January 18th, 2010

(This is part 1 of a series of posts on ways to pass arguments to a function through event listeners. For part two, see here, for part three, , and for part four, .)

One of the things I often wonder about actionscript 3 is the best way to pass arguments to a function through an event listener. Typically when you add an event listener, you simply specify a type of event, and the name of the function to call when the event is dispatched. (You may also specify the optional parameters useCapture, priority, and useWeakReference, but seeing as how that’s not relevant to the subject at hand, we’ll be focusing on just the first two):

myButton.addEventListener(MouseEvent.CLICK, outputText);

When ‘myButton’ was clicked, this would then call the function ‘outputText’, which would take just one parameter – the event that triggered it:

function outputText(evt:MouseEvent):void {
trace("Hello World!");
}

But what if you wanted to have multiple buttons that all triggered one function, each passing a different parameter? This functionality isn’t supported as standard, so a little improvising is required. This series of posts aims to discuss all your options, from custom events (the good), to external variables (the bad), and anonymous functions (the ugly). Read the rest of this entry »

Make buttonMode enabled dynamic text fields with AS3

January 4th, 2010

This is a little niggling problem that I find myself rediscovering every now and then when working with dynamic text – particularly dynamic text as part of an interface. When you have a movieclip with buttonMode set to true, you expect the cursor to change to a pointing hand whenever it is over any part of the movieclip, as in this example:

Mousing over the button gives you a hand cursor, updates the button to it’s ‘over’ state, and changes the text at the top, all as expected. The problems arise when using a dynamic text field to label the button, like this:

Mousing over the button still changes it’s state and alters the text, and to a certain extent changes the cursor. However, when you move the cursor over the text field it goes back to a regular arrow, despite the fact that flash can clearly tell it’s still over the button, as evidenced by the ‘over’ state of the button, and the top text field.

Read the rest of this entry »