Cocos2d坐标系

Pen*_*rce 5 iphone drawing cocos2d-iphone ios

在cocos2d中,坐标系非常简单.左下角是(0,0).每当我设置任何东西的位置都没关系.

让我们说一个图层是TouchEnabled.

当我设置我的精灵的位置,它很好..

[crab setPosition:ccp(20, 50)];
Run Code Online (Sandbox Code Playgroud)

但如果我得到一个触摸的x和y坐标(并假设我点击了精灵..):

- (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 );
}
Run Code Online (Sandbox Code Playgroud)

当y从上到下增加时,y坐标与cocos2d的坐标系表示的相反!

所以我假设视图的坐标系不同于mac的坐标系...这是非常直观的..这是正确的吗?

谢谢 !

Ben*_*ove 4

OpenGL 使用左下角的 (0, 0),而 iOS 和处理触摸的 UIKit 使用左上角作为 (0, 0)。转换你的观点很容易,你只需要记住这样做即可。

cocos2d 2.0上正确的方法是

CGPoint uiKitPoint = [touch locationInView:touch.view];
CGPoint cocos2dPoint = [[CCDirector sharedDirector] convertToGL:uiKitPoint];
Run Code Online (Sandbox Code Playgroud)