【问题标题】:Call custom action in spritekit around 100 times in 3 seconds在 3 秒内调用 spritekit 中的自定义操作约 100 次
【发布时间】:2016-07-19 14:35:55
【问题描述】:

我用过

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

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

我想在 3-4 秒内运行大约 100-200 次。

任何解决方案??

【问题讨论】:

    标签: swift object sprite-kit skaction


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,你是对的,我有大约 20-25 fps...我认为这是因为所谓的调试和发布方案...我想要 60 fps,我应该实现什么?跨度>
    【解决方案2】:

    我不确定 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)
    

    【讨论】:

    • 感谢您的回答@claassenApps,但我主要关心的是自定义操作。如果你可以尝试回答这个问题,请尝试
    【解决方案3】:

    根据来源:

    /** 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)
    

    输出

    计数器持续到 100

    【讨论】:

    • 不,那不会这样做,您只打印经过的时间百分比,并且因为您将其设置为 3,这就是您以 1 的间隔达到 100% 的原因(在某些时候您应该会看到相同的数字打印了两次)
    • 稍微不精确是可以接受的因素,同样的 customActionWithDuration 方法也不能保证准确性。OP 不需要确切的数字,更不用说所有可能减慢时间的因素。跨度>
    • 是的,但你不提供答案,如果设备以 1fps 运行怎么办
    猜你喜欢
    • 1970-01-01
    • 2014-01-13
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    相关资源
    最近更新 更多