我正在研究一个项目,并尝试使用SKAction
重设位置,但SKSpriteNode
没动!谁能帮忙?
import SpriteKit
import Foundation
enum Difficulty: TimeInterval {
case easy = 8.5
case normal = 6.0
case hard = 4.0
case extraHard = 2.0
}
class Note: SKSpriteNode {
init() {
super.init(texture: SKTexture(imageNamed: "dropNotes"), color: .clear, size: CGSize(width: 50, height: 50))
self.position = CGPoint(x: 375, y: 999)
}
func fall(difficulty: Difficulty) {
let fallAction = SKAction.move(
to: CGPoint(
x: 375,
y: 180
),
duration: difficulty.rawValue
)
let resetAction = SKAction.move(to: CGPoint(x: 375, y: 700), duration: 0.01)
run(fallAction)
run(resetAction)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
有人知道为什么会这样吗?如果回答,请回答!
查看代码的这一部分
run(fallAction)
run(resetAction)
您正在相同时间运行这两个动作。相反,我猜您想运行fallAction
,然后AFTER完成后,resetAction
对吗?
在这种情况下,您需要创建一个序列并运行它。
let sequence = SKAction.sequence([fallAction, resetAction])
run(sequence)
这里是完整的秋季方法更新
func fall(difficulty: Difficulty) {
let fallAction = SKAction.move(
to: CGPoint(
x: 375,
y: 180
),
duration: difficulty.rawValue
)
let resetAction = SKAction.move(to: CGPoint(x: 375, y: 700), duration: 0.01)
let sequence = SKAction.sequence([fallAction, resetAction])
run(sequence)
}