为什么 SKSpriteNode 不能与 SKAction 一起工作?

Why is SKSpriteNode not working with SKAction?

我正在做一个项目并尝试用 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)

您运行同时执行这 2 个操作。相反,我猜您想 运行 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)
}