【发布时间】:2012-04-16 16:44:08
【问题描述】:
如何在 Cocos2d-iPhone 中获取屏幕中心点的世界空间坐标?
【问题讨论】:
标签: cocos2d-iphone
如何在 Cocos2d-iPhone 中获取屏幕中心点的世界空间坐标?
【问题讨论】:
标签: cocos2d-iphone
简单,只取高宽除以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];
【讨论】:
无论你使用什么框架,无论是否使用 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
【讨论】: