Tony Polinelli
Tarwin Stroh-Spijer

contact [at]

6/25 Easey Street
Collingwood, 3066
Vic, Australia

+61 3 8060 5321

JSFL: Select all classes of the selected type on layer

I've been playing with JSFL a little recently. It was really hard to begin finding any resources on how to use it and those I did find were pretty sparse with information. So to help the community out I'm going to start posting my experiments, some useful, some only good for me, for everyone to take a look at, take apart and generally learn from.

The first one is one of the simplest. It lets you select all Elements of the same class on the stage. Simply select one of them, say a RadioButton, then choose the TMP - Select Same Class from the Commands menu and all other RadioButton instances should be selected.

I've been using this in a game I've been creating. I just lazily drag a bunch of different enemies onto one layer, then when I want to seperate them onto different layers just use this command. Easy. So here it is.

  1. doc = fl.getDocumentDOM();
  2. s = doc.selection;
  3. if(s.length == 1){
  4.     type = s[0].libraryItem.linkageBaseClass;
  6.     var tl = doc.getTimeline()
  7.     var elts = tl.layers[tl.currentLayer].frames[tl.currentFrame].elements;
  9.     a = [];
  10.     for(i=0; i <elts.length; i++){
  11.         if(elts[i].libraryItem.linkageBaseClass == type){
  12.             a.push(elts[i]);
  13.         }
  14.     }
  15.     doc.selection = a;
  16. }

Yeah, stupid directory I know. I'll show you how to make an MXP installer in a later post, to make it easier to send these things around.

The "fl." holds all the functions that a Flash specific in JSFL. The rest of the code is just plain old Javascript, so if you need help with that just do a search on the net for general Javascript documentation.

Firstly we create a variable doc to make it easier to access our current document. Then s which holds an array of all the Elements currently selected. We check to make sure that we've only selected one Element (we could at this stage do an fl.alert("message") to warn that there was more than one item selected. If there is only one item selected we get the type using the .libraryItem.linkageBaseClass and then set another variable tl to make it quick to access the current timeline.

The next bit is a bit complicated. I get all the elements on the current layer, in the current timeline and put them into the array elts. If someone wants to update this I'd suggest the first place to start would be to get it to select elements on ALL layers. Would be useful.

I then loop through the array of Elements and add them to a temporary array a, but only if their linkageBaseClass ist the same as the initially selected one (it's just a simple String).

Lastly, set the document selection to the temporary array of Elements.

Tags: ,

One comment

  1. Nick

    This is an old-ass post, i'm aware, but thanks for pointing out that the selection can be handled and manipulated like an array. Solved a lot of my issues!


Leave a Reply

Our Friends:

Powered by haXe / poko cms | Copyright 2008-09 TouchMyPixel