Swift 计时器检查 TouchesEnded

标签 swift timer sprite-kit

很抱歉有点尴尬,但是请给我完整的代码来说明如何做到这一点:

我希望在游戏中每发射一颗子弹后延迟 1 秒,以防止垃圾邮件。如果可能的话,无需为子弹生成创建单独的函数,就像我在touchesEnded中所做的那样。所以点击、射击、等待。点击、射击、等待。在等待过程中,如果点击屏幕,则不会发生任何事情。谢谢,抱歉我是初学者

guard let touch = touches.first else {
        return
    }
    let touchLocation = touch.location(in: self)

    //Set up initial location of bullet and properties
    let bullet = SKSpriteNode(imageNamed: "bullet")
    bullet.name = "Bullet"
    bullet.position = player.position
    bullet.setScale(0.75)
    bullet.zPosition = 1
    bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.size.width/2)
    bullet.physicsBody?.isDynamic = true
    bullet.physicsBody?.categoryBitMask = PhysicsCategory.Projectile
    bullet.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
    bullet.physicsBody?.collisionBitMask = PhysicsCategory.None
    bullet.physicsBody?.usesPreciseCollisionDetection = true



    //Determine offset of location to bullet
    let offset = touchLocation - bullet.position

    //Stops Bullet from shooting backwards
    if (offset.y < 0) { return }

    addChild(bullet)

    //Get the direction of where to shoot
    let direction = offset.normalized()

    //Make it shoot far enough to be guaranteed off screen
    let shootAmount = direction * 1000

    //Add the shoot amount to the current position
    let realDest = shootAmount + bullet.position

    //Create the actions

    if currentGameState == gameState.inGame {
    let actionMove = SKAction.move(to: realDest, duration: 1.2)
    let actionMoveDone = SKAction.removeFromParent()
    bullet.run(SKAction.sequence([actionMove, actionMoveDone]))
    }

}

最佳答案

试试这个:

var fired = false

// Other code...

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    let touchLocation = touch.location(in: self)

    if fired == false {

        fired = true

        //Set up initial location of bullet and properties
        let bullet = SKSpriteNode(imageNamed: "bullet")
        bullet.name = "Bullet"
        bullet.position = player.position
        bullet.setScale(0.75)
        bullet.zPosition = 1
        bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.size.width/2)
        bullet.physicsBody?.isDynamic = true
        bullet.physicsBody?.categoryBitMask = PhysicsCategory.Projectile
        bullet.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
        bullet.physicsBody?.collisionBitMask = PhysicsCategory.None
        bullet.physicsBody?.usesPreciseCollisionDetection = true



        //Determine offset of location to bullet
        let offset = touchLocation - bullet.position

        //Stops Bullet from shooting backwards
        if (offset.y < 0) { return }

        addChild(bullet)

        //Get the direction of where to shoot
        let direction = offset.normalized()

        //Make it shoot far enough to be guaranteed off screen
        let shootAmount = direction * 1000

        //Add the shoot amount to the current position
        let realDest = shootAmount + bullet.position

        //Create the actions

        if currentGameState == gameState.inGame {
            let actionMove = SKAction.move(to: realDest, duration: 1.2)
            let actionMoveDone = SKAction.removeFromParent()
            bullet.run(SKAction.sequence([actionMove, actionMoveDone]))

        }

        run(SKAction.wait(forDuration: 1), completion: { fired = false })

    }
}

这意味着每次发射子弹时,都会出现 1 秒的计时器,以防止子弹代码在时间结束之前运行。此时, bool 值切换回 false,允许代码再次运行。

关于Swift 计时器检查 TouchesEnded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41881990/

相关文章:

ios - 通过点击触摸吸烟 Sprite 而不是快速滑动

ios - SpriteKit : Getting Audio to stop playing

java - 如何将 Action 监听器应用到我的计时器?

swift - sceneDidLoad 运行两次

swift - 使用 swift 项目 19 进行黑客攻击时,我不断收到 fatal error : unexpectedly found nil while unwrapping an Optional value

swift - 删除 UIView 后重置其他组件的约束

javascript - 重新实例化 AngularJS 计时器

java - 如何创建一个在不同时间重复的 Java 计时器

ios7 - 我如何从 UIViewController 移动到 myScene?

ios - 在 IOS Swift 中,如何使白色文本在任何背景上都可见,就像在 snapchat 故事中一样