Swift Timer检查TouchesEnded

时间:2017-01-26 20:04:51

标签: 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]))
    }

}

1 个答案:

答案 0 :(得分:2)

试试这个:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private my: string) {}
} 

这意味着每次发射子弹时,会发生1秒的计时器,这会阻止子弹代码在时间结束前运行。此时,布尔值切换回false,允许再次运行代码。