PDA

View Full Version : [AS3] scrollRect Weirdness



sarahelizabeth
April 10th, 2007, 04:36 PM
I'm getting some weird behavior while trying to build a scrollbar in as3. I've applied a scrollRect to the movieclip I want to scroll:


public function create(targ, w, h):void{
trace('Scrollbar: Create');
this.targ = targ;
this.w = w;
this.h = h;
trace("targ.height is: "+targ.height);
this.targ.scrollRect = new Rectangle(0, 0, this.w, this.h);
trace("targ.height is: "+targ.height);
scrollpos=0;
checkComponents();
}
Both the traces before and after trace out the correct value, in this case 267. However, once I call any sort of mouseEvent (applied to the targ, the arrows used for the scrollbar... etc) it traces the height of targ as 125 - the height of the scrollRect. Is this a bug? Am i missing something about scrollRect?

Thanks,
Sarah

rabidGadfly
April 10th, 2007, 05:44 PM
Do you get a different result if you trace this.targ.height instead of targ.height?

sarahelizabeth
April 10th, 2007, 05:44 PM
nope, same response.

I also tried it with e.target.height and e.currentTarget.height, within the event handlers that were on targ.

blockage
February 24th, 2009, 08:37 AM
I've found a solution to most height / width problems caused by scrollRect (http://usecake.com/lab/find-the-height-and-width-of-a-sprite-with-a-scrollrect.html). The trick involves using pixelBounds, but also requires a bit of mucking about with tranformation matrices.

The solution looks a bit like this...

ActionScript Code:

/**
* This function works like DisplayObject.getBounds(), except it will find the full
* bounds of any display object, even after its scrollRect has been set.
*
* @param displayObject - a display object that may have a scrollRect applied
* @return a rectangle describing dimensions of the unmasked content
*/
public function getFullBounds ( displayObject:DisplayObject ) :Rectangle
{
var bounds:Rectangle, transform:Transform,
toGlobalMatrix:Matrix, currentMatrix:Matrix;

transform = displayObject.transform;
currentMatrix = transform.matrix;
toGlobalMatrix = transform.concatenatedMatrix;
toGlobalMatrix.invert();
transform.matrix = toGlobalMatrix;

bounds = transform.pixelBounds.clone();

transform.matrix = currentMatrix;

return bounds;
}


Sorry for the cross post.

J