Cocos2d 中 分有大致4个坐标系
player1 = [CCSprite spriteWithFile:@"Icon-72.png" rect:CGRectMake(0, 0, 72,72)];
player1.position = ccp(winWidth/2, winHeight/2);
[self addChild:player1 z:0];
player2 = [CCSprite spriteWithFile:@"Icon-Small-50.png" rect:CGRectMake(0, 0,50, 50)];
[player1 addChild:player2 z:0];
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [player1 convertToNodeSpace:touchLocation];
CCSprite *player = (CCSprite*)[self getChildByTag:77];
touchLocation = [player1 convertToWorldSpace:touchLocation];
NSLog(@"%f, %f", touchLocation.x, touchLocation.y);
然后点击图片后得到的坐标是 223,122
这就说明现在所在的坐标系是world space 或者说 opengl es 坐标系中 也就是 图片的 父级坐标系
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
如果直接想得到 用户是否点击到 精灵 或者图片上面的 本地坐标
再添加这条
touchLocation = [player1 convertToNodeSpace:touchLocation];
当然 用 一条代码 可以代替上面的 2条 省事的话
touchLocation = [player1 convertTouchToNodeSpace: touch];
——————————————————————————————————————————————————
// converting local node coordinates to world space
-(CGPoint) convertToWorldSpace:(CGPoint)nodePoint;
-(CGPoint) convertToWorldSpaceAR:(CGPoint)nodePoint;
// converting world coordinates to local node space
-(CGPoint) convertToNodeSpace:(CGPoint)worldPoint;
-(CGPoint) convertToNodeSpaceAR:(CGPoint)worldPoint;
// converting touch (world) coordinates to local node
space
-(CGPoint) convertTouchToNodeSpace:(UITouch*)touch;
-(CGPoint) convertTouchToNodeSpaceAR:(UITouch*)touch;
// convert a world point to node coordinate space
CGPoint localPoint = [someNode
convertToNodeSpace:worldPoint];
现在 我用player1 和 player2 , 把player2 的坐标系 换成 player1 的
再判断点击是不是在坐标系内
if(CGRectContainsPoint([player2
textureRect], touchLocation))
{
NSLog(@"Inside");
}else {
NSLog(@"Outside");
}
可以看到 player2 的 图片位于靠近原点的位置 但是它的坐标系已经转换为player1 的 local Node space coordinate。
可以看到 不管你怎么点都是 215, 135 的坐标 应为 player1的position 坐标 位于 player2 的本地节点坐标系(Local Node space coordinate)当中, 原点是 player2 的 原点 而不是屏幕左下角的原点
再点击player1 和 player2
控制台输出
下面给出了几个常用的坐标系转换的情况
// convert a local node point to world coordinate
space
CGPoint worldPoint = [someNode
convertToWorldSpace:localPoint];
// same as above, but localPoint is offset to be relative to
anchorPoint of someNode
CGPoint worldPointAR = [someNode
convertToWorldSpaceAR:localPoint];
本地坐标系转换到另外一个节点的坐标系当中(这里是父坐标系)
// convert a local node point to another node's coordinate
space
CGPoint targetNodeLocalPoint = [targetNode
convertToWorldSpace:otherNodeLocalPoint];
// same as above, but otherNodeLocalPoint is offset to be relative
to anchorPoint of targetNode
CGPoint targetNodeLocalPointAR = [targetNode
convertToWorldSpaceAR:otherNodeLocalPoint];
Normally (and unfortunately) cocos2d places child nodes relative to the parent's lower left contentSize corner. In the case of a sprite using a 100x100 image, and the sprite positioned at 400x300, a child node with a position of 0x0 will be centered on the coordinate 400-(100/2)x300-(100/2) = 350x250. This is odd but that's the way cocos2d works. The non-AR conversion coordinates convert coordinates relative to this origin point 350x250 whereas the AR variants convert coordinates relative to the node's anchorPoint, in this case relative to 400x300.
一般或者不幸的是 cocos2d 放置 子节点的位置 是相对 父节点的 左下角的位置这样的方式。 在这个例子里 使用100*100的图片, 精灵位置是 400,300, 一个子节点的位置是 0,0 应该是位于400-100/2 = 350,和300-100/2 = 250中心的地方. 这比较奇怪, 但这就是cocos2d 的方式。(按照一般思维 应该在原点 就是 400, 300 的地方 但是 父节点的锚点不是 0.5 , 0.5 而是 0,0)
The difference between non-AR and AR coordinate conversion methods is merely the distance between the node's lower left origin point and the anchorPointInPoints property. Should the anchorPoint of the node and all of its parents be set to 0x0 (not recommended btw) then both coordinate conversion methods would give you the same results.
这样锚地和 无锚点的坐标系之间转换 将出现很微小的误差, 应该把节点的锚点和所有它的父节点都设置为0,0 ,你将得到正确的结果.
CGPoint cocosPoint = [[CCDirector sharedDirector]
convertToGL:uikitPoint];
CGPoint uikitPoint = [[CCDirector sharedDirector]
convertToUI:cocosPoint];
CGPoint cocosPoint = [someNode
convertToWorldSpace:localPoint];
CGPoint uikitPoint = [[CCDirector sharedDirector]
convertToUI:cocosPoint];
反之
CGPoint cocosPoint = [[CCDirector sharedDirector]
convertToGL:uikitPoint];
CGPoint localPoint = [someNode
convertToNodeSpace:cocosPoint];