【问题标题】:Swift iOS SKActions not working for sprites brought in from SKS fileSwift iOS SKActions 不适用于从 SKS 文件引入的精灵
【发布时间】:2018-08-22 08:33:21
【问题描述】:

我有一个 spriteKit 项目,它通过从 sks 文件加载精灵来将精灵带到场景中。我知道它们加载正确,因为我可以对它们执行其他功能。在模拟器中,他们甚至运行 SKActions,但在实际设备(iPad pro gen 2)上,他们不运行这些动作。我制作了以下测试程序来说明问题。

具体的问题是为什么从 SKS 文件加载的精灵不在真实设备上运行 SKActions?

import SpriteKit
import GameplayKit

class GameScene: SKScene {


private var fileDropSite: SKSpriteNode!

var lessonNode: SKSpriteNode?
var lessonButton: SKSpriteNode?


override func didMove(to view: SKView) {
    fileDropSite = childNode(withName: "fileDropSite") as! SKSpriteNode

}

func bringSKSTOLoad() {
    fileDropSite.removeAllChildren()
    let lessonToLoad = SKSpriteNode(fileNamed: "practiceLesson")?.childNode(withName: "unitNode")
    lessonToLoad?.move(toParent: fileDropSite)

    lessonNode = fileDropSite.childNode(withName: "//lessonNode") as? SKSpriteNode
    lessonButton = fileDropSite.childNode(withName: "//lessonButton") as? SKSpriteNode
    print("lessonLoaded")
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let touchLocation = touch.location(in: self)
        let touchedNode = atPoint(touchLocation)
        if let nodesName = touchedNode.name {
            if nodesName == "fileDropSite" {
                print("fileDropSite TOuched")
                bringSKSTOLoad()
            }
            if nodesName == "lessonButton" {
                print("lessonButton TOuched")
                lessonNode?.run(SKAction.repeatForever(SKAction.sequence([SKAction.moveBy(x: -100, y: 0, duration: 0.5), SKAction.moveBy(x: 100, y: 0, duration: 0.5)])))
            }
        }
    }
}

澄清一下:fileDropSite 是游戏场景中的子精灵,课程节点和课程按钮是名为 practiceLesson 的文件中的精灵,并且是名为 unitNode 的精灵的子精灵。下面的代码将这些初始化为变量,并在触摸 fileDropSite 精灵时,然后通过 fileDropSite(场景的子节点)将文件中的精灵加载到场景中,并初始化它们。有一个触摸功能会导致课程节点及其子节点在模拟器上来回晃动,但在 rea 设备上不会。

【问题讨论】:

    标签: ios swift sprite-kit xcode9 skaction


    【解决方案1】:

    试试……

    override func didMove(to view: SKView) {
        fileDropSite = childNode(withName: "fileDropSite") as! SKSpriteNode
        self.isPaused = false
    }
    

    从 iOS11 开始,Apple 已将场景和 SKSpriteNodes 的默认状态设为暂停。一般在现场运行self.isPaused = false 对我有用。

    如果需要的话……

    func bringSKSTOLoad() {
        fileDropSite.removeAllChildren()
        let lessonToLoad = SKSpriteNode(fileNamed: "practiceLesson")?.childNode(withName: "unitNode")
        lessonToLoad?.move(toParent: fileDropSite)
    
        lessonNode = fileDropSite.childNode(withName: "//lessonNode") as? SKSpriteNode
        lessonButton = fileDropSite.childNode(withName: "//lessonButton") as? SKSpriteNode
        print("lessonLoaded")
        lessonToLoad.isPaused = false
        lessonNode.isPaused = false
        lessonButton.isPaused = false
    }
    

    【讨论】:

    • 我的第一个想法是,我已经尝试将 fileDropSite.isPaused 设置为 false,但我没有想到要遍历其他所有内容。但你是对的。事实证明,在这种情况下,由于我要对其执行操作的是课程节点,因此需要专门取消暂停。我想仅仅取消暂停它来自的父级是不够的。尽管如此,我还是不敢相信我花了一天的大部分时间来思考这个问题!非常感谢。
    • 欢迎您,很高兴它有所帮助。请将此标记为正确,以便其他人可以从这个问题中受益。
    • 完成!再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多