next

'actionscript' tag

Creating Instances from the Library with Strings

0

Problem: while working on an AS3 document you uncover an unusual situation in which you have a string class name that corresponds to something in the library. How does one do it?

I’ve come across this a number of times and found myself dropboxing this snippet so I would always have it with me …

import flash.utils.*;

/**
 * Takes a string classname, returns an instance of that class, if it exists.
 */

private function newInstanceFromString(className:String):*{
    try{
        var classRef:Class = getDefinitionByName(className) as Class;
        return new classRef();
    } catch( e:ReferenceError ){
        trace('Error: there was a problem creating an instance of the "'+className+'" class. Check your library and linkage to ensure it\'s there.');
        trace(e+"\n");
    }
}

See the AS3 livedocs for more information about getDefinitionByName, the super helpful method that makes it all work.

Tinting a Display Object

0

Tinting a display object in AS3 is remarkably easy:

// EXAMPLE 1: tint a display object
import fl.motion.Color;
// [..]
var tint:Color = new Color();
tint.setTint( 0x0099ff, 1 );
myobject.transform.colorTransform = tint;

You’ve got two options for resetting the colour, store the original colour or create a new Color object. The following example resets the colour of a display object when it is hovered over.

// EXAMPLE 2: reset with a new Color Object
import fl.motion.Color;
import flash.events.MouseEvent;
// [..]

// call the hover() function when we hover over the display object:
myobject.addEventListener(MouseEvent.ROLL_OVER, hover);

private function hover(e:MouseEvent):void{
    e.currentTarget.transform.colorTransform = new Color(); // removes tint
}

Alternatively, you can store the original “colour” of the untinted in a variable before you tint it. The following trivial example demonstrates this, but you wouldn’t see any visible change on the stage (FYI).

// EXAMPLE 3: reset the tint by storing the original value
import fl.motion.Color;
// [..]
var tint:Color = new Color();
tint.setTint( 0x0099ff, 1 );

var originalColour:Color = myobject.transform.colorTransform; // store the original colour.

myobject.transform.colorTransform = tint; // tint the object blue
myobject.transform.colourTransform = originalColour; // tint the object back to its original colour

AS3 Mouse Cursor

0

Movieclips with mouse handlers that make them behave as buttons no longer automatically use the lovable hand cursor. Hence the need for these two properties to be set to true on your DisplayList object!

buttonMode = true;
useHandCursor = true;

Removing Everything from a Display Container

0

I ran into this problem and couldn’t figure out what was going on because I fail miserably:

// doesn't work because as this code executes numChildren is decreasing;
// it ends up not removing everything
for(var i:Number=0; i < numChildren; i++){
  removeChildAt(i);
}

The solution:

while( numChildren > 0 ){
  removeChildAt(0);
}

This expression is useful for deleting things from the bottom of the displayList up.

next