我创造了一个包含具有重力和恢复属性的球的游戏(每个球通过随机函数具有不同的属性)。现在我想检测每个球的最后位置,然后调用从节点函数中删除。我使用更新功能来检测球在屏幕上跳跃时的位置。我不确定我是否可以,我怎样才能发现它的最后位置。我正在考虑创建一个存储每个位置的数组,但我不确定它是否正确,并想知道是否有更简单和更好的方法。 这是我现在的代码:
override func update(_ currentTime: TimeInterval) {
self.enumerateChildNodes(withName: "BALL") { (node:SKNode, nil) in
print(node.position)
}
}
你可以帮帮我吗?数组选项是唯一的方式还是有另一个更好的选择?
答案 0 :(得分:0)
您可以对球进行子类化并添加您自己的存储位置的属性。
class BallNode: SKShapeNode {
var lastPosition: CGPoint?
}
let ball = BallNode()
ball.name = “BALL”
ball.position = CGPoint.zero
self.addChild(ball)
在更新....
override func update(_ currentTime: TimeInterval) {
self.enumerateChildNodes(withName: "BALL") { (node:BallNode, nil) in
print(node.position)
if let last= node.lastPosition {
print(last)
// do something with last
}
//Update last position before end of updatE
node.lastPosition = node.position
}
}