0

我正在创建一个让玩家使用通电的游戏。每当玩家选择激活电源时,此功能就会运行。它使用道具将玩家的图像更改为玩家的图像,并更改碰撞物理,以便玩家现在对敌人免疫。

只要变量 powerActivated 为 1,它就会执行此操作,但是正如您所见,它直接回到 0。我需要它延迟 5-10 秒,然后转到 0。这将允许用户使用 powerUp 几个在它消失之前的几秒钟。

 func superAbility(){

        powerActivated = 1

        if powerActivated == 1 {

        player.texture = SKTexture(imageNamed: "heroWithPower")
        player.physicsBody!.categoryBitMask = PhysicsCategories.PowerUp
        player.physicsBody!.collisionBitMask = PhysicsCategories.None
        player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy

        // delay should be here

        powerActivated = 0

        }

        else {
            player.texture = SKTexture(imageNamed: "hero")
            player.physicsBody!.categoryBitMask = PhysicsCategories.Player
            player.physicsBody!.collisionBitMask = PhysicsCategories.None
            player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy

        }
4

1 回答 1

2

使用 SKAction 来创建延迟,这样您在游戏时间而不是实时等待,因此任何外部电话操作(如电话)都不会破坏您的游戏。

func superAbility(){

        player.texture = SKTexture(imageNamed: "heroWithPower")
        player.physicsBody!.categoryBitMask = PhysicsCategories.PowerUp
        player.physicsBody!.collisionBitMask = PhysicsCategories.None
        player.physicsBody!.contactTestBitMask = PhysicsCategories.None //I think you meant to set this to none to be immune to enemies

        let deactivateAction = SKAction.run{
            [unowned self] in
            self.player.texture = SKTexture(imageNamed: "hero")
            self.player.physicsBody!.categoryBitMask = PhysicsCategories.Player
            self.player.physicsBody!.collisionBitMask = PhysicsCategories.None
            self.player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
        }
        let wait = SKAction.wait(forDuration:5)
        let seq = SKAction.sequence([wait,deactivateAction])
        player.run(seq, withKey:"powerup")
}
于 2019-04-15T16:55:30.650 回答