Posts Tagged Delegate

Using Delegate to have the right scope when using TweenLite, Tweener and Events in ActionScript 2

Not so long ago I was having a bad day using Flash 8 and ActionScript 2 just after completing a project in ActionScript 3. Now this Dan Hunter dude made a comment saying I should try using this Delegate class. Well last week I thought I should give it a try so I head over the blog post and try to figure out what it is used for. I had heard the word Delegate before, but never quite understood it.

So I look at the post and it is exactly what I hate in a post; it is very technical and not quite to the point. I don’t think I am a bad programmer and I should understand stuff like that but there is something missing in the post: why should I use this class in the first place?

Well I am going to tell you why you should used this class; because it will give you the right scope of the function called by the delegate function. Ok, ok maybe I’m not quite clear either, but go read this article on how to use the Delegate class and you should understand everything. I found it while installing the new version of Flash Develop (beta 6). The real useful thing about the Delegate class is that it permits you to use the “this” you want inside the function you want.

Here is my example using events; let’s start by using a simple button on click custom event. So you make this custom button class that handle the rollover and rollout internally and when you have a onrelease event, you send an event to the class containing the button. You do so in order to keep your code clean and well organized. Here is the code you would have for the event listener inside the class that will contain the custom button:

1
2
3
4
5
myCustomButton.addEventListener("click", handleClick);
 
function handleClick(evt:Object):Void{
  trace (this)//this will actually refer to myCustomButton instead of the containing class
}

By using the Delegate class, “this” will refer to the containing class. Here is how you do it:

1
2
3
4
5
myCustomButton.addEventListener("click", Delegate.create(this, handleClick));
 
function handleClick(evt:Object):Void{
  trace (this)//this will refer to the containing class
}

This will save you the trouble of having to find in which scope you are and also having to use this._parent all the time.

Another good use of the Delegate class is when you use the onComplete argument with Tweener or TweenLite. When you use the onComplete argument with both these Tweening engines, it will call the function you specify it when it will be done Tweening. With Tweener this function will be scoped to the MovieClip your doing a Tween on and with TweenLite the function will be scoped to the TweenLite Object which is not very convenient. Here is how you would use the Delegate class for these situation:

1
2
3
4
5
6
7
Tweener.addTween(myCustomButton, {_alpha:100, time:1, onComplete:Delegate.create(this, doNextStep)});
 
TweenLite.to(myCustomButton, 1, {_alpha:100, onComplete:Delegate.create(this, doNextStep)});
 
function doNextStep():Void{
  trace (this)//this will refer to the class containing the Tweening code
}

Well this will save me a lot of trouble. I was thinking that I was a good programmer and that experience didn’t really mean anything but these little things, like using the Delegate class to have the scope you want or know that TweenLite is faster than Tweener which is faster than the Adobe Tween classes,  can only be acquired with experience and they make your days a whole lot better. This kinda is a lesson of humility.

, ,

2 Comments


ActionScript 2 and Object Oriented Programming; an entire day of trouble

I was back to work today after the holidays and my first day hasn’t been that fun. First of all I’m back working in ActionScript 2 after working on the cool Christmas card which was in ActionScript 3. Second, I have been trying all day to make my menu work while programming it in an object oriented kinda way but all my efforts have been in vain.

 The first big problem I encountered is that even if you assign a class to a symbol in the library, if you put that symbol on the stage, it won’t use the class you assigned to it. You have to use the attachMovie method in order to make that work. Ok that’s fine I can find workaround in order to make that work.

Now I finally get that working the trouble is that the onRollOver and other onSomething won’t work in that class because of where it is situated (in an other class or other bug I just couldn’t find). Well I lost my whole day on that…

I feel it’s kinda wasteful to spend time on problems like this (in ActionScript 2) because I am not really learning something that will be useful in the long run. Anyway I don’t usually like to make post like this, because I don’t provide an answer to my problem but I really had to get this out of my system. Sorry about this, better post next time.

, ,

3 Comments