在 cocos2d 中,坐标系非常简单。左下角是 (0,0)。 每当我设置任何东西的位置时都很好。
假设一个层是 TouchEnabled。
当我设置 Sprite 的位置时,没问题..
[crab setPosition:ccp(20, 50)];
但是如果我得到触摸的 x 和 y 坐标(并假设我点击了 sprite..):
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches began!");
NSArray *touchArray = [touches allObjects];
UITouch *one = [touchArray objectAtIndex:0];
pointOne = [one locationInView:[one view]];
CCLOG(@"points are: %f and %f", pointOne.x, pointOne.y );
}
y 坐标与 cocos2d 的坐标系相反,因为 y 从上到下增加!
所以我假设 View 的坐标系与 mac 的坐标系不同..这非常违反直觉..对吗?
谢谢!
最佳答案
OpenGL 使用左下角的 (0, 0) 而 iOS 和因此处理触摸的 UIKit 使用左上角作为 (0, 0)。转换您的观点很容易,您只需要记住这样做即可。
在cocos2d 2.0上正确的方法是
CGPoint uiKitPoint = [touch locationInView:touch.view];
CGPoint cocos2dPoint = [[CCDirector sharedDirector] convertToGL:uiKitPoint];
关于iphone - Cocos2d坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12721458/