AS3 Mouse Cursor

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

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.

Add Filters to Display Objects

Flash’s built-in filters (eg. blur/drop shadow) can be applied to display objects and bitmaps, though frankly it feels much more straightforward when you’re applying them to a display object instead of a bitmap. But I haven’t done anything with bitmaps yet … so …

All the filters are part of the flash.filters.* class.

// must import above the class definition
import flash.filters.DropShadowFilter;
// -- [..]

var myClip:MovieClip = new MovieClip();
var shadowFilter:DropShadowFilter = new DropShadowFilter(); // takes a lot of arguments ...
myClip.filters = [shadowFilter]; // filters is an instance variable from the DisplayObject class -- 'tis an Array

// if this was a bitmap (as opposed to a DisplayObject) we'd need to do applyFilter() or something

Sifting Through the Display List to Delete Things

Hah, it’s a pretty specific case … but this checks the last item of the display list and removes it if it partially matches (indexOf) the name on the display list item. This requires that the display list item to be given a name.

var myClip:MovieClip = new MovieClip();
myClip.name = "thenameofit";

if( getChildAt(numChildren-1).name.indexOf("thenameofit") > -1 ){
    removeChildAt(numChildren-1)
}

Yeah. Heh. So I’m keeping this code around, why? It could be used in the program I’m writing now, but it’s not nearly flexible enough as this, which loops through the display list and removes any child with containing a partially match of “deleteme”. Much more flexible, but also more intensive in a complex movie?

for( var i:int=0; i < this.numChildren; i++ ){
    if( getChildAt(i).name.indexOf('deleteme') > -1 ){
        removeChildAt(i);
    }
}
Older Posts