Archive for the 'ActionScript 2.0' Category

The VideoPlayer Class API (aka, the Missing Manual)

Tuesday, February 27th, 2007
ActionScript 2.0 Quick Tips

I was just gabbing with someone about the NetStream class, which nudged my mind toward the VideoPlayer class.  According to the Components Language Reference, the FLVPlayback class “extends the MovieClip class and wraps a VideoPlayer object.”  As it turns out, the VideoPlayer class is mentioned in the Components Langauge Reference, but if you dig into it, you’ll quickly discover that none of the class members is hyperlinked.  In other words, you’ll get an overview of the properties, methods, and events defined by that class, but no actual explanation.

I find that an odd omission — but Adobe does make the full API available.  Keep reading »

Loading and Tracking Multiple Files at the Same Time (Part 1)

Sunday, February 25th, 2007
ActionScript 2.0

Not long ago, a blog guest entered into conversation with me about loading and tracking multiple files at the same time (see Event Handlers versus Event Listeners comments, starting with the eighth comment).  Tiemen was hoping for a way to use the MovieClipLoader class inside a custom ActionScript 2.0 class, not only to load external files — which isn’t especially difficult — but also to track each file individually; that is, to be able to pass individual functions into the custom class, and have that class manage the tedium of assigning the passed-in functions to the MovieClipLoader events of each file.  This particular requirement was the tricky part.  If the custom class merely had to manage event handlers for a single MovieClipLoader instance, that would be easy.  (In fact, this is completely possible.  Be aware, if you choose to read the other entry’s comments, I mistakenly believed at the time that the events of a single MovieClipLoader instance were not capable of providing useful information for more than one loading file at a time.  I’ve noted those errors in my replies.)  Because Tiemen wanted the ability to assign separate functions for each file’s events, well … that meant maintaining an array of MovieClipLoader instances.  Let’s take a look at one way to accomplish this goal.  Keep reading »

Code Hinting Regardless of Naming Conventions

Wednesday, February 21st, 2007
ActionScript 2.0 Quick Tips

Depending on my mood, I may precede certain variable names with a small prefix that describes the type of object they point to.  For example, I may give a movie clip the instance name mcBall, rather than just ball.  Why?  Well, it allows me to see at a glance that I’m dealing with a MovieClip instance, which can come in handy during coding and also while I’m poking through the Debugger panel.  It doesn’t have any measurable effect on the functionality of the variable … it’s just one of those things you get used to.  I certainly don’t always adhere to this convention, but when I do, I’m practicing something called Hungarian notation, which has a decent pedigree (at least, in computer years).

Flash provides at least one naming convention that actually can make a practical difference, if you follow the suggested suffixes in the “About using suffixes to trigger code hints” section of Learning ActionScript 2.0 in Flash.  I’m not especially a fan stylistically, but, for example, if I name that ball clip ball_mc, I’ll get automatic code hinting for the MovieClip class (and so will you) in ActionScript 1.0 and 2.0.  A full list of suffixes is listed in that section.  Code hinting is definitely a useful tool, because I’m not always familiar with the class members of the object at hand.

What if you don’t like suffixes?  Or prefixes, for that matter?  Well, if you use AS2’s strong typing syntax (the :Number in something like var total:Number = 5;), it doesn’t matter what you name your variable:  you’ll get code hinting if you want it (see File > Preferences > ActionScript).  That’s fine for everything but movie clip instance names, which aren’t necessarily declared as variables.  But … see, if you declare instance names anyway — even though you don’t need to — you get the benefit of code hinting regardless of the instance name.

With a simple line like this …

var ball:MovieClip;

… even though you haven’t set that instance to anything, you’ll get MovieClip-centric code hints for subsequent references to that instance name in your code.

updateAfterEvent() … What’s the Scoop?

Monday, February 19th, 2007
ActionScript 2.0

Someone asked me the other day, “What’s the deal with updateAfterEvent()?  What, exactly, does it do, and when should it be used?”  Well, I’m a fan of the ActionScript 2.0 Language Reference, so my usual reply is along the lines of, “Let’s check out what the ASLR has to say,” but in this case, the documentation doesn’t speak the whole truth.  Keep reading »

The Displacement Map Filter Demystified

Friday, January 26th, 2007
ActionScript 2.0

Flash Professional 8 introduced a number of exciting new visual filters — such as drop shadow, blur, glow, and bevel — which reproduce many of the corresponding filters of Photoshop and Fireworks.  Of these, most are available via the Property inspector’s Filters tab.  All filters are accessible to ActionScript, but a few actually require programming.  One of these is the DisplacementMapFilter class, which distorts images based on the colors or transparency in a special reference map.

I just started a new article series on Community MX to explain the displacement map filter in visual terms.  My intent is to illuminate the somewhat overwhelming sample code provided in the ActionScript 2.0 Language Reference.  The first article is free content, and teaches what the DisplacementMapFilter class is and how to use it to reposition pixels in an imported graphic file.  In future installments, you will learn how to produce a handful of very cool visual effects that are only possible with ActionScript, including a magnifying class, fisheye lens, and ripples.

http://www.communitymx.com/content/article.cfm?cid=97280

Sound.position “Gotcha” with Multiple Calls to Sound.loadSound()

Tuesday, January 23rd, 2007
ActionScript 2.0

The Sound.position property indicates how far along a Sound instance has played.  If you’ve loaded an external file or attached an embedded file at runtime, but haven’t yet started it, that instance’s position property is 0.  If it’s a 10-second clip and you’re 2.5 seconds in, the property reads 2500 (that’s 2,500 milliseconds).  When your audio reaches its end, position will match that instance’s Sound.duration property, which indicates the total length of the audio.  This is a can be useful for checking when a sound has concluded — of course, the Sound.onSoundComplete event is much more straightforward — but there are any number of reasons you might want to keep tabs on a sound’s position.  Unfortunately, this property doesn’t always report the value you may expect.  Keep reading »

Troubleshooting Tales:  The Case of the Mysterious Array.sortOn() Call

Friday, January 19th, 2007
ActionScript 2.0

Not long ago, I wrote an article for the Adobe Dev Center on handling cue points for external audio files.  The article shows how to extend the native Sound class in ActionScript 2.0 and 3.0 to add support for a custom cuePoint event.  In my SoundSynch.addCuePoint() method, I used Array.sortOn() to assure the growing list of cue points stays in chronological order.  Here’s a quick look at the AS2 version:

// Add Cue Point
public function addCuePoint(cuePointName:String,
  cuePointTime:Number):Void {
  _cuePoints.push(
    {
      type: "cuePoint",
      name: cuePointName,
      time: cuePointTime,
      target: this
    }
  );
  _cuePoints.sortOn("time", Array.NUMERIC);
}

The Array.sortOn() method accepts two parameters:  first, the name of the property on which to sort; second, and this is optional, the manner in which to sort.  In this case, I wanted to sort numerically, so I specified Array.NUMERIC.  Now, it gets fun.  Keep reading »

setTimeout() “Gotcha” in Class Files

Tuesday, January 16th, 2007
ActionScript 2.0

The setTimeout() function is a valid citizen in the realm of ActionScript 2.0; it was simply left off the roster for some reason, so you won’t find it in the documentation.  It works very much like its JavaScript counterpart and is less cumbersome to use than setInterval() for triggering a single delayed action.  You can reference setTimeout() just fine in timeline code, but I found an unexpected problem when employing this function in a class file.  Its presence halted the compile process and caused all sorts of misleading errors, such as the idea that Stage can’t be reference in a class file (it certainly can).  Is there a workaround?  Yes.  Keep reading »

A Tip on the Boolean() Function (Casting as Boolean)

Tuesday, January 9th, 2007
ActionScript 2.0

When you load data from XML or text files, or retrieve values from the TextField.text property, the information you get is a string.  Even if the incoming value is, say, the numeral 3 (without quotes), it’s a string when evaluated by ActionScript.  Even if the value is “false” (with or without quotes), it’s a string — not a Boolean — and may appear to ActionScript as true!  Whoa!  That could cause a few problems.  In the case of numbers, there’s an easy way to tell ActionScript what the datatype should be, and it’s called casting.  You may cast a string numeral into an actual Number datatype by using the Number() function.  With a text field whose instance name is money

var looseChange:Number = 0;
looseChange = Number(money.text);

That converts the value of the money.text property to a true number, which you can verify with trace(typeof(looseChange)); — but the Boolean() function handles things differently.  Keep reading »

Simplify:  Use Boolean Expressions Creatively

Wednesday, January 3rd, 2007
ActionScript 2.0

I was coding up a slideshow this afternoon for a quick demonstration to a client.  In the end, much of today’s “rough draft” ActionScript will be converted into a custom SlideManager class.  For the time being, though, my “jump in and throw something together” approach was helpful anyway, because it brought to mind a number of features the client is going to ask about — I’d bet money on it — and now I’ll be prepared.  In addition, it reminded me of an admirable principle in programming:  elegance.  Keep reading »