6

如何在 Cocos2d-iPhone 中获取位于屏幕中心的点的世界空间坐标?

4

2 回答 2

9

很简单,只需将高度和宽度除以 2

CGSize winSize = [[CCDirector sharedDirector] winSize];
CGPoint point = ccp(winSize.width/2, winSize.height/2);

这是一种更高级的方法。如果您在 sprite 的父级上调用了 setPosition(在本例中为 =self),这也将起作用

CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite* centerSprite = [CCSprite spriteWithFile:@"sprite"];
CGPoint centerPoint = ccpSub(ccp(winSize.width/2, winSize.height/2), [self position]);
[centerSprite setPosition:centerPoint];
[self addChild: centerSprite];
于 2012-04-16T18:48:22.197 回答
0

无论您使用什么框架,无论是否使用 Cocos,您都可以获得屏幕的确切中心,即使 Cocos 未设置为使用整个屏幕(有时如果您裁剪 Cocos 为广告腾出空间,就会出现这种情况)通过做这样的事情:

int width=[[UIScreen mainScreen] bounds].size.width
int height=[[UIScreen mainScreen] bounds].size.height
CGPoint center=ccp(width/2,height/2);

注意ccp宏只在 Cocos 中有效;否则,使用CGPointMake

于 2012-04-17T01:29:16.453 回答