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

问题描述

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



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



有人能告诉我我必须使用的屏幕尺寸以及原因?



非常感谢!



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

  SKSpriteNode * background = [SKSpriteNode spriteNodeWithImageNamed:@ 我的背景]; 
background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));

[self addChild:background];

编辑



我在谷歌搜索并发现:



必须使用viewWillLayoutSubviews更改viewDidLoad方法。



以下是此方法:

   - (void)viewWillLayoutSubviews 
{
[super viewWillLayoutSubviews] ;

//配置视图。
SKView * skView =(SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;

//创建和配置场景。
SKScene * scene = [MyScene sceneWithSize:CGSizeMake(skView.bounds.size.width * 2,skView.bounds.size.height * 2)];
scene.scaleMode = SKSceneScaleModeAspectFill;

//呈现场景。
[skView presentScene:scene];
}

首先,场景= MySceneWithSize线为:

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

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

解决方案

您当前问题的简短答案:

  background.size = self.frame.size; 

答案很长,有关其他方法的讨论......



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



1逻辑单位是旧套件上通常为1像素,较新套件/ iPad上为2像素。
这可能是5年内的4个像素。



在逻辑单元中工作可以保护你免受以后更改的大小,并且应该帮助程序员 - 尽管这有用经常混淆新手。



获取图像以填充当前场景的最简单方法是将其大小设置为逻辑单位,无论其原始大小如何。



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



您可以通过size属性执行此操作,或者通过操作执行此操作(如果您确实想要创建动画。



在场景中只是....

   - (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];
}

或者,如果你的图像运行良好,就像在较高分辨率下一样可以将您的图像重命名为[email protected],将其添加到您的项目中,您可能根本不需要调整大小,但是对于较低分辨率的设备,您还需要将其缩小50%,并将其保存为myBackground.png。



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

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.

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?

Thanks a lot!

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

EDIT:

I searched on google and found this:

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

Here is this method:

    - (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];
}

At first the scene = MySceneWithSize line was:

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

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?

解决方案

The short answer to your immediate problem:

background.size = self.frame.size;

The long answer, and discussion about other methods...

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

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.

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.

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];
}

Alternatively, if your image works well, as is, on higher resolutions, you can rename your image to [email protected], 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.

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

09-02 13:24