If I want to change the behavior of an object dynamically I rather
create a new class to encapsulate the functionality. I might have to
write more code but I think it this is a very clean approach. 


class CustomClickBehavior implements Clickable 
{
        


        public function click():Void
        {
                trace ("hello world!");
        }
}



// class  
class Foo
{
        var myClickBehavior: Clickable;

        // here I can change the behavior to what ever I want
        public function setClickBehavior (myClickBehavior:
Clickable):Void
        {
                this. myClickBehavior = myClickBehavior;
        }


        public function onClick():Void
        
        {
                myClickBehavior.click();
        }

}


// Interface

Interface Clickable
{
        public function click():void;
}


// code

var myFoo = new Foo();
var myCustomClickBehavior  = new CustomClickBehavior();

myFoo.setClickBehavior(myCustomClickBehavior);





-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of dnk
Sent: Monday, 14 August 2006 1:47 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] |:::| can you write dynamic meathods in a
class?

Bjorn Schultheiss wrote:
> Hey Guys, check this out
>
>
> Var clickHandler:Function = MyClass.staticMethod;
> var args:Array = ['Can', 'pass', 'in', 'any', 'amount'];
> newBtn.addEventListener("click", Delegate.create(this,
function(evt:Object,
> meth:Function, args:Array) { meth.apply(null, args) }, clickHandler,
args )
> ); 
That is a good idea! I just tried one of simpler layout (at least to me 
in terms of what I understand in AS) .... works like a charm:

import utils.Delegate;
//Create array to pass multiple args in - both string and number for
testing
var aMyArgs:Array = [1, 2, 'three', 4];
//use the delegate
this.menuBtn.addEventListener("click", Delegate.create(this, onHit1, 
aMyArgs));
//create the btn event to handle the click
function onHit1(o:Object, n:Array)
{
    for (i = 0; i < n.length; i++)
    {
    trace("it was hit with the arg: " + n[i]);
    }
}

This is VERY handy (well to me anyways) to be able to pass in what ever 
args I need to.

d


_______________________________________________
Flashcoders@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
_______________________________________________
Flashcoders@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

Reply via email to