Search code examples
javascriptflashfor-loopjsfl

JSFL for...in loop doesn't seem to work


I'm writing a script in JSFL for Flash CS5, and I'm trying to get a list of layers off the main timeline. I'm getting the timeline, then looping through it all with a for...in loop, but the objects I'm getting seem to be undefined. Here's some test code I made:

alert(fl.getDocumentDOM().getTimeline().layers[0].name); //Returns "text1"

for(layer in fl.getDocumentDOM().getTimeline().layers) {
    alert(layer.name); //Returns "undefined"
}

So, does JSFL not support for...in? That's kinda odd, since it seems it's just a JavaScript engine.


Solution

  • Whoooh there. JSFL is not just a JavaScript engine, it is bizarro world JavaScript which can be remarkably unpredictable. Don't believe me? Not sure if this is still the case, but try fl.getDocumentDOM().selection.push(<obj>). It didn't work, but this did: var s = fl.getDocumentDOM().selection; s.push(<obj>) fl.getDocumentDOM().selection = s.

    That said, your syntax is off:

    var layers = fl.getDocumentDOM().getTimeline().layers;
    // include 'var' it's good taste
    for(var layer in layers) {
        // for... in iterates the KEYS, so you actually have to do a lookup
        alert(layers[layer].name);
    }
    

    As an aside, you're better off iterating through arrays with numeric indexes, it is clearer and faster.