Remembering why I hate AS3

I’ve mostly switched over to Unity development, but once in a while a client will request a game in Flash and I take the job. It’s been about a year since I’ve done any AS3 work, but I picked it back up pretty quickly.

I started building my baseclass with all my reusable methods and started with the once I use the most (Pause, fadeIn, fadeOut, etc).  I started knocking everything together and it was going just fine, but then after adding in several classes I noticed that my alpha transitions started to hang randomly… how curious!  Here’s an example of my base class:


package
{
/**
* ...
* @author Timothy W. Johnson
* @description: Main base class. Most other classes will extend this.
*/

import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.utils.Timer;
import fl.transitions.Tween;
import fl.transitions.easing.*;

public class BaseClass extends MovieClip
{

public function BaseClass():void
{
//TODO: Nothing!
}

public function Pause(time : int, func : Function):void {
var timer : Timer = new Timer(time, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, func);
timer.start();
}

public function fadeIn():Boolean {
var fadeTween : Tween = new Tween(this, “alpha”, Regular.easeIn, 0, 1, 0.3, true);
return true;
}

public function fadeOut():Boolean {
var fadeTween : Tween = new Tween(this, “alpha”, Regular.easeOut, 1, 0, 0.3, true);
return true;
}

}

}

This started to really bug the crap out of me as different objects I loaded from this base class would freeze when fading in or fading out. At first i thought there was something wrong with the transitions library, but that wasn’t it. It was only after some more troubleshooting that I realized it was the infamous AS3 garbage collector ruining my day. You can read more detailed blog postings about why it’s infamous, but the short of it is that you never know when it’s going to run and it’ll do all sorts of damage.

To ensure that the garbage collector doesn’t destroy your object, make sure you declare them at the class level. After making this change, everything worked correctly:


package
{
/**
* ...
* @author Timothy W. Johnson
* @description: Main base class. Most other classes will extend this.
*/

import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.utils.Timer;
import fl.transitions.Tween;
import fl.transitions.easing.*;

public class BaseClass extends MovieClip
{
private var fadeTween : Tween;

public function BaseClass():void
{

}

public function Pause(time : int, func : Function):void {
var timer : Timer = new Timer(time, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, func);
timer.start();
}

public function fadeIn():Boolean {
fadeTween = new Tween(this, “alpha”, Regular.easeIn, 0, 1, 0.2, true);
return true;
}

public function fadeOut():Boolean {
fadeTween = new Tween(this, “alpha”, Regular.easeOut, 1, 0, 0.2, true);
return true;
}

}

}

Advertisements

0 Responses to “Remembering why I hate AS3”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: