How does the AnchorPoint work exactly?

How does the AnchorPoint work exactly?
0.0 0

#1

Hello,

So I’m trying to setup an object in between my own and the Node of cocos2d which will upon initialisation set the anchor point according to my wishes. I want to be able to switch it around such that when I add children to my game area I want my origin to be in the middle bottom of the area itself.

In other cases I want it in the centre, for the balls or the obstacles that I create.

I can’t seem to move the anchor point much though, so I wonder how it’s all tied together. Some of these extended nodes will also have physics bodies. Is the AnchorPoint dependent on ContentSize? Because some of my nodes don’t have any content per say, other than being a parent to their children.


#2

http://cocos2d-x.org/docs/en/basic_concepts/parent_child.html

http://cocos2d-x.org/docs/en/sprites/manipulation.html


#3

It only speaks of Sprites. I am asking about the nodes themselves, not matter the ContentSize, e.t.c.

If I have a Node, that to me is set to be a parent for various UI parts of the GameScene with a size of say 100 x 200 and I want my AnchorPoint to be in a specific position, say (50, 0), how do I do that?

The articles doesn’t mention how it works with Nodes, other than that the default Anchor Point is (0.5, 0.5). Does this mean. It makes sense that changing it given a float changes nothing, as it has no Content Size without attaching a sprite or something. This is my question, not how Sprites work.


#4

There is no difference between Node and Sprite in this case. A Sprite is a subclass of Node.

So if you want different anchor points other than the normal, I imagine you will will need to fine-tune this yourself.


#5

I know the sprite is a sub-class of Node.

But the behaviour I get is not documented. When I change the Anchor on a Sprite, it seems to do as documented, no problems. But In order for it to even have an effect it needs to have a content size, I imagine. Which a regular node doesn’t have. The Sprite does because it loads the actual sprite, and thus (0.5, 0.5) means something there. But what does that mean for a Node without a width or height? That is what I am trying to figure out here.

I made a very simple engine myself two years ago and my anchor points weren’t based on a float between 0 and 1, and instead had to be calculated using normal values. I hadn’t gotten any further than that, but that was mostly because I worked with objects without a known width or height.

So my question is: Does changing the anchor point have any affect at all if getContentSize() == Size(0, 0)? Or if it returns nullptr, not sure.

If anybody can shed some light on this I’d be super happy :slight_smile:


#6

in my experience no, You can easily test this by changing the anchorpoint and outputting the content size to the console or looking at it over and over in the debugger.


#7

No as in it does not change or it does not return Size with zero?

Which I have done. The anchor point does not seem to be moving at all. But I wanted to be sure this is the case and not that I have messed up or done something one should not, etc.


#8

No, it does not change.

would you mind posting some code so we can have a look?


#9

I can’t right now but maybe later.

The idea is simple though. I have a subclass of Node that upon creation takes an enum which will set the anchorpoint accordingly. But without a content size this means nothing, I suppose. So I wonder how to solve that.

I have a class called Board, which contains Frame and that in turn contains Grid. So I will have a grid in a frame, which is the play area, so that when I add children to the grid using my helped function placeNodeInSquare(int row, int col) it will do the calculations to handle that.

The problem is that the nodes themselves that are put in there have no width or height right now (as I have no assets for them currently. They’re just blocks/objects on the map), so instead of ending up inside the square, they end up crossing over four squares, as their anchorpoint is still (0, 0) instead of (0.5, 0.5). Which makes sense if this has to do with the content size.

So I need to know how to work around it for now. I do however know the size of a square, so I could maybe do node->setContentSize(squareSize) if that will help?


#10

The anchor point does depend on the content size. An anchor point of (0.25, 0.5) would be saying “I want the anchor point to be at an X of the contentSize width of the Node multiplied by 0.25 and a Y of the contentSize height of the Node multiplied by 0.5.” As you suggested, manually setting the content size of your node is probably the best solution.