为什么SKSpriteNode无法与SKAction一起使用?

问题描述 投票:0回答:1

我正在研究一个项目,并尝试使用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")
    }
}

有人知道为什么会这样吗?如果回答,请回答!

swift skspritenode skaction
1个回答
0
投票

问题

查看代码的这一部分

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.