Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How do I get the bounds of a sprite? — Gideros Forum

How do I get the bounds of a sprite?

SnowThiefSnowThief Member
edited July 2012 in General questions
Hi.

Some functions return a sprite and I can use getWidth() and getHeight() to get the width and height of that sprite, but how can I get the bounds of it instead?

"Sprite:getHeight()

Returns the height of the sprite, in pixels. The height is calculated based on the bounds of the content of the sprite."

local MyTextField = TextField.new(MyFont, "LLLyyy")
MyTextField:setPosition(100, 100)
stage:addChild(MyTextField)

How do I move this text to the upper left corner of the stage? setPosition(0, 0) doesn't work, as that puts most of the text above the stage.

Right now I do this:

MyTextField:addChild(PixelAtRow1000)
MyTextField:setPosition(MyTextField:getHeight()-1000, 0)
MyTextField:removeChild(PixelAtRow1000)

But isn't there a better way, like a getUpperBound() function?

Comments

  • Ok normally my response would be search is your friend but I have since found that the vanilla search facility is not the best. Using google for searching seems to work better and searching for
    site:http://www.giderosmobile.com/forum "Sprite:getBounds"
    brings up 2 results. I think what you require is here (thanks to @atilim for the heads up), which is the first of the results returned by google.
  • Ah, yes, that is exactly what I need, thank you very much! :)
  • atilimatilim Maintainer
    edited July 2012
    Also text fields are aligned to the baseline http://en.wikipedia.org/wiki/Baseline_(typography) Therefore when you get the bounds with Sprite:getBounds you will get negative y position.

    Hopefully I'll introduce setAnchorPoint function for TextFields so that you'll be able to align the texts to the ascender.
  • Yes, I read about that, but personally don't need it now that I have getBounds(). :)

    Thank you very much for your time. :)
  • For those that don't know - currently Sprite:getBounds(parent) is an undocumented function (as in I think it got forgotten!), I shall attempt to rectify that here!

    Sprite:getBounds(parent)

    Returns the screen bounds of the sprite (4 values) relative (I suspect) to the specified parent (usually "stage"), VERY useful for working out the bounds of complex Sprites (or centring text)

    Returns:
    x,y,width,height

    eg.
    local myImg = Bitmap.new(Texture.new("Img.png"))
    stage:addChild(myImg)
     
    local x,y,w,h = myImg:getBounds(stage)
    print(x,y,w,h)

    Likes: unlying, kae

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.