简体   繁体   English

检测SKAction是否已完成运行,Swift

[英]Detecting if SKAction is done running, Swift

I want to change the value of a variable after an action has been run and not during it is running. 我想在运行一个动作后更改变量的值,而不是在它运行期间。 This is my code: 这是我的代码:

   override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    if gameNotStarted {
        if firePosition.x != 320 || firePosition.y > 280 {  
            fire.runAction(fireButtonReturn)
            iapButton.runAction(iapButtonReturn)
            aboutButton.runAction(abtButtonReturn)

            if fire.hasActions() == false {
                println("has no actions")
                firePosition.x = 320
                firePosition.y = 280
            } 
        }       
    }
}

The three actions I am running have exactly the same duration so I only need one to check if they have finished running. 我正在运行的三个操作具有完全相同的持续时间,所以我只需要一个来检查它们是否已经完成运行。 After they have finished running I want to change the value of firePosition.x and firePosition.y . 运行完毕后,我想更改firePosition.xfirePosition.y的值。 It is very important that fire position values change exactly after the action has been run and absolutely not before the actions are done running. 非常重要的是,火灾位置值在动作运行后完全改变,绝对不是在动作完成运行之前。

However with my current code I never run the if fire.hasActions() ... part according to results and console. 但是根据我的当前代码,我根本不会根据结果和控制台运行if fire.hasActions() ...部分。 I found similar questions, they were in obj-c. 我发现了类似的问题,他们是在obj-c。 Thanks for your help. 谢谢你的帮助。

You should use a completion-handler for your kind of problem: 您应该使用完成处理程序来解决您的问题:

//Run the action
iapButton.runAction(iapButtonReturn,
    //After action is done, just call the completion-handler.
    completion: {
        firePosition.x = 320
        firePosition.y = 280
    }
)

Or you could use a SKAction.sequence and add your actions inside a SKAction.block : 或者你可以使用一个SKAction.sequence并添加里面你的行动SKAction.block

var block = SKAction.runBlock({
    fire.runAction(fireButtonReturn)
    iapButton.runAction(iapButtonReturn)
    aboutButton.runAction(abtButtonReturn)
})

var finish = SKAction.runBlock({
    firePosition.x = 320
    firePosition.y = 280
})

var sequence = SKAction.sequence([block, SKAction.waitForDuration(yourWaitDuration), finish])

self.runAction(sequence)

you should use a completion handler: 你应该使用一个完成处理程序:

fire.runAction(fireButtonReturn,
    completion: {
        println("has no actions")
        firePosition.x = 320
        firePosition.y = 280
    }
)

the problem with your solution is, that the action is initiated with the runAction call but then runs in the background while the main thread continues the execution (and therefore reaches it before the action is finished). 您的解决方案的问题是,该操作是使用runAction调用启动的,但随后在主线程继续执行时在后台运行(因此在操作完成之前到达它)。

extension SKNode {
    func actionForKeyIsRunning(key: String) -> Bool {
        if self.actionForKey(key) != nil {
            return true
        } else {
            return false
        }
    }
}

You can use it : 你可以用它:

if myShip.actionForKeyIsRunning("swipedLeft") {
   print("swipe in action..")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM