Archive for the 'ActionScript 2' Category

15th Aug 2008

Hold on a Second

You can find a lot of tween libraries for AS2 and AS3. Many of these include some sort of mechanism for delaying the start of a tween. A while back I took my own shot at this based off of another class that I found and came up with:

import mx.transitions.Tween;
class com.vfd.animation.tweens.DelayedTween extends Tween{

private var intervalID:Number
private var delay:Number = 0;

public function DelayedTween(object, property, easingFunction, start, end, duration, useSeconds, delay) {

super(object, property, easingFunction, start, end, duration, useSeconds);
this.delay = Math.abs(delay*1000);
this.setDelay();

}
private function start() {

}

private function setDelay() {
if (this.delay > 0) {
this.intervalID = setInterval(this, “startAnimation”, this.delay);
}else {
this.startAnimation();
}
}

private function startAnimation() {
clearInterval(this.intervalID);
super.start();
}

}

This was more or less a simple copy of the class I had found. I got a chance to look at this again today and realized I could simplify it a bit. I ended up with:

/**
* @author Jeremy Wischusen
* This class allows for a delay before starting the animation.
*/
import mx.transitions.Tween;
import mx.utils.Delegate;
class com.vfd.animation.tweens.DelayedTween extends Tween {
/*
Holds the number of seconds to wait before starting the animation.
*/
private var _delay:Number = 0;
/*
Instance variable holding a reference to the start function of the Tween class.
This is needed due to scoping issues when using setTimeout.
*/
private var superStart:Function;
public function DelayedTween(object, property, easingFunction, start, end, duration, useSeconds, delay) {
this.delay = delay;
/*Use the Delegate class to create a reference back to the tween start funciton
that can be used with setTimeout. */
this.superStart = Delegate.create(super, super.start);
super(object,property,easingFunction,start,end,duration,useSeconds);
}
/*Override the default start function so that the delay can be applied.*/
public function start() {
if (this._delay>0) {
_global.setTimeout(this.superStart,this._delay);
} else {
super.start();
}
}
/*Setter function for delay attribute*/
public function set delay(seconds:Number){
this._delay = Math.abs(seconds*1000);
}
}

As you can see, I reduced the number of functions, used the simpler setTimeOut and rely more on inheritance and overriding than in the previous version. I also decided to make and AS3 version.

package com.vfd.animation.tweens{
import fl.transitions.Tween;
import flash.utils.setTimeout;
/**
* @author Jeremy Wischusen
* This class allows for a delay before starting the animation.
*/
public class DelayedTween extends Tween {
/*
Holds the number of seconds to wait before starting the animation.
*/
private var _delay:Number = 0;
public function DelayedTween(object, property, easingFunction, start, end, duration, useSeconds, delay) {
this.delay = delay ;
super(object,property,easingFunction,start,end,duration,useSeconds);
}
/*Override the default start function so that the delay can be applied.*/
public override function start():void {
if (this._delay>0) {
setTimeout(super.start,this._delay);
} else {
super.start();
}
}
/*Setter function for delay attribute*/
public function set delay(seconds:Number):void {
this._delay = Math.abs(seconds*1000);
}
}
}

I have noticed that occasionally the compiler spits out:
The superconstructor must be called first in the constructor body.

If you place the super constructor before the delay variable is set, the tween just starts immediately since the variable has a default value of 0. This is what caused the need for the additional functions in the first version. While the compiler may occasionally complain (sometimes it shows the message and other times it does not), it still compiles and works.

Will be committing these to the repository on Google code sometime today.

Posted by Posted by Jeremy Wischusen under Filed under ActionScript 2, ActionScript 3, OOP, Open Source Projects Comments No Comments »

28th Apr 2008

Setting Up A Global Class Path In Flash

If you are just getting into using classes in Flash, chances are you are probably just creating your class (.as files) in the same directory as your FLA. This is simple, fast and works, but if I want to reuse a class from one Flash piece in another, I now have to copy over all of those class files into my new project directory. If I use that methodology, if I have to update something in one of my classes, I now have to update them in more than one location. This quickly turns into a maintenance nightmare and it turns out there is a simple solution.

Flash (as far back as mx 2007, not sure about older versions) will allow you to select a folder(s) where you can store classes that are used across multiple projects. This is commonly referred to as a class path. When you set up a class path and then do something like:


import com.mydomain.MyClass;
var myInstance:MyClass = new MyClass();

where MyClass is some custom class that you wrote, Flash will now look in your specified class path to see if it can find the corresponding class file (in this case MyClass.as). This means I am now free to use this class in any of my projects without having copy my class file into each project directory.

Please note that I am showing you how to set this for the program as a whole. You can also set this on a per document basis using the publishing settings

So how do we set this little piece of magic up?

  1. Got to the Edit menu and select Preferences.
  2. The following dialog box should appear:
    Flash Preferences
  3. Select ActionScript from the list on the left.
  4. Click the button entitled “ActionScript [version number] Settings” next to the Language label and the following dialog box should appear.
  5. Make sure to hit the + button resetting the entry that reads $(LocalData)/Classes will reset the default class path for Flash and Flash will no longer be able to find its built in classes. Clicking the + sign should make a blank line appear in the list.
  6. Click on the icon that looks like cross hair. This should bring up a file selection dialog box.
  7. Select the folder where you would like to store your classes.
  8. Keep clicking OK until all of the dialog boxes have closed.

Now you have your class path set up. This means that you can start storing you class files in this location. Please take note of the following:

  • Now that you have the class path set up, you can use import statements to bring in your classes just like you do with the standard Flash classes. For example import com.mydomain.MyClass; . Note a few things about the import statement.
    • The actual folder you selected is NOT included in the import statement (in my example C:\asclasses)
    • In import statements, dots = slashes. What I mean by this is that in our example, com.mydomain.MyClass is looking a file named MyClass.as in a folder mydomain that is a subfolder of com (i.e. /com/mydomain/MyClass.as). The beginning folder com in turn is actually a sub folder of the class path (i.e. C:\asclasses\com\mydomain\MyClass.as). Since we have setup C:\asclasses as a class path, Flash automatically looks in that location and we only have to specify the path using the dot syntax from that folder down. If you want to learn more about this dot syntax, do some research on the topic of packages in Flash.
    • As you may have noticed, you leave off the .as file extension.

Now that you have you class path set up, you can truly begin to start creating classes that are reusable across projects and only have to be maintained in one location.

Posted by Posted by Jeremy Wischusen under Filed under ActionScript 2, ActionScript 3, Flash, OOP Comments 2 Comments »