gpt4 book ai didi

Swift 定时器检查 TouchesEnded

转载 作者:可可西里 更新时间:2023-11-01 01:25:13 25 4
gpt4 key购买 nike

很抱歉让我感到尴尬,但请问我能否获得有关如何执行此操作的完整代码:

我希望在我的游戏中每发射一颗子弹后延迟 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com