views:

30

answers:

3

Hi everybody ::- ).

I'm trying to add some scroll bars to a Sprite using ScrollRect. The Sprite starts empty and when I add something at 500, 500, it should scroll, as it is outside the viewport. Unfortunately, adding a 10x10 Sprite at 500, 500, doesn't make my Sprite change its size to 510, 510, but to 10, 10 ::- (.

We all know that DisplayObjectContainers resize according to what's put in them. But I didn't know up until today that if you put something at 500, 500, the Sprite / DObjContainer will NOT resize from 0, 0 to 500, 500, but simply jump directly at 500, 500. Annoying, as I cannot put Scrollbars on something whose content fluctuates so. I need 0, 0 to remain 0, 0 and if something is placed at 500, 500, the sprite's height should increase with 500 + that something's height.

Ideas? Insight?

Thank you in advance ::- D, Scrollmasters ::- D.

+1  A: 

quick fix, maybe not too elegant... add a Shape in your Sprite with a fixed height of 500 and set the alpha to 0 or use it for a nice colored background :)

PatrickS
Hi. The height/width is variable depending on the content. However, I might do such a less elegant workaround if nobody offers a better solution. I could do a rectangle from 0, 0 to the X/Y of the lowest/rightest Display Object.
Axonn
PatrickS
+1  A: 

I made it this way:

Container (sprite)
    scrollbars
    content (sprite with scrollrect)

In content sprite, draw a rectangle to give it full size.

alxx
Hi. Heh, that's sort of workaround I was trying to avoid, thinking there is some other, more elegant method. But yeah, sure, if no other elegant solution shows up, that's what I'm going to do.
Axonn
AFAIK, that's how sprites work. They can't be resized with no content (Sprite is not Canvas).
alxx
Could be, yes. To me, it's a bit ilogical. I am not trying to resize it, merely for it to respect the 0, 0 of the coordinate space. If I add something at 40, 40, then it should cover the area between 0, 0 and 40, 40, or at least some setting to allow me to tell it to do so. What I'm trying to say is that they should have implemented this "workaround" internally and allow a property or something to turn it on. Might have been overhead though... Oh well...
Axonn
+1  A: 

This will not change the height/width of the Sprite so I'm not sure it will help you. But in case you control the code that does the scrolling, you could calculate the size of your object (considering its origin is always 0,0) by getting it bounds and adding x + width and y + height. Something like this.

var rect:Rectangle = sp.getBounds(sp);
var w:Number = Math.abs(rect.x) + rect.width;
var h:Number = Math.abs(rect.y) + rect.height;

I'm using Math.abs in case you add children at negative offsets, but from your description that's not the case, so you could avoid that step.

Juan Pablo Califano
This is indeed the correct solution and, most importantly, NOT a workaround. I've been directed to the getBounds function by somebody at actionscript.org. Thanks for also providing the correct way to implement it (which I discovered by myself last night after a few sniffing around the properties of getBounds. And yes, I control the code that does the scrolling. I managed to finish everything and it works nicely. Am very satisfied that I managed to avoid workarounds ::- D. Thanks.
Axonn
@Axonn. Cool. I'm glad you solved your problem.
Juan Pablo Califano