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
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
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.
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/
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
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.
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.
_______________________________________________
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.
_______________________________________________
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);
>
>
Ben,
That's precisely why I've written the class. Take a look at the
webpage, it might help. :-)
Cheers,
Ian
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.
_______________________________________________
> 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
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.
_______________________________________________
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
His out of office email is spamming every thread.