SKAction不停止

时间:2017-05-27 18:49:15

标签: ios swift sprite-kit skaction

我用SKAction做了一个计时器,但当我停止动作时不停止

创建计时器

    func createTimer (name:@escaping os_block_t , wait:TimeInterval, sprite:SKSpriteNode?,actioname: String) {

    var myaction = SKAction()

    myaction = SKAction.sequence([SKAction.run(name), SKAction.wait(forDuration: wait)])

    sprite?.run(SKAction.repeatForever(myaction), withKey: actioname)
}

停止计时器

    func stopTimer(actioname:String, sprite:SKSpriteNode?) {

    if sprite != nil {
        sprite?.removeAction(forKey: actioname)
    }else {
        self.removeAction(forKey: actioname)
    }
}

这里我用createTimer函数

创建一个计时器
func didBegin(_ contact: SKPhysicsContact) {

    let firstBody = contact.bodyA
    let secondBody = contact.bodyB

    //Biscuit and Liquid(line)
    if firstBody.categoryBitMask == biscuitCategory && secondBody.categoryBitMask == liquidlineCategory && isGameover == false && isCollision == false || firstBody.categoryBitMask == liquidlineCategory && secondBody.categoryBitMask == biscuitCategory && isGameover == false && isCollision == false {

        //Change collision to true
        isCollision = true

        //Make haptic
        makehpatic(vol: .light)

        //Change gamemode to Ready & label to Ready
        mymode = gamemode.ready
        modetitle.texture = SKTexture(imageNamed: "Ready")
        playsound(spritename: modetitle, soundname: "Modesound")

        //Play sound
        playsound(spritename: biscuit, soundname: "Watersound")

        //Get random number
        collisiontimerrandom = CGFloat.random(min: 0.2, max: 5.2)

        //Start the timer when biscuit and liquid collision
        createTimer(name: updatecollisionTimer, wait: 0.1, sprite: liquid, actioname: collisiontimer)

    }

在这里,如果我的2个变量相等但不停止

,我会停止动作(计时器)
//Update collision timer func
func updatecollisionTimer () {

    //Increase count by 0.1
    collisiontimercount += 0.1

    //Convert collisiontimer from CGFloat to String
    let collisionsrandomstring = String(format:"%.1f", collisiontimerrandom)

    //Check if the timer is equal to the random number
    if collisiontimercount.description == collisionsrandomstring {

        print("Random: \(collisionsrandomstring)")

        //Change label to Out
        mymode = gamemode.out
        modetitle.texture = SKTexture(imageNamed: "Out")
        playsound(spritename: modetitle, soundname: "Modesound")

        //Stop count timer (collisiontimer)
        stopTimer(actioname: collisiontimer, sprite: liquid)

        //Start release timer
        createTimer(name: updatereleaseTimer, wait: 0.1, sprite: biscuit ,actioname: releasetimer)

        print("Release \(releasetimercount)")
    }
}

0 个答案:

没有答案