在xcode中处理不同的屏幕尺寸

时间:2015-05-11 07:34:38

标签: ios objective-c iphone sprite-kit

我有一款首次为iPhone 6开发的iPhone游戏,当在iPhone 5上播放时,它可以正常工作但是当在iPhone 4 sim上播放时,两侧都伸出来了?!?我从左侧和右侧产生的节点在屏幕上产生,而不是像iPhone 6和5那样在屏幕上产生。 (请不要说这是重复的,因为我一直在寻找几周如何处理不同的iPhone)

我有左箭头和右箭头在y轴上随机产生。它们在屏幕上产生然后在屏幕上慢慢移动而不是在屏幕上快速移动,但是在iPhone 4上箭头在屏幕上产生。这是其中一个箭头的代码

-(void) leftArrow
{
    int randomY = arc4random() % (675 - 175);
    int yPoint = randomY + 175;
    leftArrow = [SKSpriteNode spriteNodeWithImageNamed:@"arrow"];

    leftArrow.size = CGSizeMake(leftArrow.size.width/1.2, self.frame.size.height/22);
    leftArrow.position = CGPointMake(CGRectGetMidX(self.frame) - 230, yPoint);
    leftArrow.zPosition = 50;
    leftArrow.alpha = 1;

    SKAction* action1 = [SKAction moveToX:leftArrow.position.x + 30 duration:0.5];
    SKAction* wait = [SKAction waitForDuration:0.75];
    SKAction* action2 = [SKAction moveToX:leftArrow.position.x + 470 duration:1.5];
    SKAction* sequence = [SKAction sequence:@[action1,wait,action2,destroy]];

    [self addChild:leftArrow];
    if (deadDown == 1 || deadUp == 1 || didIntersect == true)
    {
        [leftArrow runAction:destroy];
    }
    else
    {
        [leftArrow runAction:sequence completion:^{
            if (ii != 1)
            {
                if (didIntersect != true)
                {
                    score++;
                    if (score == 100)
                    {
                        backgroundScore.fontSize = 300;
                        backgroundScore.position = CGPointMake(backgroundScore.position.x, backgroundScore.position.y + 30);
                }
                strFromInt = [NSString stringWithFormat:@"%d",score];
                backgroundScore.text = strFromInt;
                }
            }
        }];
    }
}

我也在我的didmovetoview函数中使用它

self.scaleMode = SKSceneScaleModeAspectFill;

3 个答案:

答案 0 :(得分:2)

主要问题是处理与图像资源和屏幕对象坐标相关的各种屏幕尺寸。

我的解决方案是编写代码,就像编写iPhone 6 plus一样。为iPhone 6屏幕尺寸制作所有图像@ 3x尺寸和屏幕布局坐标。

只需一点代码,我就可以获得iPhone 6 plus,6,5和4屏幕尺寸的统一布局。我为每个人都添加了屏幕截图。角色图像为300x300。 2个按钮图像为100x100。

static const float kIphone6PlusScaleFactorX = 1.0;
static const float kIphone6PlusScaleFactorY = 1.0;
static const float kIphone6ScaleFactorX = 0.9;
static const float kIphone6ScaleFactorY = 0.9;
static const float kIphone5ScaleFactorX = 0.772;
static const float kIphone5ScaleFactorY = 0.772;
static const float kIphone4ScaleFactorX = 0.772;
static const float kIphone4ScaleFactorY = 0.652;

#import "GameScene.h"

@implementation GameScene {
    float scaleFactorX;
    float scaleFactorY;

    SKSpriteNode *node0;
    SKSpriteNode *node1;
    SKSpriteNode *node2;
    SKLabelNode *label0;
}

-(void)didMoveToView:(SKView *)view {
self.backgroundColor = [SKColor blackColor];

    if(view.frame.size.height == 736) {
        NSLog(@"iPhone 6 plus");
        scaleFactorX = kIphone6PlusScaleFactorX;
        scaleFactorY = kIphone6PlusScaleFactorY;
    }
    if(view.frame.size.height == 667) {
        NSLog(@"iPhone 6");
        scaleFactorX = kIphone6ScaleFactorX;
        scaleFactorY = kIphone6ScaleFactorY;
    }
    if(view.frame.size.height == 568) {
        NSLog(@"iPhone 5");
        scaleFactorX = kIphone5ScaleFactorX;
        scaleFactorY = kIphone5ScaleFactorY;
    }
    if(view.frame.size.height == 480) {
        NSLog(@"iPhone 4");
        scaleFactorX = kIphone4ScaleFactorX;
        scaleFactorY = kIphone4ScaleFactorY;
    }

    node0 = [SKSpriteNode spriteNodeWithImageNamed:@"Pic"];
    node0.position = CGPointMake(self.size.width/2, self.size.height/2);
    [node0 setScale:scaleFactorX];
    [self addChild:node0];

    node1 = [SKSpriteNode spriteNodeWithImageNamed:@"button0"];
    node1.position = CGPointMake(100*scaleFactorX, 100*scaleFactorY);
    [node1 setScale:scaleFactorX];
    [self addChild:node1];

    node2 = [SKSpriteNode spriteNodeWithImageNamed:@"button1"];
    node2.position = CGPointMake(314*scaleFactorX, 100*scaleFactorY);
    [node2 setScale:scaleFactorX];
    [self addChild:node2];

    label0 = [SKLabelNode labelNodeWithFontNamed:@"HelveticaNeue-Bold"];
    label0.text = @"Big Game Menu";
    label0.fontSize = 48*scaleFactorX;
    label0.fontColor = [SKColor whiteColor];
    label0.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
    label0.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
    label0.position = CGPointMake(207*scaleFactorX,690*scaleFactorY);
    [self addChild:label0];
}

iPhone 4

enter image description here

iPhone 5

enter image description here

iPhone 6

enter image description here

iPhone 6 Plus

enter image description here

请注意文本标签是如何正确缩小的,不仅仅是字体大小,还有位置。

您可以使用上述逻辑来生成与屏幕大小相关的节点。

供我参考,我确实使用了GameViewController中的标准代码,因为我发现使用更简单的版本更容易。这是我用来呈现我的SKView的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    SKView * skView = (SKView *)self.view;
    SKScene *scene = [GameScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    [skView presentScene:scene];
}

答案 1 :(得分:0)

从iPhone 5或6移动到4时,屏幕宽高比从16:9(5和6)变为4:3(iPhone 4)。没有图像失真的选项是使用SKSceneScaleModeAspectFill(您是)或SKSceneScaleModeAspectFit。来自Apple文档:

SKSceneScaleModeAspectFill" ....这可以保证视图的整个区域都被填充,但可能会导致部分场景被裁剪。"

SKSceneScaleModeAspectFit" .....这可以保证整个场景可见,但可能需要在视图中使用信箱。"

我遇到了类似的问题并为4:3设备(iPhone 4 / iPad)重新创建了单独的图像,因为以上两个选项都没有给我我想要的内容。

答案 2 :(得分:-1)

尝试使用比例而不是实际尺寸。所有iOS设备都有不同的宽度和高度。

更改此

leftArrow.position = CGPointMake(CGRectGetMidX(self.frame) - 230, yPoint);

这样的事情

leftArrow.position = CGPointMake(CGRectGetMidX(self.frame) - self.frame.size.width / 2, yPoint);

我只能猜测它应该做什么,但尝试更改此

int randomY = arc4random() % (675 - 175);
int yPoint = randomY + 175;

对此:

int randomY = arc4random() % (self.frame.size.height - self.frame.size.height / 4);
int yPoint = randomY + self.frame.size.height / 4;

我还建议你改变这些:

  

更改那些“30”和“470”

SKAction* action1 = [SKAction moveToX:leftArrow.position.x + 30 duration:0.5];

SKAction* action2 = [SKAction moveToX:leftArrow.position.x + 470 duration:1.5];