سؤال

I am trying to achieve something like this:

enter image description here

Here is my code:

CGSize size = [UIScreen mainScreen].bounds.size;
[self resizeSprite:background toWidth:size.width toHeight:size.height];
NSLog(@"Screen size: %@",NSStringFromCGSize(size));
background.position = CGPointMake(0 + background.contentSize.width ,size.height - background.contentSize.height);
playButton.position = CGPointMake(size.width/2, size.height/2);
NSLog(@"Button position: %@",NSStringFromCGPoint(playButton.position));
NSLog(@"Background position: %@",NSStringFromCGPoint(background.position));

And here is the resizeSprite method:

-(void)resizeSprite:(CCSprite*)sprite toWidth:(float)width toHeight:(float)height {
    sprite.scaleX = width / sprite.contentSize.width;
    sprite.scaleY = height / sprite.contentSize.height;
}

When I run in the iPhone 4 inch simulator it looks perfect but when I run on the iPad simulator it looks like this:

enter image description here

If I use:

CGSize size = [[CCDirector sharedDirector]viewSize];

It looks like this on the iPad:

enter image description here

هل كانت مفيدة؟

المحلول

Right Solution:

Changed

-(void)resizeSprite:(CCSprite*)sprite toWidth:(float)width toHeight:(float)height {
sprite.scaleX = width / sprite.contentSize.width;
sprite.scaleY = height / sprite.contentSize.height;
}

To:

-(void)resizeSprite:(CCSprite*)sprite toWidth:(float)width toHeight:(float)height {
sprite.scaleX = width / [sprite boundingBox].size.width;
sprite.scaleY = height / [sprite boundingBox].size.height;
}

And change background position to:

background.position = CGPointMake(size.width/2, size.height/2);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top