[英]How do I place a background image wider than the scene using SpriteKit?
我想知道如何将SKSpriteNode将与上一SKScene 3,240 1,920由像素的SKTexture的背景图像,而我把它用在雨燕3 SpriteKit宽高比。
这背后的想法是使图像比场景更宽,以在玩家移动时实现视差效果。
下面的代码放置图像但缩小它以适合水平突破其宽高比的场景:
let texture = SKTexture(imageNamed: "Background")
let background = SKSpriteNode(texture: texture)
if let view = self.view {
background.size = view.bounds.size
}
background.anchorPoint = CGPoint.zero
addChild(background)
下面的代码变体放置图像,但是在这种情况下仅在垂直方向上挤压它的纵横比:
let texture = SKTexture(imageNamed: "Background")
let background = SKSpriteNode(texture: texture)
if let view = self.view {
background.size.height = view.bounds.size.height
}
background.anchorPoint = CGPoint.zero
addChild(background)
如果我保留background.size
属性未设置,则场景中不会显示任何图像。
如果我尝试使用1,080×1,920像素的SKTexture相同的代码,它工作正常。
此外,场景scaleMode
属性设置为以下代码行:
gameScene.scaleMode = .aspectFill
任何形式的帮助将非常感谢。
非常感谢您的宝贵时间。
干杯,费德里科。
如果您的背景图像具有您想要的尺寸,即3,240 x 1,920px,那么只需按原样放置 - 不要改变它的高度或任何地方的尺寸。
你说:
如果我保留background.size属性未设置,则场景中不会显示任何图像。
我刚刚将一个3,240乘1,920px的精灵放入一个没有问题的sks文件中。 精灵是否有可能但是在某些东西之后,即zPosition较低? 或者图像的大小不是您认为的那样? 加
print("Background size is \\(background.size)")
添加精灵后。
您可能还需要查找场景的.scaleMode
如设置属性GameViewcontroller.swift
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.