SpriteKit背景添加到场景之外

时间:2023-01-22 22:22:19

I'm trying to position the background all over the scene background, but for some reason it puts it outside of screen.

我试图在场景背景中定位背景,但由于某种原因它将它放在屏幕之外。

This is what I used:

这是我用过的:

SKTexture *backgroundTexture = [self.atlas textureNamed:@"back"];
SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture size:self.view.frame.size];
background.position = (CGPoint) {CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)};
[self addChild:background];

This is how it puts it:

这就是它的用法:

SpriteKit背景添加到场景之外

2 个解决方案

#1


You need to add your background image to the SKScene view and as such determine the coordinates from self.

您需要将背景图像添加到SKScene视图中,并确定自身的坐标。

SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithImageNamed:@"BackgroundImage"];
backgroundNode.position = CGPointMake(self.size.width/2, self.size.height/2);
backgroundNode.zPosition = 1;
[self addChild:backgroundNode];

#2


Actually the problem was that in the new Xcode 6.3.1 when you create a new scene it adds a SKScene view of some kind, this view is bigger then the iPhone normal screen, maybe it's iPhone 6+ screen size that's why the background image was out of screen bounds. I just had to remove this view file and not reload it from code and then it worked out.

实际上问题是在新的Xcode 6.3.1中,当你创建一个新的场景时,它会添加某种类型的SKScene视图,这个视图比iPhone普通屏幕大,也许它的iPhone 6+屏幕大小就是为什么背景图像是超出屏幕范围。我只需删除此视图文件,而不是从代码重新加载它然后它解决了。

Thanks anyway, the comment above helped me to understand that.

无论如何,谢谢,上面的评论让我明白了。

#1


You need to add your background image to the SKScene view and as such determine the coordinates from self.

您需要将背景图像添加到SKScene视图中,并确定自身的坐标。

SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithImageNamed:@"BackgroundImage"];
backgroundNode.position = CGPointMake(self.size.width/2, self.size.height/2);
backgroundNode.zPosition = 1;
[self addChild:backgroundNode];

#2


Actually the problem was that in the new Xcode 6.3.1 when you create a new scene it adds a SKScene view of some kind, this view is bigger then the iPhone normal screen, maybe it's iPhone 6+ screen size that's why the background image was out of screen bounds. I just had to remove this view file and not reload it from code and then it worked out.

实际上问题是在新的Xcode 6.3.1中,当你创建一个新的场景时,它会添加某种类型的SKScene视图,这个视图比iPhone普通屏幕大,也许它的iPhone 6+屏幕大小就是为什么背景图像是超出屏幕范围。我只需删除此视图文件,而不是从代码重新加载它然后它解决了。

Thanks anyway, the comment above helped me to understand that.

无论如何,谢谢,上面的评论让我明白了。