在SpriteKit中为函数添加延迟

时间:2017-01-26 19:50:38

标签: ios swift sprite-kit

我希望能够让玩家每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))
    }
}

3 个答案:

答案 0 :(得分:1)

首先,在班级添加一个标志,指示玩家是否可以发射鱼雷。

var canFireTorpedo = true

然后,在fireTorpedo方法结束时,将canFireTorpedo设置为false,然后在0.7秒后再次将其设置为true:

canFireTorpedo = false
player.run(SKAction.sequence([
    SKAction.wait(forDuration: 0.7), 
    SKAction.run { [weak self] in self?.canFireTorpedo = true }]))

之后,在方法开头检查canFireTorpedo

func fireTorpedo() {
    if canFireTorpedo {
        // put everything in the method here, including the parts I added
    }
}

希望这段代码不言自明。

答案 1 :(得分:1)

每0.7秒启动

let actionKey = "repeatLaunchMethod"

let launchMethod = SKAction.afterDelay(0.7, runBlock: {
       [weak self] in
       guard let strongSelf = self else { return }
       strongSelf.fireTorpedo()
})
// do this if you want to repeat this action forever
self.player.run(SKAction.repeatForever(launchMethod), withKey: actionKey)
// do this if you want to repeat this action only n times
// self.player.run(SKAction.repeat(launchMethod, count: 5), withKey: actionKey)

停止此操作,您可以执行以下操作:

if self.player.action(forKey: actionKey) != nil {
       self.player.removeAction(forKey: actionKey)
}

扩展程序用于呈现代码更具可读性:

extension SKAction {
    /**
     * Performs an action after the specified delay.
     */
    class func afterDelay(_ delay: TimeInterval, performAction action: SKAction) -> SKAction {
        return SKAction.sequence([SKAction.wait(forDuration: delay), action])
    }
    /**
     * Performs a block after the specified delay.
     */
    class func afterDelay(_ delay: TimeInterval, runBlock block: @escaping () -> Void) -> SKAction {
        return SKAction.afterDelay(delay, performAction: SKAction.run(block))
    }
}

答案 2 :(得分:1)

我喜欢这样做:

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")
        }
    }
}

虽然在场景中发现了一个关键的“射击”,但你不能再射击了。在真实游戏中,这可能是节点的一部分(应该在节点上运行一个动作)。