Cocos2d 中 分有大体4个坐标系html
player1 = [CCSprite spriteWithFile:@"Icon-72.png" rect:CGRectMake(0, 0, 72,72)];node
player1.position = ccp(winWidth/2, winHeight/2);ios
[self addChild:player1 z:0];ide
player2 = [CCSprite spriteWithFile:@"Icon-Small-50.png" rect:CGRectMake(0, 0,50, 50)];测试
[player1 addChild:player2 z:0];ui
CGPoint touchLocation = [touch locationInView:touch.view];this
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];url
touchLocation = [player1 convertToNodeSpace:touchLocation];spa
CCSprite *player = (CCSprite*)[self getChildByTag:77];3d
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");
}
再点击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];