我如何设置一个计时器,我的节点可以在Swift Xcode中每两秒跳一次?

时间:2022-02-10 05:18:05

I have this code where my node could jump every time I tap the screen. I want there to be like a two or three second wait before I could make the node jump again when I tap the screen. How would I do this? Thanks!

我有这个代码,每次点击屏幕我的节点都会跳转。我希望在我点击屏幕之前,我可以再次等待两到三秒钟。我该怎么做?谢谢!

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    var touch: UITouch = touches.first as! UITouch

    theHero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 250))
    theHero.texture = SKTexture(imageNamed: "jumpman")
    println("works")


 }

2 个解决方案

#1


Since you're using SpriteKit, the easiest way to handle this is by using the SKScene.update method. SpriteKit calls this method once per frame, if the scene is presented and not paused.

由于您使用的是SpriteKit,因此最简单的方法是使用SKScene.update方法。如果场景出现并且没有暂停,SpriteKit每帧调用一次此方法。

In touchesBegan:withEvent:, just set a flag indicating that a jump was requested. In update:, check whether the flag is set and sufficient time has elapsed since the last jump. If both are true, clear the flag, update the "last jump time" property, and jump.

在touchesBegan:withEvent:中,只需设置一个标志,指示已请求跳转。在更新:中,检查标志是否已设置,并且自上次跳转以来已经过了足够的时间。如果两者都为真,则清除标志,更新“最后跳转时间”属性,然后跳转。

class MyScene: SKScene {

    let theHero: SKSpriteNode = SKSpriteNode()
    var lastJumpTime: NSTimeInterval = 0
    var jumpIsPending: Bool = false
    static let JumpCooldownSeconds: NSTimeInterval = 2

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        jumpIsPending = true
    }

    override func update(currentTime: NSTimeInterval) {
        jumpIfNeeded(currentTime)
        // ... other per-frame stuff
    }

    private func jumpIfNeeded(currentTime: NSTimeInterval) {
        if jumpIsPending && lastJumpTime + MyScene.JumpCooldownSeconds <= currentTime {
            jumpIsPending = false
            lastJumpTime = currentTime

            theHero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 250))
            theHero.texture = SKTexture(imageNamed: "jumpman")
            println("works")
        }
    }

}

#2


This is what properties are for - to maintain state between invocations of methods. Each time you do jump, store the current time (the touch's timestamp) in a property. Now, the next time, look at the current time (the new touch's timestamp) and subtract the stored time. If it isn't more than 2 seconds, do nothing. If it is, store the new current time and jump.

这就是属性的用途 - 在方法调用之间维护状态。每次跳转时,将当前时间(触摸的时间戳)存储在属性中。现在,下一次,查看当前时间(新触摸的时间戳)并减去存储的时间。如果不超过2秒,则不执行任何操作。如果是,则存储新的当前时间并跳转。

#1


Since you're using SpriteKit, the easiest way to handle this is by using the SKScene.update method. SpriteKit calls this method once per frame, if the scene is presented and not paused.

由于您使用的是SpriteKit,因此最简单的方法是使用SKScene.update方法。如果场景出现并且没有暂停,SpriteKit每帧调用一次此方法。

In touchesBegan:withEvent:, just set a flag indicating that a jump was requested. In update:, check whether the flag is set and sufficient time has elapsed since the last jump. If both are true, clear the flag, update the "last jump time" property, and jump.

在touchesBegan:withEvent:中,只需设置一个标志,指示已请求跳转。在更新:中,检查标志是否已设置,并且自上次跳转以来已经过了足够的时间。如果两者都为真,则清除标志,更新“最后跳转时间”属性,然后跳转。

class MyScene: SKScene {

    let theHero: SKSpriteNode = SKSpriteNode()
    var lastJumpTime: NSTimeInterval = 0
    var jumpIsPending: Bool = false
    static let JumpCooldownSeconds: NSTimeInterval = 2

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        jumpIsPending = true
    }

    override func update(currentTime: NSTimeInterval) {
        jumpIfNeeded(currentTime)
        // ... other per-frame stuff
    }

    private func jumpIfNeeded(currentTime: NSTimeInterval) {
        if jumpIsPending && lastJumpTime + MyScene.JumpCooldownSeconds <= currentTime {
            jumpIsPending = false
            lastJumpTime = currentTime

            theHero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 250))
            theHero.texture = SKTexture(imageNamed: "jumpman")
            println("works")
        }
    }

}

#2


This is what properties are for - to maintain state between invocations of methods. Each time you do jump, store the current time (the touch's timestamp) in a property. Now, the next time, look at the current time (the new touch's timestamp) and subtract the stored time. If it isn't more than 2 seconds, do nothing. If it is, store the new current time and jump.

这就是属性的用途 - 在方法调用之间维护状态。每次跳转时,将当前时间(触摸的时间戳)存储在属性中。现在,下一次,查看当前时间(新触摸的时间戳)并减去存储的时间。如果不超过2秒,则不执行任何操作。如果是,则存储新的当前时间并跳转。