【发布时间】:2017-01-24 20:12:33
【问题描述】:
在我的游戏中,你点击屏幕上的任意位置,子弹就会朝那个方向飞去。唯一的问题是你可以尽可能快地拍摄。有什么办法可以在每次拍摄后添加延迟。所以我想拍,等1秒再拍。这是我在 touchesEnded 中的代码:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
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.0)
let actionMoveDone = SKAction.removeFromParent()
bullet.run(SKAction.sequence([actionMove, actionMoveDone]))
}
}
感谢您的帮助。
【问题讨论】:
-
发射子弹时启动计时器。在下一次触摸时,检查计时器是否已经过了一秒。如果有,拍摄并重新启动计时器,否则忽略。
-
这很简单,可以通过多种方式完成。一种方法是使用键运行操作。所以,你发射一颗子弹,然后对一个玩家执行一个持续一秒钟的动作,这意味着你的射击被锁定。因此,每次您尝试发射子弹时,您都会检查此操作键是否存在于特定节点上。
-
我有机会获得这两种方法的代码吗?对不起,这里是初学者
标签: swift sprite-kit delay projectile