【问题标题】:Swift: how to invalidate timer on animated SKShapenode countdown?Swift:如何使动画 SKShapenode 倒计时的计时器无效?
【发布时间】:2015-11-12 16:34:43
【问题描述】:

好的,我正在构建一个 sprite kit 游戏,在我之前的一个问题中,我得到了让圆形 skshapenode 根据用完的计时器“展开”的代码(这样圆形已经消失了)计时器完成)。

这是我的问题:Swift, sprite kit game: Have circle disappear in clockwise manner? On timer? 这是我一直在使用的代码:

func addCircle() {

        circle = SKShapeNode(circleOfRadius: 50)
        let circleColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
        circle.fillColor = circleColor
        circle.strokeColor = SKColor.clearColor()
        circle.position = CGPoint (x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5+45)
        circle.zPosition = 200
        circle.zRotation = CGFloat(M_PI_2)
        addChild(circle)

        countdown(circle, steps: 120, duration: 5) { //change steps to change how much of the circle it takes away at a time

                //Performed when circle timer ends:
                print("circle done")
                self.gameSoundTrack.stop()

                self.removeAllChildren()
                self.removeAllActions()
                self.viewController.removeSaveMe(self)

                self.GOSceneNodes() //Implements GOScene
                self.viewController.addGOScene(self) //Implements GOViewController
        }

    }
// Creates an animated countdown timer
    func countdown(circle:SKShapeNode, steps:Int, duration:NSTimeInterval, completion:()->Void) {
        let radius = CGPathGetBoundingBox(circle.path).width/2
        let timeInterval = duration/NSTimeInterval(steps)
        let incr = 1 / CGFloat(steps)
        var percent = CGFloat(1.0)

        let animate = SKAction.runBlock({
            percent -= incr
            circle.path = self.circle(radius, percent:percent)
        })
        let wait = SKAction.waitForDuration(timeInterval)
        let action = SKAction.sequence([wait, animate])

        runAction(SKAction.repeatAction(action,count:steps-1)) {
            self.runAction(SKAction.waitForDuration(timeInterval)) {
                circle.path = nil
                completion()
            }
        }
    }

    // Creates a CGPath in the shape of a pie with slices missing
    func circle(radius:CGFloat, percent:CGFloat) -> CGPath {
        let start:CGFloat = 0
        let end = CGFloat(M_PI*2) * percent
        let center = CGPointZero
        let bezierPath = UIBezierPath()
        bezierPath.moveToPoint(center)
        bezierPath.addArcWithCenter(center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
        bezierPath.addLineToPoint(center)
        return bezierPath.CGPath
    }

这一切都很好,但是如果按下游戏中的另一个按钮,我需要一种方法来使圆圈的倒计时无效,以便在计时器用完时执行的所有操作(上面//Performed when circle timer ends: 之后的所有操作)都不会执行.

我试过了

        circle.removeAllActions()
        circle.removeFromParent()

当按下按钮时,这只是删除了圆圈,即使圆圈消失了,一切仍会执行。

我对 Swift 很陌生,我不知道我需要使这 3 个函数中的哪一部分失效或停止以阻止倒计时完成。我怎样才能做到这一点?

【问题讨论】:

    标签: swift


    【解决方案1】:

    您可以通过向动作添加键来停止动作,然后运行

    removeActionForKey("actionKey")
    

    要向操作添加键,请将countdown 方法替换为以下内容:

    // Creates an animated countdown timer
    func countdown(circle:SKShapeNode, steps:Int, duration:NSTimeInterval, completion:()->Void) {
        let radius = CGPathGetBoundingBox(circle.path).width/2
        let timeInterval = duration/NSTimeInterval(steps)
        let incr = 1 / CGFloat(steps)
        var percent = CGFloat(1.0)
    
        let animate = SKAction.runBlock({
            percent -= incr
            circle.path = self.circle(radius, percent:percent)
        })
        let wait = SKAction.waitForDuration(timeInterval)
        let action = SKAction.sequence([wait, animate])
    
        let completed = SKAction.runBlock{
            circle.path = nil
            completion()
        }
    
        let countDown = SKAction.repeatAction(action,count:steps-1)
        let sequence = SKAction.sequence([countDown, SKAction.waitForDuration(timeInterval),completed])
    
        runAction(sequence, withKey: "actionKey")
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多