,找不到一個「基本解決」我的問題雪碧不會隨手指移動/ SpriteKit /斯威夫特3
您好,我想創建一個簡單的SKSpriteNode,將移動隨着我的手指在屏幕上的任何地方(x & y coords)。到目前爲止,我所做的是下面的代碼,我似乎無法找到其他的東西,因爲我的Sprite在遊戲運行時甚至不會移動。我也遇到了一個錯誤:
simpleS = self.childNode(withName: "simpleS") as! SKSpriteNode
這行代碼導致我的程序不能運行,因爲我已經添加它。請並且感謝您的幫助。
在這種情況下,Sprite「simpleS」是我嘗試在觸摸開始時移動的Sprite。
代碼從這裏開始:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var simpleS = SKSpriteNode()
override func didMove(to view: SKView) {
simpleS = self.childNode(withName: "simpleS") as! SKSpriteNode
simpleS.physicsBody?.affectedByGravity = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
simpleS.run(SKAction.moveTo(x: location.x, duration: 0.0))
simpleS.run(SKAction.moveTo(y: location.y, duration: 0.0))
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
simpleS.run(SKAction.moveTo(x: location.x, duration: 0.0))
simpleS.run(SKAction.moveTo(y: location.y, duration: 0.0))
}
}
}
此代碼有幾個問題。嘗試按照這樣的指南來獲得更多SpriteKit的速度; https://www.raywenderlich.com/49721/how-to-create-a-breakout-game-using-spritekit。 –