0
votes

Consider this: you create a new project on Cocos2D 2.0. You have the traditional Helloworld layer. You add a layer to it with the following structure:

Helloworld (cclayer)
   │
   ┕━ baseLayer (cclayer)
        │
        ┕━ myReducedNode [CCSprite node]
              │
              ┕━ myFullSprite (ccsprite)
                    │
                    ┕━ smallSprite (ccsprite)
  • myReducedNode is a node inside baseLayer, created using [CCSprite node] and has a scale applied to it, so, when I apply that scale I reduce myFullSprite and all smallSprites at the same time.
  • myFullSprite is a 1024x768 points sprite inside myReducedNode.
  • smallSprites are 230x348 points sprite inside myFullSprite.

Consider this craziness:

  • first I apply a scale of 1 to myReducedNode. When I drag smallSprite and check its coordinates, everything is fine. If I position smallSprite on the top left corner of myFullSprite, I read the center coordinate of smallSprite as being (115,594) which is the correct value.
  • I apply a 0.8 scale to myReducedNode. Dragging smallSprite to the same top left corner of myFullSprite, cocos is now reporting the center of smallSprite to be (17,641) ?????????!!!!!!

I am talking about local coordinates, I mean, the position smallSprite is inside myFullSprite.

What is causing this? There's no apparent logic on this number... This number has no relation with the scale applied to the top node.

What am I missing here? I am banging my head on the wall for days, trying to figure this puzzle!!! thanks.


More information. I hope this helps figure out why the coordinates have those values...

  • baseLayer position is (612, 389) on Helloworld.
  • myReducedNode position is (0,0) on baseLayer.
  • myFullSprite position is (0,0) on myReducedNode
3

3 Answers

0
votes

I think you should take a look at convertToWorldSpace:, since you are scaling and nesting things, transformations most likely apply to those coordinates.

Here you have a question that might be useful and this post on cocos2d too

0
votes

Try this:

CGPoint smallSpriteLocalPosition;
smallSpriteLocalPosition = 
[smallSprite.parent convertToNodeSpace:smallSprite.position];

Then print out those coordinates and see if they register properly. That should give you the node (local) coordinates of the smallSprite relative to its parent, the fullSprite. You should also be able to convertToWorldSpace for coordinates within the window bounds.

This is what has worked for me in the past when working with child sprites; it can be a bit tricky. Make sure you use the proper variables in the convert call, otherwise you won't get the right data. Let me know if that works as I haven't tried it with layers that are three deep.

0
votes

after a few changes in code and a several days of research and tries, I conclude this is a bug of Cocos2D or a lack of consistency between how Layers, Sprites and Nodes work (as suggested by LearnCocos2d) , as there's no way to explain the obtained values. I will try to fill a bug report on that.