the_critic
the_critic

Reputation: 12820

How do convertToWorld-/NodeSpace work ?

I am working on a game for a little while now and I worked with convertToWorldSpace by trial & error. It always worked out, but I have no idea what I am doing to be honest. I really hate that I do not understand what I am doing, but unfortunately the Internet does not give much information on this problem. So I hope somebody can explain, why for example I have to call convertToWorldSpace from the node's parent, and when I use convertToNodeSpace. I have absolutely no clue. Can somebody explain in general what they do and maybe give an example ? I would really appreciate it! :-)

Upvotes: 2

Views: 1789

Answers (1)

hjm
hjm

Reputation: 421

simply put...

the world space is the coordinate grid of the screen.

nodespace is the coordinate grid of the layer.

cocos2d and most other game frameworks consist of multiple layers within one scene and you would most likely have a bunch of other little nodes/sprites. when you are calculating the coordinates for each sprites in your layer you would be using node space, but however when you get touch locations, it would be in world space so you would need to convert it using convertToNodeSpace etc.

hope this helps!

---- edit ----

maybe an example will help...

 [somenode convertToWorldSpace:ccp(x, y)];

the above code will convert the coordinates (x, y) to the coordinates on the screen. so for example if (x, y) is (0, 0) that position will be the bottom left corner of the node, but not necessarily on the screen. this will convert (0, 0) of the node to the position of the screen.

[somenode convertToNodeSpace:ccp(x, y)];

the above code will do the opposite. it will convert the coordinates on the screen (x, y) to what it would be on somenode.

so it comes handy when you have something you want to move (or get the position of or whatever) that's a child of some other node or layer, since most of the time you want to move the whatever it is relative to the screen rather than within that layer.

Upvotes: 5

Related Questions