我正在开发我的游戏,但我无法删除一个操作。
我有一组永远从右向左移动的管道(就像飞扬的鸟)。
我在一个函数中创建了管道,并在 didMoveToView
中移动了它们。因此,在管道函数中,我创建了管道并运行了操作。
(我简化了代码)
func initPipes() {
let pipePair = SKNode()
pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
//PIPE 1
let pipe1 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))
//here i set physics and position
pipePair.addChild(pipe1)
//PIPE 2
let pipe2 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))
//same as pipe1
pipePair.addChild(pipe2)
//SCORE
let scoreSprite = SKSpriteNode(color: SKColor.clearColor(), size: CGSize(width: 50, height: 150))
pipePair.addChild(scoreSprite)
pipePair.runAction(moveAndRemove, withKey: "move")
pipes.addChild(pipePair)
}
当检测到碰撞时,我运行一个 gameOver
函数。在这个函数里面,如果我写:
removeActionForKey("move")
管道继续移动。可能是因为我必须编写 pipePair.removeActionForKey("move")
。但是由于 pipePair
是本地的,所以我不能这样做。我将如何解决这个问题?
最佳答案
您可以为pipePair
节点命名:
pipePair.name = "pipePair"
然后你可以像这样在 gameOver
方法中停止“移动” Action :
let pipePair = pipes.childNodeWithName("pipePair")
if(pipePair.actionForKey("move") != nil){
pipePair.removeActionForKey("move")
}
我猜这是一种方法。
关于swift - 删除在函数内声明的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31815303/