previous

'actionscript' tag

Add Filters to Display Objects

0

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

0

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);
    }
}

Little Thing About Arrays

0

This drove me crazy because the error messages weren’t related to this … but when you make an array in Actionscript you need to initialize it create an instance of it to make it useful. In retrospect this is completely obvious, but I didn’t pick up on it. Sigh.

// -- DOES NOT WORK:
var myArray:Array; // not initialized for use as an array
myArray.push('lemonade');

// -- WORKS:
var myArray:Array = new Array(); // for the love of god, do this.
myArray.push('pretzels');

I was running into a problem where none of the array methods (eg: pop/push/unshift/etc/) were working because I forgot to do this. I am an idiot and wasted a lot of time.

Interestingly, you can do this with no problems:

var myArray:Array; // not initialized for use as an array
myArray = ['red', 'green', 'blue', 'ass']; // array literal format

stage.width vs stage.stageWidth

1

I was trying to figure out how to access the width of the stage as it is when you make a document in Flash (defaults to that lovable 550px wide). Not only that, but I was trying to do it from an external AS document (ie. an AS file that wasn’t the document class); long story short about that — any external AS file that extends MovieClip or Sprite is ultimately just a far-away subclass of the DisplayObject class; therefore,

stage.width

or

stage.stageWidth

works in external AS docs AFTER the class has been added to the stage (that’s the gotcha!).

OKAY, so: 

stage.width

(or height) gives you the width of everything currently on the stage. If you’ve got nothing on the stage,

stage.width

returns 0. It was incredibly frustrating.

Moral of the story:

trace(this.stage.width); // width of everything currently on the stage (could be 0)
this.stage.stageWidth; // width of the stage (defaults to 550)