How to get the center of the screen in cocos2d?

How to get the coordinate of a space point in the world, located in the center of the screen, in Cocos2d-iPhone?

+6
source share
2 answers

Simple, just take the height and width and divide it by 2

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

Here is a slightly more advanced way to do this. This will also work if you called setPosition on the sprite's parent (= self in this example)

 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]; 
+9
source

You can get the exact center of the screen no matter which structure you use, Cocos or not, and even if Cocos is not configured to use the entire screen (for example, sometimes, if you crop Cocos to make room for advertising) by doing- something like this:

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

Note that the ccp macro ccp works on Cocos; otherwise use CGPointMake

0
source

Source: https://habr.com/ru/post/913337/


All Articles