$$() selector examples?
  • HughCHughC May 2012

    I'm struggling to get anything beyond the most basic (read: targeting the current timeline) element selectors working.

    As soon as I try to use the second context param, it fails, whether a string, or a library item (see code below) it fails.

    Each time, all I get is a silent JS error.

     

    function checkContents(item, index, items) {
        var collection = $('*', item);
        collection.list();
    }
    var objects = $$('/characters/*:movieclip');
    objects.list();
    objects.each(checkContents);
    trace("done");

    in the above example objects list() is successful, and other trace statements indicate it's dying on the $ selector (ie collection.list() never produces output. Any suggestions?

  • DaveDave May 2012

    Firstly, if you're using CS5 - don't! It eats all JavaScript errors so is effectively useless for debugging problems. Use CS4 instead.

    Secondly, your first selection using the item selector function looks good.

    However, I'm pretty sure that the 2nd context param on both selector functions is still a WIP right now (sorry). Right now, you'll need to do the following:

    function checkContents() {
    var collection = $('*');
    collection.list();
    }
    $$('/characters/*:movieclip').exec(checkContents);

    This actually forces flash to physically go into the timeline on each item.

    Or:

    function checkContents(item, index, items) {
    var timeline = item.timeline;
    // do something with timeline
    }

    I'll have a proper look later though, and if I can, will move this up the priority list, as it's something I'd like to get cracked as well.

     

  • HughCHughC May 2012

    thanks for that Dave, it set me on a more useful path; I've refined the script somewhat, it now looks like this:

     

    function checkContents(item, index, items) {
        var collection = $('*:text');
        collection.attr( 'y', function(element, index, elements){ return element.y - element.getTextAttr("size")/8; } );
    }
    
    var objects = $$('*:movieclip');
    objects.exec(checkContents);
    trace("done");
    
    What I'm tryng to do here is adjust all the y positions of my type to account for some Mac / PC font differences / baseline shifting. But the result I'm getting is pretty looney-tunes. I see the UI update at lightning speed as it iterates through, but many text blocks remain unchanged, while I see others march up the screen, having many y updates applied to them one after another. Should the selector, at least, operate on all frames within a timeline?
    I appreciate your comment re: CS5 vs CS4, but I'm at a loss what to do, backsaving is so unfriendly, and my animator collaborator is particularly concerned that bouncing back and forth between versions is going to cause us more heartburn than just sticking with just the most recent one. 
    Has anyone loaded xJSFL up in CS6? Is the situation re js error reporting any better?

     

  • DaveDave May 2012

    Yeah, I feel your pain.

    Unfortunately, develpment this side has really slowed to a crawl due to other commitments, so each time I go back to the code I have to get my head into it again. As a framework developer you do your best to code with general situations in mind, but sometimes you just don't foresee someone else's needs - but a small update can fix this.

    FYI, the selector $() function only operates on a single frame, but all layers. if you want to operate on all frames, you might be better off using the Iterator class, which is designed be a OO solution to replace multiple nested for loops:

    function frameCallback(frame, index, layer)
    {
    var collection = $(':text', frame.elements);
    collection.attr('y', function(element){ return Math.round(element.y - element.getTextAttr('size') / 8); } )
    }

    var context = Context.create();
    Iterators.layers(context, null, frameCallback);

    If this doesn't work for you, feel free to zip your .fla and send it my way.

    Unfortunately, Iterators, Context and Selectors *are* all linked, but the links between them are still "not quite finished", and a few bugs need to be fixed, along with some bits needing to be more intuitive. It's also in the ToDos to pass in a Layer context to the $() function. Anyway - another item on my immediate (whenever that is) to do list, along with properly-behaving nested selectors.

    Regarding the "unchanged" blocks, I think this is something I've seen before. Try a Math.round() on the result and see if that makes a difference.

    No idea about CS6 yet, but I should find out.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with OpenID Sign In with Google Sign In with Twitter

Sign In Apply for Membership

Tagged