Archive for the actionscript 3.0 Category

Storing SWFObject.addVariable() vars globally in AS3

Embedding variables with SWFObject is easy. In previous versions of Actionscript all the variables registered with the SWFObject were globally available within the SWF. From any class or object, you could call _root.theVariableName and access that variable.

However, moving to AS3, things are a little different. The SWFObject still embeds FlashPlayer 9 content without a problem – the SWF is properly embedded into the HTML and all the variables and parameters passed to the SWFObject are registered with the SWF. The only difference in AS3 is that these variables are not automatically available throughout your FLA.

Continue reading Storing SWFObject.addVariable() vars globally in AS3

Global Variables in AS3

EDIT — 09/03/09 I decided to update this article in response to some of the comments about the potential dangers of using global properties. Its a convenient way to store variables that are accessible from anywhere in your project, but with great power comes great responsibility :).

Yes, we know it can get sloppy, but sometimes is just makes sense to create variables that are accessible from anywhere in your project.

Prior to AS3 it was really easy… there was the _global object: _globals.myvar = 'mystring'. You are most likely reading this because you are trying to use the _global object in AS3. Ecmascript (the standard on which AS3 is based) does not support this notion, rather it forces you to create your own globally accessible object if you so desire. They are encouraging instance based data coupling, because it’s a lot more structured and less error prone. If you are working on a large application, I will encourage you to not use global variables (especially if unit testing is a requirement). However, if you don’t give a damn about what us programming snobs say, have no idea what unit testing is, or just want to use some global variables in AS3, then don’t feel bad, they can be quite handy!

Continue reading Global Variables in AS3

onReleaseOutside in AS3 using DisplayObject.stage.addEventListener

I am working a few basic wrapper classes for my AS3 library. I am essetially using the Decorator pattern to wrap display objects with, for example, draggable resizing functionality. As I was creating my ResizableSprite class, I came to a point where the onReleaseOutside event of old would have really come in handy.

Needless to say, onReleaseOutside is no longer an event in AS3. Simple enough to get around, right? Can’t we just set a MouseListener to check for MouseUp event? The answer, depending on your Actionscript background is both yes and no. In AS2 the Mouse object was global, meaning that any line of code in the application (no matter what frame, object, class, etc… it was written in) that referenced the Mouse object all talked to the same object. Things have changed in AS3; each Display Object (SimpleButton, Sprite, MovieClip, TextField… basically anything drawn on the stage) has its own internal Mouse object, that only reacts when the mouse is over that object. So, in the case of onReleaseOutside, clicking on the object will trigger an event, moving off that object will trigger an event, but releasing the mouse off of the object will be ignored and won’t fire an event.

Continue reading onReleaseOutside in AS3 using DisplayObject.stage.addEventListener

Drawing Sounds with AS3

A while back I made my first attempt and create a sound visualizer in AS3. It was a pretty feeble, albeit necessary step toward making something that I looked cool and ran smoothly. Over the last couple of weeks, I have been playing around with a visualizer in my spare time, and finally have something I think is worth another post.

This time around, I am actually loading in a sound and getting its audio spectrum data. This is farther than my last, graphic-test only attempt. The new Sound model in AS3 is a bit different than previous versions, but it is not very difficult to understand. The major differences are a result of the new object hierarchy of the Sound package… and when you think about it they are intuitive changes. For the sake of this example, I won’t get into the nitty gritty of all the new classes in the Sound package, but keep it to the ones I am using for my visualizer. I am using:

  • a Sound object (the sound’s raw data)
  • a SoundChannel object (a reference to the playing sound)
  • the SoundMixer object (a static class that controls all the SoundChannels in use)

The first step in creating a visualization of a playing sound is to… play the sound! Instantiate a new Sound object and pass it a URLRequest object that points to the URL of the sound file you’d like to load.

Ex.)

_sound = new Sound(new URLRequest('./your.mp3'));

Once the audio file is loaded (at least enough to start playing), go ahead and create a SoundChannel object by calling the Sound.play() method.

Ex.)

_channel = _sound.play();

Once the sound is playing, call the SoundMixer.computeSpectrum() method to get a data snapshot (ByteArray) of the current waveform comprised of all the playing SoundChannels.

var spectrum:ByteArray = new ByteArray(); 
SoundMixer.computeSpectrum(spectrum);

I won’t pretend to know the ins and outs of the new ByteArray class in AS3… I actually find it a bit confusing. All I seem to have figured out is that running a loop through 256 values and calling the ByteArray.readFloat() method on the spectrum variable will give you 256 unique values spanning the waveform for the left channel of the playing sound. Calling the exact same loop/readFloat() combo again will give you 256 unique values spanning the right channel of the playing sound. Once you understand how to get that data and relate it to the playing sound, visualizing the sound is pretty easy.

In my example, I am doing some extras with the graphics of my visualizer. I am drawing each channel separately, and I am applying some Bitmap drawing technics to give each channel line its own trailing effect. Then I am drawing the bitmaps to the stage to give they effect of trailing motion and allowing the visual sound data to linger on the stage. There are a lot cooler looking visualizers out there, but hopefully if you are looking to get into this kinda stuff, this example can help you out a bit.

Here it is:

source files. Beat by PROT

Cooking with AS3: Dynamic Fire

A while ago I made a fire effect with ActionScript 3.0. It was a good exercise in understanding actionscript’s Perlin Noise filters and the new way to draw shapes and gradients at runtime in AS3.

notextile..

source files