Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
How can I set and use the anchor point of an empty sprite (used to hold a group)? - Gideros Forum

How can I set and use the anchor point of an empty sprite (used to hold a group)?

I can’t figure out how set the anchor point of an empty sprite used to group elements together. Here’s the code I’m trying to get to work (and understand why it does what it does).

local bgGroup = Sprite.new()
--create a group to hold background elements

bgGroup:setAnchorPoint( 0.5 , 0.5 )
--set anchorPoint to middle of group

print("Group anchor ", bgGroup:get("anchorX"),bgGroup:get("anchorY") )
--print statement to tell me the coordinates of the anchor of the group
--I now realize these coordinates won’t exist yet since the group is empty at this point

bgGroup:setPosition( conf.width / 2 , conf.height / 2 )
--I expected this line to put the anchor point of the group in the middle of the
--screen (conf.width and conf.height are defined in a config file.

print("Group position ",bgGroup:get("x"),bgGroup:get("y"))
--print statement to tell me coordinates of the group (I assumed the coordinates
--of the mid point of the group

self:addChildAt(bgGroup, 1)
--add this group to the stage

local bg = Bitmap.new(Texture.new("images/greenBackground.png", true))
--create sprite containing the background image (for this testing I used an image
--smaller than the screen size to better see its position)

bg:setAnchorPoint( 0.5 , 0.5 )
--set anchorPoint to the middle of the image.

print("BackgroundSprite anchor ", bg:get("anchorX"),bg:get("anchorY"))
--print statement to tell me the coordinates of the anchorPoint

bg:setPosition( 0 , 0 )
--I expected this to set the position of the midpoint of this sprite to the position of the anchor
--point (midpoint) of the group.

print("BackgroundSprite position ",bg:get("x"),bg:get("y"))
--print statement to tell me the coordinates of this sprite (which I assumed would be the
--coordinates of the midpoint of the image.

bg:setAlpha( 0.3 )
--make the background semi-transparent. This is to make it lighter, not so anything below
--it can be seen (since there’s nothing below it)

bgGroup:addChildAt(bg, 1)
added this background image as a child of the bgGroup

THIS CODE RETURNS A BLANK SCREEN. I could see there’s a problem with the group anchor position so I moved the line:
bgGroup:setAnchorPoint( 0.5 , 0.5 )
to the end of the code (after the bg sprite block).

This gave the result you can see in the screenshot. Basically, the middle of the image is located at 0,0 (the top left corner of the screen).

In the screenshot, you can also see the output of my print statements in the bottom left corner. The output of each time I ran the code is separated by the statement STARTING SPLASH SCENE INIT.

The last block is the results after I moved bgGroup:setAnchorPoint( 0.5 , 0.5 ) to the end. The block before that is from before I moved that code.

How can I properly set and use the anchorPoint of a sprite that has no image and is used only to group elements together?
1335 x 712 - 76K


  • basically every Sprite you add to another Sprite will inherit it's AnchorPoint.
  • olegoleg Member
    Accepted Answer
    centrX = application:getContentWidth()/2
    centrY = application:getContentHeight()/2
     bgGroup = Sprite.new()
    bgGroup:setPosition( centrX, centrY )
    stage:addChildAt(bgGroup, 1)
     bg = Bitmap.new(Texture.new("1.png", true))
    bgGroup:addChildAt(bg, 1)
    bgGroup:setAnchorPoint( 0.5 , 0.5 )
  • Thanks, Oleg.

    I can't believe I spent hours and hours trying to figure this out and it is this easy.

    One change I did was that I put the definition for the variables centerX and CenterX in my configuration file so they're available to be used in all of the scenes.

    The line "minx,miny,maxx,maxy=application:getLogicalBounds()" generated an error. In trying to figure out why, I discovered this function was only recently added (version 2017.11.3). I'm using version 2017.10, so I guess it's time to upgrade.

    Thanks again.

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.