我正在使用的这个动画有效,但它是否正确?
This animate I am using works, but is it correct?
所以我是 Swift 的新手,在 YouTube 上上课,正在拼凑各种东西。例如,要将 SKSpriteNode 移回其原始位置,我有:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let touchedNodes = self.nodes(at: location)
for node in touchedNodes.reversed() {
if node.name == "draggable" {
originalNode = touch.location(in: self)
self.currentNode = node
guard let scene = node.scene else { fatalError("Wait this node is not inside a scene!?") }
thePosition = node.convert(node.position, to: scene)
} } } }
func addMovement (obstacle:SKSpriteNode, origNP: CGPoint) {
// var actionArray = [SKAction]()
let moveAction = SKAction.move(to: CGPoint(x: origNP.x, y: origNP.y), duration: 1.0)
let sequence = SKAction.sequence([moveAction])
obstacle.run(sequence)
}
我在网上找到的原代码只有一个参数(障碍)并调用了这一行:
let moveAction = SKAction.move(to: CGPoint(x: obstacle.position.x, y: obstacle.position.x.y), duration: 1.0)
我只想知道我这样做的方式是正确的。不是我走运以后它会回来咬我的情况。
取决于您要实现的目标。移动动作本身似乎还可以。
虽然您不需要为单个操作使用序列。序列旨在用于一个接一个地链接多个动作。由于您只有一项操作,因此您可以将代码更新为:
let moveAction = SKAction.move(to: CGPoint(x: origNP.x, y: origNP.y), duration: 1.0)
obstacle.run(moveAction)
所以我是 Swift 的新手,在 YouTube 上上课,正在拼凑各种东西。例如,要将 SKSpriteNode 移回其原始位置,我有:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let touchedNodes = self.nodes(at: location)
for node in touchedNodes.reversed() {
if node.name == "draggable" {
originalNode = touch.location(in: self)
self.currentNode = node
guard let scene = node.scene else { fatalError("Wait this node is not inside a scene!?") }
thePosition = node.convert(node.position, to: scene)
} } } }
func addMovement (obstacle:SKSpriteNode, origNP: CGPoint) {
// var actionArray = [SKAction]()
let moveAction = SKAction.move(to: CGPoint(x: origNP.x, y: origNP.y), duration: 1.0)
let sequence = SKAction.sequence([moveAction])
obstacle.run(sequence)
}
我在网上找到的原代码只有一个参数(障碍)并调用了这一行:
let moveAction = SKAction.move(to: CGPoint(x: obstacle.position.x, y: obstacle.position.x.y), duration: 1.0)
我只想知道我这样做的方式是正确的。不是我走运以后它会回来咬我的情况。
取决于您要实现的目标。移动动作本身似乎还可以。
虽然您不需要为单个操作使用序列。序列旨在用于一个接一个地链接多个动作。由于您只有一项操作,因此您可以将代码更新为:
let moveAction = SKAction.move(to: CGPoint(x: origNP.x, y: origNP.y), duration: 1.0)
obstacle.run(moveAction)