Swift iOS SKActions 不适用于从 SKS 文件引入的精灵
Swift iOS SKActions not working for sprites brought in from SKS file
我有一个 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 是游戏场景中的子精灵,lessonNode 和 lessonButton 是名为 practiceLesson 的文件中的精灵,并且是名为 unitNode 的精灵的子精灵。以下代码将这些初始化为变量,并在接触 fileDropSite 精灵时,然后通过 fileDropSite(场景的子元素)将文件中的精灵加载到场景中,并初始化它们。有一个触摸功能会导致 lessonNode 及其子节点在模拟器上来回摇晃,但在真实设备上不会。
尝试...
override func didMove(to view: SKView) {
fileDropSite = childNode(withName: "fileDropSite") as! SKSpriteNode
self.isPaused = false
}
从 iOS11 开始,Apple 已将场景和 SKSpriteNode 的默认状态设置为暂停。一般运行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
}
我有一个 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 是游戏场景中的子精灵,lessonNode 和 lessonButton 是名为 practiceLesson 的文件中的精灵,并且是名为 unitNode 的精灵的子精灵。以下代码将这些初始化为变量,并在接触 fileDropSite 精灵时,然后通过 fileDropSite(场景的子元素)将文件中的精灵加载到场景中,并初始化它们。有一个触摸功能会导致 lessonNode 及其子节点在模拟器上来回摇晃,但在真实设备上不会。
尝试...
override func didMove(to view: SKView) {
fileDropSite = childNode(withName: "fileDropSite") as! SKSpriteNode
self.isPaused = false
}
从 iOS11 开始,Apple 已将场景和 SKSpriteNode 的默认状态设置为暂停。一般运行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
}