[Flashcoders] AS3 Delegate

3 views
Skip to first unread message

Ian Thomas

unread,
Apr 9, 2007, 1:50:41 PM4/9/07
to Flashcoders mailing list
Hi all,
In developing my first largish AS3 app, I've discovered something
that I think is missing.

I'd assumed that Delegate, which peppered all my AS2 projects, would
have been made redundant and, indeed, it kind of has - in that AS3
takes care of the context of a function, so you can happily say (for
example):

button.onRelease=myFunction; // AS3

instead of

button.onRelease=Delegate.create(this,myFunction); // AS2

Hurrah!

But unfortunately, I've always used Delegate for a bit more than that
- an expanded version (like the Proxy class that's been kicking
around) that lets you tack on additional variables to make callbacks
that bit more useful.

That being the case, I've come up with a new version for AS3 which
suits my purposes and may be handy for other people.

All the info is here:
http://wildwinter.blogspot.com/2007/04/come-back-delegate-all-is-forgiven.html

Hope it's helpful,
Ian
_______________________________________________
Flash...@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Merrill, Jason

unread,
Apr 9, 2007, 2:12:15 PM4/9/07
to flash...@chattyfig.figleaf.com
However, in AS 2 to tack on variables, I did/do it differently:

var myDel:Object = myButton.onRelease = Delegate.create(this,
doFunction)
myDel.fruit = "pineapple";

function doFunction():Void
{
trace(arugments.caller.fruit)
}

I assumed in AS3 you could do something similar, though I don't know, I
haven't tried yet:

var myObj:Object = myButton.onRelease = doFunction
myObj.fruit = "pineapple";

function doFunction():Void
{
trace(arugments.caller.fruit)
}

Or no? I kinda doubt you have to do it the way you posted on your blog,
but I don't know for sure. If you do, that really sucks.


Jason Merrill
Bank of America
GT&O Learning & Leadership Development
eTools & Multimedia Team

Tareq AlJaber

unread,
Apr 9, 2007, 2:15:10 PM4/9/07
to flash...@chattyfig.figleaf.com
I will be out of the office starting 04/09/2007 and will not return until
04/16/2007.

I will not have access to my email during this time. If you need
immediate assistance, please contact Aaron Dolberg
(adol...@adobe.com). Thank you.

Tareq AlJaber

unread,
Apr 9, 2007, 2:19:55 PM4/9/07
to flash...@chattyfig.figleaf.com

T. Michael Keesey

unread,
Apr 9, 2007, 2:20:45 PM4/9/07
to flash...@chattyfig.figleaf.com
The problem with using proxies is that you circumvent compile-time
checking. I find that there are better ways of dealing with such
situations. Three suggestions:

1) In the handler function, look for a property of the event target.
This works for most cases where you need a handler function to react
differently in a given situation. (For example, different buttons
could carry an "index" event that the handler looks for via
MyButtonClass(event.target).index.)

2) Differentiate the event type and have one handler deal with several
event types. This is not useful in cases where you may have an
unlimited number of different responses, and generally I wouldn't
recommend this as a solution, but there may be some cases where it's
appropriate.

3) Create a new subclass of Event with one or more extra properties.
(For example, IndexEvent, with an added property, "index".)

By the way, I think the AS3 syntax for this:

> button.onRelease=myFunction; // AS3

... is actually:

button.addEventListener(MouseEvent.CLICK, myFunction);

--
T. Michael Keesey
Director of Technology
Exopolis, Inc.
2894 Rowena Avenue Ste. B
Los Angeles, California 90039
--
The Dinosauricon: http://dino.lm.com
Parry & Carney: http://parryandcarney.com
ISPN Forum: http://www.phylonames.org/forum/

ben gomez farrell

unread,
Apr 9, 2007, 2:22:34 PM4/9/07
to flash...@chattyfig.figleaf.com
I didn't really read the webpage, but you can't tack on a variable I'm
pretty sure. To tack something on, the class needs to be dynamic, which
an Object class is, but a MovieClip or Sprite class isn't. So unless
you're caller is something you extended from a movieclip or sprite and
made dynamic, you won't be tacking anything on.
I think I'm right, I'm pretty sure I tried!

Delegate is still kinda useful. I've been trying to get by without it
since I'm still learning, but the way I've been doing things without it
is to dispatch customized events that have specific parameters. It's
kind of a pain in the butt though, say if you want to pass a parameter
along with your mouseclick. Since you can't control how a mouseclick is
dispatched, you have to make a class to listen for a mouseclick, and
then listen to the custom dispatched event from that class.

At least this is how I've been doing things so far!
ben

Ian Thomas

unread,
Apr 9, 2007, 2:26:04 PM4/9/07
to flash...@chattyfig.figleaf.com
Hi Jason,
That's an interesting one - haven't tried that (but then, our
Delegate function coped with it).

Unfortunately you won't be able to do that as you describe, because
in AS3 there's no arguments.caller - only arguments.callee.

I doubted that I had to do it my way, too - was quite disappointed
when I discovered that AS3 didn't have a better mechanism because I
thought, like with the context issue, it was something lots of people
had tripped over in AS2.

I may have missed something new, of course!

Cheers,
Ian

On 4/9/07, Merrill, Jason <jason....@bankofamerica.com> wrote:
> However, in AS 2 to tack on variables, I did/do it differently:
>
> var myDel:Object = myButton.onRelease = Delegate.create(this,
> doFunction)
> myDel.fruit = "pineapple";
>
> function doFunction():Void
> {
> trace(arugments.caller.fruit)
> }
>
> I assumed in AS3 you could do something similar, though I don't know, I
> haven't tried yet:
>
> var myObj:Object = myButton.onRelease = doFunction
> myObj.fruit = "pineapple";
>
> function doFunction():Void
> {
> trace(arugments.caller.fruit)
> }
>
> Or no? I kinda doubt you have to do it the way you posted on your blog,
> but I don't know for sure. If you do, that really sucks.

Tareq AlJaber

unread,
Apr 9, 2007, 2:32:09 PM4/9/07
to flash...@chattyfig.figleaf.com

Tareq AlJaber

unread,
Apr 9, 2007, 2:37:05 PM4/9/07
to flash...@chattyfig.figleaf.com
I will be out of the office starting 04/09/2007 and will not return until
04/16/2007.

I will not have access to my email during this time. If you need
immediate assistance, please contact Aaron Dolberg
(adol...@adobe.com). Thank you.

_______________________________________________

Tareq AlJaber

unread,
Apr 9, 2007, 2:49:11 PM4/9/07
to flash...@chattyfig.figleaf.com
I will be out of the office starting 04/09/2007 and will not return until
04/16/2007.

I will not have access to my email during this time. If you need
immediate assistance, please contact Aaron Dolberg
(adol...@adobe.com). Thank you.

_______________________________________________

Ian Thomas

unread,
Apr 9, 2007, 2:53:57 PM4/9/07
to flash...@chattyfig.figleaf.com
Hi Michael,
I think this is down to a difference in programming style. We don't
use events in quite the same way - or rather, for _most_ situations,
we use events in exactly the way that you describe.

However, there are many situations where defining event types,
listeners and dispatchers is (I find) much too heavyweight.
Specifically, in instances where there is only going to be one single
create-doprocess-callback sequence and nothing else will ever get
involved in the chain.

Additionally, you can use the Callback method I describe to tack
additional arguments on to handlers called in response to event
objects created internally by Adobe code - which are instances in
which you couldn't extend the relevant Event object.

(This difference in approach may be because we're leaping into AS3
straight from Flash/AS2 rather than going via Flex.)

Cheers,
Ian

On 4/9/07, T. Michael Keesey <kee...@gmail.com> wrote:
> The problem with using proxies is that you circumvent compile-time
> checking. I find that there are better ways of dealing with such
> situations. Three suggestions:
>
> 1) In the handler function, look for a property of the event target.
> This works for most cases where you need a handler function to react
> differently in a given situation. (For example, different buttons
> could carry an "index" event that the handler looks for via
> MyButtonClass(event.target).index.)
>
> 2) Differentiate the event type and have one handler deal with several
> event types. This is not useful in cases where you may have an
> unlimited number of different responses, and generally I wouldn't
> recommend this as a solution, but there may be some cases where it's
> appropriate.
>
> 3) Create a new subclass of Event with one or more extra properties.
> (For example, IndexEvent, with an added property, "index".)
>
> By the way, I think the AS3 syntax for this:
>
> > button.onRelease=myFunction; // AS3
>
> ... is actually:
>
> button.addEventListener(MouseEvent.CLICK, myFunction);
>

>

Ian Thomas

unread,
Apr 9, 2007, 3:07:24 PM4/9/07
to flash...@chattyfig.figleaf.com
On 4/9/07, ben gomez farrell <b...@yellow5labs.com> wrote:
> Delegate is still kinda useful. I've been trying to get by without it
> since I'm still learning, but the way I've been doing things without it
> is to dispatch customized events that have specific parameters. It's
> kind of a pain in the butt though, say if you want to pass a parameter
> along with your mouseclick. Since you can't control how a mouseclick is
> dispatched, you have to make a class to listen for a mouseclick, and
> then listen to the custom dispatched event from that class.
>
> At least this is how I've been doing things so far!

Ben,
That's precisely why I've written the class. Take a look at the
webpage, it might help. :-)

Cheers,
Ian

Tareq AlJaber

unread,
Apr 9, 2007, 3:15:09 PM4/9/07
to flash...@chattyfig.figleaf.com
I will be out of the office starting 04/09/2007 and will not return until
04/16/2007.

I will not have access to my email during this time. If you need
immediate assistance, please contact Aaron Dolberg
(adol...@adobe.com). Thank you.

_______________________________________________

Ian Thomas

unread,
Apr 9, 2007, 3:15:49 PM4/9/07
to flash...@chattyfig.figleaf.com
On 4/9/07, T. Michael Keesey <kee...@gmail.com> wrote:

> By the way, I think the AS3 syntax for this:
>
> > button.onRelease=myFunction; // AS3
>
> ... is actually:
>
> button.addEventListener(MouseEvent.CLICK, myFunction);

Sorry, yes - you're quite right. I was illustrating a general point
and clearly chose the wrong thing to use as an example - we're coming
from Flash, not Flex, and don't use Adobe's components.

Ian

Tareq AlJaber

unread,
Apr 9, 2007, 3:22:51 PM4/9/07
to flash...@chattyfig.figleaf.com

Tareq AlJaber

unread,
Apr 9, 2007, 3:33:43 PM4/9/07
to flash...@chattyfig.figleaf.com

Tareq AlJaber

unread,
Apr 9, 2007, 3:38:23 PM4/9/07
to flash...@chattyfig.figleaf.com

Tareq AlJaber

unread,
Apr 9, 2007, 3:49:55 PM4/9/07
to flash...@chattyfig.figleaf.com
I will be out of the office starting 04/09/2007 and will not return until
04/16/2007.

I will not have access to my email during this time. If you need
immediate assistance, please contact Aaron Dolberg
(adol...@adobe.com). Thank you.

_______________________________________________

Muzak

unread,
Apr 9, 2007, 5:01:28 PM4/9/07
to flash...@chattyfig.figleaf.com
I think that's just another example of "quick and dirty".
If your button needs a property "fruit" it should be a property of the class.
In other words, it should be its own class, eg FruitButton, with a property "fruit".

In AS2 I have never needed to pass any arguments to a Delegate, ever..
If you find you need to, you're doing something wrong and there's other (and better) ways of doing it.

In Flex, components have a data property of type Object that you can use.
It's main purpose is for components used as renderers, but if you feel like it, you can use it for other purposes as well.

function btnClickHandler(evt:MouseEvent):void {
trace(evt.currentTarget.data.fruit);
}
var btn:Button = new Button();
btn.data = {fruit:"pineapple"};
btn.addEventListener(MouseEvent.CLICK, btnClickHandler);

However, I doubt FCS3 components will have that same data property, since item renderers are not a big part of the Flash Component
Framework, at least not in the same way it is in Flex.

AS3 finally has an Event class, which can be easily extended.. Time to start using it ;-)

regards,
Muzak

----- Original Message -----
From: "Merrill, Jason" <jason....@bankofamerica.com>
To: <flash...@chattyfig.figleaf.com>
Sent: Monday, April 09, 2007 8:12 PM
Subject: RE: [Flashcoders] AS3 Delegate


> However, in AS 2 to tack on variables, I did/do it differently:
>
> var myDel:Object = myButton.onRelease = Delegate.create(this,
> doFunction)
> myDel.fruit = "pineapple";
>
> function doFunction():Void
> {
> trace(arugments.caller.fruit)
> }
>
> I assumed in AS3 you could do something similar, though I don't know, I
> haven't tried yet:
>
> var myObj:Object = myButton.onRelease = doFunction
> myObj.fruit = "pineapple";
>
> function doFunction():Void
> {
> trace(arugments.caller.fruit)
> }
>
> Or no? I kinda doubt you have to do it the way you posted on your blog,
> but I don't know for sure. If you do, that really sucks.
>
>
> Jason Merrill

Steven Sacks | BLITZ

unread,
Apr 9, 2007, 5:19:37 PM4/9/07
to flash...@chattyfig.figleaf.com
KICK THIS GUY OFF THE LIST!

His out of office email is spamming every thread.

Reply all
Reply to author
Forward
0 new messages