【发布时间】: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