Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the center of the screen in cocos2d?

How do I get the world space coordinate of point that is in the center of the screen, in Cocos2d-iPhone?

like image 911
Jay Avatar asked Apr 16 '12 16:04

Jay


1 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 somewhat more advanced way to do it. This will also work if you have called setPosition on the parent of the sprite (=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];
like image 176
s1m0n Avatar answered Nov 11 '22 23:11

s1m0n