Vixiom Axioms

October 4, 2006

Passing Parameters with Flash’s Delegate Class

Filed under: ActionScript, Flash Alastair @ 6:39 pm

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

Digg! submit Passing Parameters with Flash’s Delegate Class to stumbleupon.com submit Passing Parameters with Flash’s Delegate Class to del.icio.us submit Passing Parameters with Flash’s Delegate Class to reddit.com Like this post? subscribe to the feed.

7 Comments »

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

    Comment by Scott Morgan — October 8, 2006 @ 10:35 pm

  2. 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

    Comment by Judio — October 17, 2006 @ 3:33 am

  3. Easy:

    private function drawMenu():Void {

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

    Comment by Scott Morgan — October 17, 2006 @ 6:54 pm

  4. 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 :)

    Comment by Scott Morgan — October 17, 2006 @ 7:01 pm

  5. 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.

    Comment by Jason Merrill — March 2, 2007 @ 10:50 pm

  6. nice, very handy

    Comment by Toby Skinner — June 17, 2007 @ 3:44 am

  7. Absolutely brilliant! Saved my day. Thank you!

    Comment by Rob — July 6, 2008 @ 7:06 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Powered by WordPress