Ios 分数无限增长
我在Ios 分数无限增长,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我在update方法中使用了以下逻辑来跟踪节点的位置,一旦到达某个点,它将从场景中移除,并且分数将增加1 override func update(currentTime: CFTimeInterval) { /* Called before each frame is rendered */ println(colorDot.position) var checkPoint = CGPoint(x: self.frame.size.width / 2, y: self.
update
方法中使用了以下逻辑来跟踪节点的位置,一旦到达某个点,它将从场景中移除,并且分数将增加1
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
println(colorDot.position)
var checkPoint = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 3 + 50)
if colorDot.position.y < checkPoint.y {
if colorDotColor == centerBoxColor {
score++
colorDot.removeFromParent()
} else {
println("unable to calculate score")
colorDot.removeFromParent()
}
}
println(score)
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
println(色点位置)
var checkPoint=CGPoint(x:self.frame.size.width/2,y:self.frame.size.height/3+50)
如果colorDot.position.y
我遇到的问题是,当色点达到某一点时,它将从场景中移除,但分数将继续增加。我认为我的代码中有一个逻辑缺陷。下面是控制台的屏幕截图
仅因为要从父节点删除
色点,不会从对象中删除实例。这意味着将通过对检查点的位置检查
// This will be true even if colorDot is not part of the node hierarchy
if colorDot.position.y < checkPoint.y
//即使colorDot不是节点层次结构的一部分,也会出现这种情况
如果colorDot.position.y
您可以扩展检查以确保它仍在层次结构中:
if colorDot.parent != nil && colorDot.position.y < checkPoint.y
如果colorDot.parent!=nil&&colorDot.position.y
使用colorDot.removeFromParent()
时,colorDot
对象仍有一个位置,除非删除或覆盖该对象,否则if colorDot.position.y
将为true
尝试添加
如果colorDot.parent===self
(如果colorDot被添加到self
)。在从父对象中移除之前,尝试添加这行代码。取消以前的性能请求swithtarget:colorDot谢谢,但它不起作用,分数仍会继续上升。您是否尝试通过:var colorDot:SKSpriteNode使节点成为可选节点?然后,您还需要在将其从父级中删除后将其置零