Search Box

Google
 

Friday, February 16, 2007

A Delegate Action.

So what does Delegate has got to do with ActionScript ? Yes it means a lot when it comes to Actionscript classes and scope management. Scope issues are one of the fundamental issues when it comes to ActionScript classes.

Let me take the pleasure of discussing about Delegate class today. But wait where does Delegate come from ? She comes from mx.utils.Delegate. Meaning there is a folder 'mx' containing lots of flash goodies classes among them we find a branch 'utils' which holds the fruit called 'Delegate'. Why do i need to praise so much will be understood by you shortly.

Consider the following script :



//in the main timeline we have a movieclip with instance name "$mc".


var This:MovieClip = this;

this.$mc.onPress = function()
{
trace(This);//traces _level0;
}


Guess that code was very elementary to be understood and hope does not require any explanation. Things are working fine is assumed based on the trace message of "_level0" which should be seen when rendered.

But this is not the case when it comes to classes. For example consider the next example :



//Now in the main timeline using the movie created with the example 1.

var This:MovieClip = this;
var te:Test = new Test($mc);

//For the class 'Test' copy the code below, create a new .as name it "Test" and paste the code.


//The class defintion starts.

class Test
{
var movie:MovieClip;
var This:MovieClip;
public function Test(mc:MovieClip)
{
movie = mc;
This = movie._parent;
movie.onPress = function()
{
trace(This)//traces 'undefined';
}
}
}


Ctrl + Enter and click the movieclip on the stage.Now did you see what happened, it would have been a message 'undefined'. So how are we going to handle this forms the rest of the story with the introduction of our Delegate character as the hero.

Okay now we will do some slight modification to our "Test" class with the help of Delegate and see what happens.




//The class re - defintion starts.


import mx.utils.Delegate;
class Test
{
var movie:MovieClip;
var This:MovieClip;
public function Test(mc:MovieClip)
{
movie = mc;
This = movie._parent;
movie.onPress = Delegate.create(this,function()
{
trace(This)//traces '_level0';
});
}
}





Now on ctrl+Enter and triggering the press action of the movieclip displays a message "_level0" meaning the code was successfully doing the right thing. hmmm can i know what has happened to the new version of 'Test' class ? It was the 'Delegate' class who was taking the pain of mapping the variables of the class within the scope of the $mc's onPress action. B-e-a-utiful isn't it ?

The 'Delegate' class comes to the rescue for scope management while writing in classes.

As a bonus i wish to specify a hack for the Delegate class but i am stopped to make you wonder for what the bonus could be. If you start using Delegate class, soon you will come to a point you will be struck. Then at that time come back and earn your bonus. Till then happy sailing with Delegate class.


Regards,

Ashok Srinivasan.

3 comments:

vinodvv said...

I have made a smallest change in the class. This is what I use to do. Correct me if I am wrong.
class Test
{
var movie:MovieClip;
var This:MovieClip;
public function Test(mc:MovieClip)
{
movie = mc;
This = movie._parent;
var o:Object = this;
movie.onPress = function()
{
trace(o.This)//traces 'undefined';
}
}
}

Ashok Srinivasan said...

Vinod thanks for your comment. But i think your code works perfectly fine.

but instead if you had sent the _level0 itself as the parameter in the class constructor then on the press action you will try to trace (_level0._parent).Since movie would have been _level0 and This would have been _level0._parent.

So watch out if that is the problem otherwise the code should work.

Regards,

Ashok Srinivasan.

Anonymous said...

I come back for the bonus ^^

i.e. how to use Delegate without it to be responding to an event ? like in :

onPress = Delegate.create
onLoad = Delegate.create

AND

without event listener ?

Basicaly how to use this as a simple function call ?