2

我有一个SKEmitterNode,当按下按钮时我试图停止它。我以这种方式添加我的节点:

let followLine = SKAction.followPath(border.CGPath, asOffset: false, orientToPath: true, duration: 2.0)
let loopAction = SKAction.repeatActionForever(followLine)
emitterNode.targetNode = scene
emitterNode.runAction(loopAction, withKey: "loop")
addChild(emitterNode)

我将emitterNode添加到我的SKScene,当我想停止粒子时,我尝试了所有这些可能的方法:

let action = SKAction.runBlock { [weak self] in
    self?.emitterNode.particleBirthRate = 0
}
emitterNode.runAction(action)


emitterNode.removeAllActions()
emitterNode.removeFromParent()


removeAllActions()


let remove = SKAction.removeFromParent()
emitterNode.removeActionForKey("loop")
emitterNode.runAction(remove)

发射器不会停止,动画会继续。

4

1 回答 1

0

我发现这是我的代码的问题。我试图停止在计算机属性中创建的发射器节点,因此在访问它时分配了它。该实例显然不一样,并且发射器节点没有停止。这是我的建议。不要将计算机属性的语法与使用闭包初始化属性的语法混淆。这两段代码非常不同:

// Created only once
var laserButton: ParticlesLoadingButton = {
    let button = ParticlesLoadingButton(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
    button.particleEffect = .Laser
    button.particleColor = UIColor.orangeColor()
    return button
}()

// Created every time it is accessed
var laserButton2: ParticlesLoadingButton {
    let button = ParticlesLoadingButton(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
    button.particleEffect = .Laser
    return button
}
于 2016-02-14T19:38:28.413 回答