Internet Explorer 6 is not supported.
Upgrade to view this site.

Blog Archive

Results for the blog category ActionScript 3.0.

TransitionManager Events

I recently used the TransitionManager class to get some ready-made animation in a project I am working on. The problem was I needed to know when the tween ended so I could dispatch an event to tell my application to allow user interfacing again. I checked the Adobe API on TransitionManager and there were no events to respond to be found there. Not even a useful inherited event. So I decided to poke around in the class file itself just to see what was going on in there. In case you don’t know, never edit these classes. They are essentially part of the Flash application. If you need extra functions it’s always better to make a subclass extending the class and write your own methods and properties. That what OOP is all about.

There are 2 events that can be responded to in their without editing the code to do so. The two strings that you can add a listener for are these.

1
2
const ALL_TRANSITIONS_IN_DONE:String = "allTransitionsInDone";
const ALL_TRANSITIONS_OUT_DONE:String = "allTransitionsOutDone";

Of course, these events don’t carry any TransitionManager specific properties, it just dispatches the generic Event telling the application that it happened. I don’t know why this wasn’t included in the API, as it is something that is useful for this the implementation of this class. Maybe I’ll get around to writing a TransitionManagerEvent class one day. Until then, this works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import fl.transitions.*;
import fl.transitions.easing.*;
 
const ALL_TRANSITIONS_IN_DONE:String = "allTransitionsInDone";
const ALL_TRANSITIONS_OUT_DONE:String = "allTransitionsOutDone";
 
var currentSlide:Slide = new Slide();
addChild(currentSlide);
 
var tm:TransitionManager = new TransitionManager(currentSlide);
tm.startTransition({type:Blinds, direction:Transition.OUT, duration:2, easing:None.easeNone, numStrips:10, dimension:0});
tm.addEventListener(ALL_TRANSITIONS_OUT_DONE, outTransitionsDone);
 
function outTransitionsDone(event:Event):void
{
	trace("done");
}

You can get the FLA example file here.

Associative Arrays

An Associative Array can be made in ActionScript 3 by using an Object instead of an Array. You use the Object to make a name/value pair. Name/Value pairs consist of a name and a value and are separated by a colon. The name is what you use to reference the value. When you reference what is in the Associative Array you just write the name as if it was a property on the Object. Here is an example of how to make an Associative Array.

1
2
3
var info:Object = {name:"Tony", occupation:"Bouncer", iq:80};
 
trace("My name is " + info.name + " and I am a " + info.occupation + " with an IQ of " + info.iq + "." );

You could also write it out long-handed and the effect would be the same. Below is the same thing but the names are added as dynamic properties of the newly instantiated Object.

1
2
3
4
5
6
var info:Object = new Object();
info.name = "Tony";
info.occupation = "Bouncer";
info.iq = 80;
 
trace("My name is " + info.name + " and I am a " + info.occupation + " with an IQ of " + info.iq + "." );

They only problem with this is that you lose the length property on this Associative Array. If you try getting the length property it will come up as undefined. I looked this up and found that Senocular actually has a class that fixes this. That class is called AssociativeArray and it can be found here. When I first tried it I got an error at line 32. I didn’t read the class to figure out what was wrong, but I commented out the line throwing the error and it seems to work fine. Here is an example of that class in action. Read more …

Referencing the Document Class

Referencing the document class can be done in two ways. The first example uses brackets and a string. The brackets allows whatever is inside it to be ignored at compile time and used at runtime. If dot notation was used the compiler would try to find the function. It wouldn’t find the function and you would get a compiler error. The brackets hide it from the compiler and this is taken care of at runtime instead. The string, or the text in quotes, inside means that it is handled literally, letter for letter. This technique can also be used with a variable. This could be helpful if getting information at runtime. For example, if you are retrieving the name of a function from an external data source such as a XML file.

1
2
//example 1
root["functionReference"]();

The second example uses the Document Class name. This is very similar to dot syntax except the root keyword is put in parenthesis after the Document Class name. This technique can be used with properties and methods in the Document Class.

1
2
//example 2
DocumentClass(root).functionReference();

Loading… Please wait…

The Loader class is the Actionscript class made specifically to get JPG, GIF, PNG and SWF files from a remote source and load it into the main SWF file. The LoaderInfo class gives you specific information about the file being loaded. By using the bytesLoaded and bytesTotal properties of the LoaderInfo class it is possible to display the information in a number of ways.

Before it is displayed in these ways the preloader has to be written. Below is the step by step of a simple preloader. Two objects are required to start. A URLRequest and a Loader. The Loader is a new Loader instance. The URLRequest object accepts a string. The string is the file path to the file. Read more …

Beware Auto Format

Flash has some features to make coding easier. Some of the features include code collapse, code hinting, and commenting buttons. One of the other features is auto format which is extremely satisfying if your copy and pasting and your tabbing gets crazy.

If your code has no syntax errors auto format will indent it properly, add spaces and put your curly braces where you would like them to be. For even more control over auto format, you can change how it auto formats under preferences. Go to Flash > Preferences > Auto Format. There you can check off the auto format behaviors that best suit you.

There is one caveat to using auto format that could drive you to near madness if you can not prepared for it’s evil. It could add a line termination “;” to the class definition line. I’ve had it happen several times and I don’t know what makes it happen. I was unable to replicate it in a test. If it happens it will give you a 1084 error code in the Compiler window. It’s easily resolved, but it happened more than a few times to me, so I’d rather just be careful and use the tab key.

Colors and Numbers

There are 2 classes that deal with color. There is the appropriately named Color class and the ColorTransform class. Color is a subclass of the ColorTransform class. The Color class adds a few more features to the ColorTransform class such as control over brightness and tint.

A new instance of the ColorTransform accepts a lot of parameters. There is redMultiplier, redOffset, greenMultiplier, greenOffset, blueMultiplier, blueOffset, alphaMultiplier and alphaOffset. Personally, I find these values very counterintuitive. Give me good old HSB any day. All these crazy, meaningless numbers are additive, which means they are appended to the target’s current color value. If you have an exact color value in mind you can circumvent trying to figure out how to use all this stuff with the color property. The color property can be used with the Color class and the ColorTransform class. Below are simple examples of the usage of this. Read more …

Custom Right Click Menu

To make a custom right click menu, you will need to use 3 classes. First, use the ContextMenuItem class to make new contextual menu items. Then, make an empty context menu with the ContextMenu class. Lastly those contextual menu items need to be passed to the Array class.

Create a new variable and data type it to ContextMenuItem. Make as many new contextual menu item vars as you want. The new ContextMenuItem takes 4 parameters. The first is a string, written in quotes. This is the text that will appear in the menu. The rest of the parameters are determined by Booleans, true or false. The next parameter is separator before. False means no separator, true means include one. Then the next is enabled. Default is true or enabled, this parameter is optional. Last parameter is visible, default is true. Read more …

Introduction

I am an Actionscript beginner. I am a novice, a noob if you will. This is all true, but I am going to write about Actionscript anyway. Why you ask? Why would you write about something you are not an authority on? The reason that I am starting this documentation early is for posterity. I want to see my progression from an Actionscript beginner to an Actionscript badass. Notice I didn’t say guru. I am not peddling holistic Tibetan Goji Berries. Badass is the appropriate term. Which is what I would like to be one day.

I am not a total novice. I have background in Web 1.0. I even got the Web 1.0.3 update. Before Actionscript 3.0, I even put together some Flash sites with AS2. Those successes were generally what I would call copy and paste programming. I could find something that someone made close to what I wanted to do and then tweak the code a bit to get it to work. The logic of programming escaped me, but the sites got done and no one got wise to my programmatic fumbling. Read more …