CCSprites和CCButton在iPad模拟器中的错误位置

时间:2014-04-24 15:35:06

标签: ios objective-c cocos2d-iphone

我正在努力实现这样的目标:

enter image description here

这是我的代码:

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));

这是resizeSprite方法:

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

当我在iPhone 4英寸模拟器中运行时,它看起来很完美但是当我在iPad模拟器上运行它看起来像这样:

enter image description here

如果我使用:

CGSize size = [[CCDirector sharedDirector]viewSize];

在iPad上看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:2)

正确的解决方案:

更改

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

要:

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

将背景位置更改为:

background.position = CGPointMake(size.width/2, size.height/2);