gpt4 book ai didi

ios - 为 SpriteKit 中的函数添加延迟

转载 作者:搜寻专家 更新时间:2023-10-31 22:28:55 26 4
gpt4 key购买 nike

我希望只允许玩家每 0.7 秒发射一枚导弹。我该怎么做呢?这是我的代码。我已经尝试过我在这个网站上找到的其他方法,但它们不起作用。

func fireTorpedo() {
if !self.player.isPaused
{
if isSoundEffect == true {
self.run(SKAction.playSoundFileNamed("Rocket", waitForCompletion: false))
}

let torpedoNode = SKSpriteNode(imageNamed: "Rocket")

torpedoNode.position = player.position
torpedoNode.position.y += 5
torpedoNode.physicsBody = SKPhysicsBody(circleOfRadius: torpedoNode.size.width / 2)
torpedoNode.physicsBody?.isDynamic = true
torpedoNode.physicsBody?.categoryBitMask = photonTorpedoCategory
torpedoNode.physicsBody?.contactTestBitMask = alienCategory
torpedoNode.physicsBody?.collisionBitMask = 0
torpedoNode.physicsBody?.usesPreciseCollisionDetection = true

self.addChild(torpedoNode)

let animationDuration:TimeInterval = 0.5
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: player.position.x, y: self.frame.size.height + 10), duration: animationDuration))
actionArray.append(SKAction.removeFromParent())
torpedoNode.run(SKAction.sequence(actionArray))
}
}

最佳答案

我喜欢这样做:

class GameScene:SKScene {

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)

if action(forKey: "shooting") == nil {

let wait = SKAction.wait(forDuration: 0.7)
run(wait, withKey: "shooting")

print("shot fired")
}
}
}

虽然在某个场景中找到了“射击”键,但您不能再射击了。在真实游戏中,这可能是节点的一部分(一个 Action 应该在节点上运行)。

关于ios - 为 SpriteKit 中的函数添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41881763/

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