在spritekit中调用自定义操作大约100次,在3秒内完成

时间:2016-07-19 14:35:56

标签: swift object sprite-kit skaction

我用过

SKAction.customActionWithDuration(1, actionBlock: { (node: SKNode!, elapsedTime: CGFloat) -> Void in print("Hello")})

它只在1秒内跑了5次。当我切换到0.5秒时,它只运行了3次。

我希望在3-4秒内运行约100-200次。

任何解决方案?

3 个答案:

答案 0 :(得分:1)

customActionWithDuration每帧都会更新,如果它在1秒内运行5次,这意味着您的更新发生在200毫秒,您还有其他问题。理想情况下,您希望每隔16.6毫秒或每秒60帧调用它,因此请调查您的更新阶段,并找出导致速度减慢的原因。如果您在模拟器上运行,请确保模拟器以60fps运行。

如果您需要一致的内容,那么customActionWithDuration不适合您。

您可能需要使用repeatAction(action:,count:)来实现您的目标。

答案 1 :(得分:0)

我不确定SKAction速度的限制是什么,但你可以试试这个:

let waitAction = SKAction.waitForDuration(0.03) //0.03 is 100 times in 3 seconds
let blockAction = SKAction.runBlock({(node: SKNode!, elapsedTime: CGFloat) -> Void in print("Hello")}) 

let actionSequence = SKAction.sequence([waitAction, blockAction])

self.runAction(actionsSequence)

答案 2 :(得分:0)

根据消息来源:

/** Creates an action that executes a block over a duration
     @param duration The duration of the animation
     @param actionBlock The block to run. The block takes the following parameters:
     node The node on which the action is running.
     elapsedTime The amount of time that has passed in the animation.
     */
    public class func customActionWithDuration(seconds: NSTimeInterval, actionBlock block: (SKNode, CGFloat) -> Void) -> SKAction

您可以尝试:

let duration:NSTimeInterval = 3.0
let action = SKAction.customActionWithDuration(duration, actionBlock: { (node: SKNode!, elapsedTime: CGFloat) -> Void in
    let t = Int((elapsedTime/CGFloat(duration))*100)
    print("\(t)hello")
})
self.runAction(action)

<强>输出

enter image description here

计数器一直持续到达<100