I am beginner in cocos2d, I need init scene frame to it be less than device screen size as shown on the pinned picture, in center. In addition, scene must scale as shown on the picture. I've tried to do it as below, but unsuccessfully.
- (void)drawBackground
{
NSArray *assetPathParts = [self.dataProvider.backgroundValue componentsSeparatedByString:@"/"];
self.background = [CCSprite spriteWithImageNamed:assetPathParts.lastObject];
self.background.anchorPoint = ccp(0,0);
self.background.scale = 2;
self.background.scaleType = CCScaleTypeScaled;
[self addChild:self.background];
}
How can I fix it?
2 ways you can fit screen.
Method 1:
CGSize wSize = [[CCDirector sharedDirector] winSize];
CCSprite *background = [CCSprite spriteWithFile:@"Kundapura.png"];
background.position = ccp(wSize.width*0.5f, wSize.height*0.5f);
[self addChild:background];
background.scaleX = wSize.width/background.contentSize.width ;
background.scaleY = wSize.height/background.contentSize.height ;
Method 2:
CGSize wSize = [[CCDirector sharedDirector] winSize];
CCSprite *image = [CCSprite spriteWithFile:@"Kundapura.png"];
CCSprite *background = [CCSprite spriteWithTexture: image.texture rect:CGRectMake(0, 0, wSize.width, wSize.height)];
background.position = ccp(wSize.width*0.5f, wSize.height*0.5f);
[self addChild:background];
This fits your background to iPhone device size. Use runAction to scale on your needs.