我试图添加一个图像到我的游戏场景,但它不会出现。下面是我的代码。希望你们能帮助我
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var Ground = SKSpriteNode()
var iceCream = SKSpriteNode()
override func didMove(to view: SKView) {
Ground = SKSpriteNode(imageNamed: "background")
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: self.Ground.frame.height / 2)
self.addChild(Ground)
iceCream = SKSpriteNode(imageNamed: "VanillaIceCream")
iceCream.size = CGSize(width: 60, height: 70)
iceCream.position = CGPoint(x: self.iceCream.frame.width, y: self.frame.height / 2)
self.addChild(iceCream)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: CFTimeInterval) {
}
}
最佳答案
当您将position
设置为x: self.iceCream.frame.width
您可以使用iceCream
定位它。
我相信把它改成x: self.frame.width / 2
会解决这个问题。
如果没有错误,并且您想在屏幕左侧设置iceCream
,请在将其添加到self之后定位sprite
。
为每个元素设置zPosition
也很好,以确保它不会隐藏在background
图像后面。
编辑:
以下是方法的完整代码(准备复制/粘贴):
Ground = SKSpriteNode(imageNamed: "background")
self.addChild(Ground)
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
Ground.zPosition = 1.0
iceCream = SKSpriteNode(imageNamed: "VanillaIceCream")
self.addChild(iceCream)
iceCream.size = CGSize(width: 60, height: 70)
iceCream.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
iceCream.zPosition = 2.0