背景图像大小雪碧套件游戏

我刚开始一个新的Sprite Kit项目来学习如何使用它。 我看了很多教程,但没有教程对我的问题/问题有一个答案。

我想为我的iPhone 5S创build一个应用程序。 所以屏幕尺寸是1136×640。 我为我的应用程序创build了一个1136×640背景图像。 但是,当我把图像添加到我的应用程序,它的waaay大! iOS模拟器只显示图像的中间部分。

有人能告诉我什么屏幕大小,我必须使用,为什么?

非常感谢!

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

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

编辑

我search谷歌,发现这一点:

viewDidLoad方法必须用“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行是:

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

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

你眼前的问题的简短答案:

 background.size = self.frame.size; 

漫长的回答,关于其他方法的讨论…

场景在逻辑单元中工作,而不是物理像素(如其他文章中提到的)。

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

以逻辑单位工作可以保护您免受将来变化的大小的影响,并应该帮助程序员 – 尽pipe这经常会让新手感到困惑。

获取图像填充当前场景的最简单方法是将其大小设置为“逻辑单位”,而不pipe其大小是多less。

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

你可以通过尺寸属性来做到这一点,或者如果你真的想做一个animation,可以通过一个动作。

在现场简单….

 -(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,将其添加到您的项目中,而且您可能不需要resize,但是您还需要为较低分辨率的设备缩小50%,并保存为myBackground.png。

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

我发现,如果图像尺寸与您要创build的节点尺寸不一样,那么尺寸会变得有点滑稽(比如在非视网膜设备上使用视网膜图像或图像命名不正确)。 如果您从图像创build纹理并使用如下所示设置纹理和节点大小,则可以缩放图像以填充图像:

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

如果这能解决你的问题,我不是百分之百肯定的,但可能值得一试。

此行以磅为单位返回大小,而不是像素。 这是因为不同的设备具有不同的分辨率,但是具有相同的点数。 在非视网膜设备上,1点是1像素,在视网膜设备上1点是2像素。 这就是为什么你从这个尺寸

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

你不想把场景的大小加倍,因为它只是我们的屏幕边界,不可见的。

你有没有尝试添加“@ 2x”到图像名称的末尾?

我基本上做了上面的答案,但我继续前进,直接设置宽度和高度,以便我不必担心,如果它设置正确或不,当然,我也需要检查是否有使用iPhone4获得完整的解决scheme。

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

这对我有效。 这可以吗?

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

解:

把这段代码放到你的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]; } } 

说明:这将创build与视图边界一样大小的场景。 但是,当调用viewDidLoad时,这是视图添加到视图层次结构之前,因此它尚未响应布局更改。 所以视界可能不正确,而这可能不是启动现场的最佳时机。

这个答案是从复制: http : //www.raywenderlich.com/42699/spritekit-tutorial-for-beginners功劳归功于Ray :)。

我已经在你的SKScene中使用了相同的代码,但是没有这个解决scheme,Ray在UIViewController中提供了这个场景,它不起作用。

我曾经使用和精灵的背景。

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

希望能帮助到你!

我遇到了同样的问题 经过几天的search和尝试,终于得到了这个问题的关键。 你可以尝试改变你的scene.scaleMode几次,直到你得到你满意的大小。 我昨晚才发现,所以如果你想知道更多,这里是一个链接

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKScene_Ref/Reference/Reference.html#//apple_ref/doc/uid/TP40013024-CH1-DontLinkElementID_3