0

I have got a game layer and a child node (CCNode) called TestNode.I added a spritesheet to TestNode and several "missiles" to this spritesheet. Now my problem is the collision detection of those missiles with objects in the game layer, because coordinates seem to be f'd up.

I have got the following in the missile implementation:

//first I get the Game layer
GameLayer *gL = (GameLayer *)self.parent.parent.parent; 
//parent = spritesheet, spritesheet's parent = TestNode, TestNode's parent = Game layer

//Now I convert the coordinates of missile to Game Layer's node space
CGPoint realPos = [gL convertToNodeSpace:self.position];

This is roughly what I do in my code... and it does not seem to be working! I do pretty much the same collision detection with the TestNode and it seems to be working perfectly fine. Is it because of the spritesheet ? I have been struggling a couple of days now, but I can't get it working at all. Changes give me hardly any hints what I am doing wrong. So you are pretty much my last hope...

the_critic
  • 12,720
  • 19
  • 67
  • 115

1 Answers1

1

You want to convert the missile position to world space:

CGPoint realPos = [self convertToWorldSpace:position];

If it's still not correct, maybe you changed the anchorPoint? In that case the visual part (the texture) will simply be offset from the position and may seem to be incorrect just by looking at it.

As for self.parent.parent.parent and so on … you should read the latter part of this answer. It's not good practice because it's a fragile construct which can easily break when you change the node hierarchy.

Community
  • 1
  • 1
CodeSmile
  • 64,284
  • 20
  • 132
  • 217