Beware scrollRect: Seems great, can be nightmare.

freddy.jpg

We here at squidder have a love/hate relationship with scrollbars. Partly because, while seemingly simple, they can overly complicated very quickly.

So when we wrote our latest scrollbar class, we decided to try out scrollRect. Seemed pretty strait-forward – just pass in a rectangle and it takes care of all the masking, offset and everything. Way more efficient than setting a mask and then moving that about… or so we thought. (cue dramatic crescendo)

The big problem arrises when you want to change the size of the content (say a text field) and you want to update your scroll bar accordingly. Any height (or width) measurements you make return the value of the scrollRect rectangle (including getRect and getBounds). And the kicker is that if you unset the scrollRect, it takes a full frame before you can measure the real height again. So there’s no way to make an instantaneous measurement and update the scrollbar without a lag. Imagine our rage!
Once we found and punched a couple of monkeys, we pulled a totally bogus hack, which is to reset the scrollbar and pass in the textHeight of the textfield we were scrolling. This of course limits the usefulness of our class, as we can only scroll textfields instead of any displayObject we want.
The truth is out there. We’re still looking for answers and will update if we find it (and if you know, definitely pay it forward). But be warned that scrollRect may wind up causing more frustrations than it’s worth.

Category: as3

Tagged:

5 Responses

  1. Michael Corrin says:

    I have experienced the same problem. It is driving me nuts. I almost tossed the laptop out the ol’ window this morning.

    If I figure out a solution before you post one, I will post again. Good luck.

  2. Visual Asparagus says:

    this also drove me up the wall!!!

    after using a shadow TextField not on the display list as a sort of measuring device, i tried another trick that worked much better and will allow me to use it for a scrollPane as well. if the TextField is in a container sprite, then the TextField instance still retains is correct height and width, even though the container sprite [ with scrollRect ] indicates another height or width.

    this simple example shows what i mean.

    cClip = new Sprite();

    cClip.graphics.beginFill(0×000000);

    cClip.graphics.drawRect(0, 0, 100, 300)

    cClip.graphics.endFill();

    pClip = new Sprite();

    pClip.addChild(cClip);

    pClip.scrollRect = new Rectangle(0, 0, 50, 200);

    addChild(pClip);

    Main.LOGGER.debug(pClip.height+” :: “+cClip.height);

    good luck!

  3. Joe Wheeler says:

    I’ve (finally) found a pretty reliable fix for the scrollrect height, width problem. http://usecake.com/lab/find-the-height-and-width-of-a-sprite-with-a-scrollrect.html

    Hope this helps,
    J

  4. Onepunch says:

    1. Create a MovieClip and set it’s scrollRect
    2. Add a sprite to the movie clip
    3. Add a bunch of content to the sprite
    4. If the contents of the sprite change, update your scrollRect using Sprite.height
    5. ????
    6. Profit!

    Don’t be dumb.

Leave a Reply

Powered by WP Hashcash

Spam Protection by WP-SpamFree

Twitter