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];