Passing Parameters with Flash’s Delegate Class

Google + Helpful Bloggers = KreeK gets shise done faster

I was working on getting a dynamic menu to scroll just now that had two functions one for scrolling up and one for scrolling down. Each function was called by Flash’s Delegate class and was about 15 lines long.

However, only one line of each function was different.

I thought to myself – “Self this is dumb, you’re repeating yourself, why not make one function with an argument to tell the menu to scroll up or down”. Not one to argue with myself I reworked the function – but how to pass the param?

I went a little nuts trying to figure it out ’til I fired up trusty Google and entered “flash delegate.create pass arguments”. Link number one was Scott Morgan’s post “Pass parameters with the Delegate class” yes!

Ok, that’s all fine and dandy, now what if you need to pass a paramater to the method you’re targeting. You could extend the Delegate class and allow for this, or you could write your own class that does the same thing but allows for paramaters to be passed. But really, who has time to do that! Below is the same example as above but this time I pass a paramater to the method.

import mx.utils.Delegate;

class Whatever extends MovieClip {

private var someClip_mc:MovieClip;

function Whatever() {
var myDel = someClip_mc.onRelease = Delegate.create(this, someMethod);
myDel.button = someClip_mc;
myDel.index = 4;
}

private function someMethod():Void {
var button:MovieClip = arguments.caller.button;
var index:Number = arguments.caller.index;

trace(button + ‘ has an index of ‘ + index);
//this will trace out someClip_mc has an index of 4
}
}

Other than talking to myself sanity has returned to KreeKland.

P.S. Add Scott to the list of kicking Canadian ActionScripters – we’re forced to learn it in High School – it’s the third official language after French :P

8 Comments

  1. Posted October 8, 2006 at 10:35 pm | Permalink

    Hey man, thanks for the props. Glad you liked the tutorial.

  2. Judio
    Posted October 17, 2006 at 3:33 am | Permalink

    What happens if you are trying to set up a number of buttons within a for loop and want to pass the id of each button using the Delegate class? For example:

    import mx.utils.Delegate;

    class Whatever extends MovieClip {

    private var someClip_mc:MovieClip;
    private var someList:Array;

    function Whatever() {
    for(var i=0; i

  3. Posted October 17, 2006 at 6:54 pm | Permalink

    Easy:

    private function drawMenu():Void {

    var len:Number = 0;
    var x:Number = 0;
    for (var i:Number = 0;i

  4. Posted October 17, 2006 at 7:01 pm | Permalink

    Well, guess this blog doesn’t like < signs. Let me try this again. Easy:

    private function drawMenu():Void {
    var len:Number = 0;
    var x:Number = 0;
    __buttonArray = new Array();
    for (var i:Number = 0;i<len;i++) {
    var button = __instance.attachMovie(’myButton’, ‘myButton’+i+’_mc’, i);
    button._x = x;
    button.label = ‘Button ‘ + i;
    x += button._width;
    __buttonArray.push(button);
    var clickDel = button.onRelease = Delegate.create(this, buttonClicked);
    clickDel.id = i;
    }
    }

    private function buttonClicked():Void {
    var id:Number = arguments.caller.id;
    trace(’The id for the selected button is: ‘ + id);
    trace(’The path to this button is: ‘ + __buttonArray[id] + ‘ or ‘ + __instance['myButton'+id+'_mc']);
    }

    Hope this helps, and I hope your blog doesn’t blow up again when I submit this :)

  5. Jason Merrill
    Posted March 2, 2007 at 10:50 pm | Permalink

    Awesome! Thanks for posting this, I had always wondered about that and never found a reason. I had all kinds of kludgy
    workarounds previously, but now this makes sense and I’m using it. AS3 gets rid of Delegate, but this is great until we
    all make the transition.

  6. Posted June 17, 2007 at 3:44 am | Permalink

    nice, very handy

  7. Rob
    Posted July 6, 2008 at 7:06 am | Permalink

    Absolutely brilliant! Saved my day. Thank you!

  8. j.c Pimentel
    Posted May 2, 2010 at 4:15 pm | Permalink

    i implement this solution a couple times and works ok but i found a better solution to do this in a elegant way , using a modification of the create function of the Delegate class.

    function DelegateCreate(t:Object, f:Function,args:Array):Function
    {
    return function():Void
    {
    if ((args == undefined) || (args == null))
    args=[]
    var _newArgs:Array = args.concat(arguments);
    f.apply(t, args);
    };
    }

    var movie_clip.onPress=DelegateCreate(this,onPressFunc,[1,2,3,4,"cool"] )

    and here is the function

    function onPressFunc(arg1,arg2,arg3,arg,4,arg5)
    {
    //N args of any king or type that you pass using the parameter array.

    }

Post a Comment

Your email is never shared. Required fields are marked *

*
*

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word