背景图像大小 Sprite Kit 游戏 [英] Background image size Sprite Kit Game

查看:24
本文介绍了背景图像大小 Sprite Kit 游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了一个新的 Sprite Kit 项目来学习如何使用它.我观看并阅读了很多教程,但没有一个教程可以回答我的问题.

i just started a new Sprite Kit project to learn how to use it. I watched and read a lot of tutorials but no tutorial has a answer for my question/problem.

我想为我的 iPhone 5S 创建一个应用程序.所以屏幕尺寸是1136x640.我为我的应用程序创建了一个 1136x640 背景图像.但是当我将图像添加到我的应用程序时,它会变得很大!iOS 模拟器只显示图像的中间.

I want to create an application just for my iPhone 5S. So the screen size is 1136x640. I created a 1136x640 background image for my application. But when i add the image to my app, its waaay to big! The iOS Simulator just displays the middle of the image.

谁能告诉我我必须使用什么屏幕尺寸以及为什么?

Can someone tell me what screen size i have to use and why?

非常感谢!

这是我从教程中复制的代码.代码在initWithSize方法的myScene.m文件中

Here is the code which i copied from a tutorial. The code is in the myScene.m file in the initWithSize method

        SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"myBackground"];
    background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));

    [self addChild:background];

编辑:

我在谷歌上搜索并找到了这个:

I searched on google and found this:

必须使用viewWillLayoutSubviews"更改 viewDidLoad 方法.

The viewDidLoad method has to be changed with "viewWillLayoutSubviews".

这是这个方法:

    - (void)viewWillLayoutSubviews
    {
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [MyScene sceneWithSize:CGSizeMake(skView.bounds.size.width*2,skView.bounds.size.height*2)];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}

起初场景 = MySceneWithSize 行是:

At first the scene = MySceneWithSize line was:

SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];

但它只是 iPhone 5 屏幕尺寸 (568x320) 的一半.所以我不得不加倍大小.有人知道为什么吗?

But then it was just the half of the iPhone 5 screen size (568x320). So i had to double the size. Does somebody know why?

推荐答案

对您当前问题的简短回答:

background.size = self.frame.size;

长答案,以及关于其他方法的讨论......

场景以逻辑单位工作,而不是物理像素(如其他帖子中所述).

The scene works in Logical Units, not physical pixels (as mentioned in other posts).

1 逻辑单元在旧套件上通常为 1 像素,在新套件/iPad 上通常为 2 像素.这可能在 5 年内达到 4 个像素.

1 Logical Unit is typically 1 pixel on older kit, and 2 pixels on newer kit/iPads. This maybe 4 pixels in 5 years time.

在逻辑单元中工作可以保护您免受将来尺寸变化的影响,并且应该对程序员有所帮助 - 尽管这经常使新手感到困惑.

Working in Logical Units protects you from sizes changing in future, and is supposed to help the programmer - although this often confuses newbies.

让图像填充当前场景的最简单方法是将其大小设置为逻辑单位",无论它最初是什么大小.

The simplest way to get an image to fill the current scene is to set its size in 'logical units' no matter what size it is originally.

这比使用 SKActions 更好(因为用户最终可能会看到它们,并且在操作完成之前不能依赖基于节点尺寸的任何计算),并且比缩放更好,因为缩放需要你知道原始图像的尺寸.

This is better than using SKActions (because the user might end up seeing them, and any calculations based on the dimensions of the node can't be relied upon until the action has completed), and it's better than scaling because scaling requires you to know the original image dimensions.

你可以通过 size 属性来做到这一点,如果你真的想制作动画,也可以通过一个动作来做到这一点.

You can do this via the size property, or through an action if you really want to make an animation.

只是在场景中......

In the scene simply....

-(void)didMoveToView:(SKView *)view
{
    [super didMoveToView:view];

    SKSpriteNode* background = [SKSpriteNode spriteNodeWithImageNamed:@"myBackground"];
    background.size = self.frame.size;
    background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    [self addChild:background];
} 

或者,如果您的图像效果很好,例如在更高的分辨率下,您可以将图像重命名为 myBackground@2x.png,将其添加到您的项目中,您可能根本不需要调整大小,但是您对于分辨率较低的设备,还需要将其缩小 50%,并将其保存为 myBackground.png.

Alternatively, if your image works well, as is, on higher resolutions, you can rename your image to myBackground@2x.png, add that to your project and you probably won't need to resize at all, but you'll also need to shrink it by 50% for the lower resolution devices, and save that as myBackground.png.

我总是将导入图像的大小设置为我想要的逻辑单位(或屏幕尺寸的百分比)以保持我的理智.

I always set the size of an imported image to exactly the logical units I want (or a percentage of the screen dimensions) to preserve my sanity.

这篇关于背景图像大小 Sprite Kit 游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆