How to set anchor point of group's children

I read the guide on group programming and I can see that setting

myGroup.anchorX, myGroup.anchorY = 0, 0

sets the anchor point of the group…but what about the children inside the group? If I set a child object inside the group to position x, y = 0, 0 it places it in the centre but I want it to be at the top left.

How can I anchor the child objects to the top left?

The default anchor of a group is already <0,0>

I really don’t suggest messing with group anchors.  They are a bit tricky to understand and make work, and 99% of the time it isn’t needed nor does it achieve what one is trying to do.

Tell us (describe in detail if you can) what you’re trying to achieve and hopefully we can give you way to do it.

Sleepy time for me.  I’ll check back on this in the morning.

If I set a child object inside the group to position x, y = 0, 0 it places it in the centre but I want it to be at the top left.

Yeah, I read that, I meant elaborate.  Quoting your original post doesn’t really help much.

Are you simply saying you want to ensure that content you create gets put in the upper left corner, regardless of screen size.  That is easy.

Answer yes or no and I’ll show how tomorrow if it is a yes.

That is, determining the left, right, top, bottom bounds of the screen and ensuring placement adjacent to them is straightforward.

If this is the concept you mean (red square):
lgg4.png
 
This sample shows how:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/10/lgg4.zip
 
Tip: Uses relative placement, NOT group tricks/shifting. I’ll talk more about that in the next post.
 
Pay close attention to code in main.lua, common.lua, and config.lua parameters.

If you examine the code in common.lua there are two basic solutions available:
 
Shift Group

local centerX = display.contentCenterX local centerY = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local left = centerX - fullw/2 local top = centerY - fullh/2 local right = centerX + fullw/2 local bottom = centerY + fullh/2 local group = display.newGroup() group.x = left group.y = top local tmp = display.newRect( group, 0, 0, 40, 40 ) tmp.anchorX = 0 tmp.anchorY = 0

Place Objects Relatively

local centerX = display.contentCenterX local centerY = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local left = centerX - fullw/2 local top = centerY - fullh/2 local right = centerX + fullw/2 local bottom = centerY + fullh/2 local group = display.newGroup() local tmp = display.newRect( group, left, top, 40, 40 ) tmp.anchorX = 0 tmp.anchorY = 0

I am not a fan of shifting groups. Especially if you’re using physics. Better to place relative to edges.

Update, I said build.settings in a prior post, when I meant config.lua.  Updated, but just in  case examine my config.lua.

Specifically note, I use letterbox scaling which is IMHO generally superior to all other choices for games.

No it’s just like I described, I don’t know how else to say it so I’ll add images.

So you can see the group is in the centre because I set it to content centre, which is fine but the child object inside is put into the centre with x and y at 0…why? Surely they should need to be centred to go the centre of the group. Not sure why the default is the centre but anyway, that’s what I want to change.

Because in 99% of situations centre positioning is the right way.

If you want your object top left then place it there manually using a negative x and y.

Remember a group is the size of its bounding rect so if you want objects to snap to edges (when the group expands) you will also need to code for this manually.

Why? Positioning in corona is normally done from the top left…it seems strange to do it differently within groups. In fact the group is positioned based on the top left and then strangely, objects within the group are placed in the centre?

I want it to be based from the top left. It’s the best way to position to maintain compatibility with different screen sizes. I thought that is why all the rest of the positioning is based on the top left.

I showed how to do that second image where <0,0> is the upper left corner of the screen.

However, if you want a rectangle at <0,0> to align it’s upper left corner to <0,0> in the group, you have to set the anchorX and anchorY values of the OBJECT to 0.  Otherwise, an OBJECT at <0,0> will have its center in the upper-left corner of the screen.

There is one other thing you can do to make the upper-left corner of all screens be <0,0>.

Use left,top for xAlign and yAlign in config.lua (another thing I never do, but if it works for you…):

https://docs.coronalabs.com/guide/basics/configSettings/index.html#xalign-yalign

I’m going to repeat all my advice in one place then I have to move on.

A. Don’t set anchors on GROUP objects.

B.  AND ONE OF THE FOLLOWING:

  • Calculate left, right, top, bottom and and place relative to those positions, using anchor values on objects as needed

- OR -

  • If you insist all groups’ <0,0> be upper-left, measure, left, and top and move group(s) to that position.  Place objects as normal, using anchor values on objects as needed 

- OR -

  • If you insist all groups’ <0,0> be upper-left, use xAlign/yAlign in config.lua to enforce this.  Place objects as normal, using anchor values on objects as needed 

Not sure why you’re seeing this.  All objects are in a group and all groups behave the same unless you tweak their anchors and anchorChildren settings.

Objects not placed in a group by you go into display.currentStage which is the top group.

<x,y> Positioning is not done from top-left exactly.  It is done relative to the groups <0,0> position which may or may not be exactly aligned with the screen’s top-left.

Furthermore, all objects (excluding groups) are placed by their center.  i.e. A rectangle placed at <100,100> will by default have it’s center at <100,100> in the group that owns it.  That position may or may not be 100 pixels from the left side of the screen and the top.

Groups are excluded from the above placement rule because (as essentially infinite objects; growing and shrinking as you add and remove content) they have no center unless you enforce it with settings or calculate it yourself.  When you move a groups to <x,y> position, you’re saying to Corona, move the <0,0> position of the group (and all children) to <x,y>

Second image? I"m only seeing one image above?

I’m confused, as far as I can see objects are normally placed in the top left corner? it says that here https://docs.coronalabs.com/guide/graphics/group.html in the coordinate system. “In Corona, the default origin of display groups is 0,0 which is located at the top-left corner of the stage, not the center.”

That’s why I was wondering why it wasn’t like that inside of a group too.

Maybe we’re confusing what is meant by aligning to the centre. What I mean is when I create an object at 0,0 it’s at the top left of the simulator screen. But it is positioned BY the centre of the object. ie The half of the object will not be visible since it’s centre is at the top left corner of the screen.

Yes they will be at the top left unless the group itself is offset. So top left should be the default.

Yes they are placed BY their centre, I can see that is happening as you say. But as you can see from the diagrams I provided above, the object is being placed not just BY it’s centre but also IN the centre but with x,y set to 0. So that shouldn’t be happening is that what you’re saying?

what you want (everything has top-left anchor) is NOT the way Corona works by default, but you could force it to:

display.setDefault("anchorX", 0) display.setDefault("anchorY", 0) local group = display.newGroup() local rect = display.newRect(group, 0, 0, 40, 40)

@hasen,

Zip up your test project that is doing this place in center thing and share it here as a zip file.

Click ‘More Reply Options’ button and you’ll see this:

attaching_files.jpg

(Make sure the example is DROP DEAD simple and only contains the code needed to show the problem.)

Tip: If you’re not already doing this… you should be.  It will save you a ton of pain ==> 

When  you hit a roadblock like this, do not experiment in your main project. Create a micro test-bench and experiment there till you work it out.  Doing otherwise jeopardizes your project.

I supply a simple starter just for this purpose: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/askEdStarter.zip

Why do you want me to do that? In my last post I asked if this should be happening or not but no one responded to that so I’m still a bit confused. In the images I attached at the top of the thread, is that the correct behaviour or not?

I think there’s confusion in this thread over the anchor points, whether it means position BY, or position AT. My problem is the object is being positioned BY it’s centre but also IN the centre. It’s my understanding that Corona positions objects BY their centre but IN the top left by default. It’s stated in the coordinate documentation.