ios 背景图片大小 Sprite Kit 游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19471761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Background image size Sprite Kit Game
提问by Mike_NotGuilty
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.
我刚刚开始了一个新的 Sprite Kit 项目来学习如何使用它。我观看并阅读了很多教程,但没有教程可以回答我的问题。
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.
我想为我的 iPhone 5S 创建一个应用程序。所以屏幕尺寸是1136x640。我为我的应用程序创建了一个 1136x640 的背景图像。但是当我将图像添加到我的应用程序时,它会变大!iOS 模拟器只显示图像的中间。
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
这是我从教程中复制的代码。代码在 initWithSize 方法中的 myScene.m 文件中
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".
viewDidLoad 方法必须使用“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:
起初,场景 = MySceneWithSize 行是:
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?
但后来它只是 iPhone 5 屏幕尺寸 (568x320) 的一半。所以我不得不把尺寸加倍。有人知道为什么吗?
采纳答案by GilesDMiddleton
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.
1 个逻辑单元在旧套件上通常为 1 个像素,在新套件/iPad 上为 2 个像素。这可能是 5 年后的 4 个像素。
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.
这比使用 SKActions 更好(因为用户最终可能会看到它们,并且在操作完成之前不能依赖基于节点尺寸的任何计算),并且它比缩放更好,因为缩放需要您知道原始图像尺寸。
You can do this via the size property, or through an action if you really want to make an animation.
你可以通过 size 属性来做到这一点,或者如果你真的想要制作动画,可以通过一个动作来做到这一点。
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.
或者,如果您的图像在更高分辨率下运行良好,您可以将图像重命名为 [email protected],将其添加到您的项目中,您可能根本不需要调整大小,但您还需要为较低分辨率的设备将其缩小 50%,并将其保存为 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.
我总是将导入图像的大小设置为我想要的逻辑单位(或屏幕尺寸的百分比)以保持我的理智。
回答by James Jones
I've found that if an image is not the same size as the node you are looking to create the sizing gets a little funny (say using a retina image on a non-retina device or incorrect image naming). You can scale the image to fill if you create the texture from the image and set the texture and the node size using something like the following:
我发现如果图像的大小与您要创建的节点的大小不同,则调整大小会有点有趣(比如在非视网膜设备上使用视网膜图像或不正确的图像命名)。如果您从图像创建纹理并使用以下内容设置纹理和节点大小,则可以缩放图像以进行填充:
SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:@"MyImage.png"];
SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture size:self.view.frame.size];
background.position = (CGPoint) {CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)};
[scene addChild:background];
I'm not a 100% sure this if this will fix your issue or not but it might be worth a shot.
我不是 100% 确定这是否会解决您的问题,但它可能值得一试。
回答by Dvole
This line returns size in points, not pixels. This is made because different devices have different resolutions, but will have same sizes in points. On non retina devices 1 point is 1 pixel and on retina devices 1 point is two pixels. Thats why you get this size from
这条线以磅为单位返回大小,而不是像素。这是因为不同的设备具有不同的分辨率,但以点为单位具有相同的大小。在非视网膜设备上,1 个点是 1 个像素,在视网膜设备上,1 个点是两个像素。这就是为什么你从
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
You don't want to double the size of scene since it will just be our of bounds of your screen, invisible.
您不想将场景的大小加倍,因为它只是我们屏幕的边界,不可见。
回答by Dvole
Have you tried adding "@2x" to the end of the name of your image?
您是否尝试在图像名称的末尾添加“@2x”?
回答by Glen Zenfar
I basically do as the answers above however I go ahead and set the width and height directly so that I don't have to worry about if it is set correctly or not, of course I would also need to check if there were using an iPhone4 for a complete solution.
我基本上按照上面的答案做,但是我继续直接设置宽度和高度,这样我就不必担心它是否设置正确,当然我还需要检查是否使用了iPhone4以获得完整的解决方案。
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = YES;
skView.showsNodeCount = YES;
CGSize tmpSize;
tmpSize.width = 640;
tmpSize.height = 1136;
// Create and configure the scene
SKScene * scene = [CreationScene sceneWithSize:tmpSize];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
回答by Jaime Enrique Espinosa Reyes
I did use and sprite for background.
我确实使用和精灵作为背景。
double escalax = self.size.width/ sprite.size.width ;
double escalay = self.size.height/ sprite.size.height ;
SKAction *mostrar = [SKAction scaleXTo:escalax y:escalay duration:0];
Hope it helps!
希望能帮助到你!
回答by Jason
This worked for me. Is this ok to do?
这对我有用。这样做可以吗?
//Set background image.
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"myBackground.png"];
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
background.xScale = 0.5;
background.yScale = 0.5;
[self addChild:background];
回答by MB_iOSDeveloper
Solution:
解决方案:
Put this code into your viewWillLayoutSubviews instead into the viewDidLoad of the UIViewController which loads the sprite.
将此代码放入您的 viewWillLayoutSubviews 中,而不是放入加载精灵的 UIViewController 的 viewDidLoad 中。
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
Explanation : This creates the scene with a size as the bounds of the view. However, when viewDidLoad is called, this is before the view has been added to the view hierarchy and hence it hasn't responded to layout changes yet. So the view bounds might not be correct yet, and this probably isn't the best time to start up the scene.
说明:这将创建一个大小作为视图边界的场景。但是,当 viewDidLoad 被调用时,这是在将视图添加到视图层次结构之前,因此它尚未响应布局更改。所以视图边界可能还不正确,这可能不是启动场景的最佳时间。
This answer is copied from : http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginnersThe credit goes to Ray :).
此答案复制自:http: //www.raywenderlich.com/42699/spritekit-tutorial-for-beginners归功于 Ray :)。
I have used the same code in my SKScene like you, but without this solution Ray offered in the UIViewController which presents the scene it would not work.
我和你一样在我的 SKScene 中使用了相同的代码,但是如果没有这个解决方案,Ray 在 UIViewController 中提供了呈现场景,它就无法工作。
回答by Alfred
I got the same issue with you. After several days googling and trying, finally I got the key of this problem. You can try to change your scene.scaleModeseveral times, until you got the size which you satisfied with. I just found this last night, so if you want to know more, here is a link
我和你有同样的问题。经过几天的谷歌搜索和尝试,终于找到了这个问题的关键。你可以尝试多次改变你的scene.scaleMode,直到你得到你满意的大小。我昨晚才发现这个,所以如果你想知道更多,这里是一个链接