Little Thing About Arrays

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

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)

Find Anchors

Handy little function … finds an anchor and returns its text, or returns false.

function findAnchor(){
    var url = document.location.toString();
    if( url.match('#') ){
        return url.split('#')[1];
    } else {
        return false;
    }
}

JS Object Iterator

Extremely handy when you’re dealing with an Object and have no idea what it’s methods or properties are. ESPECIALLY when there’s no documentation and you get an alert saying [Object] or whatever.

function deconstruct(what){
    obj = eval(what);
    var temp = "";
    for (x in obj)
            temp += x + ": " + obj[x] + "\n";
    alert (temp);
}

I wish I remember the source for this because it has to be one of the handiest functions ever.

Newer Posts