首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SpriteKit中更改变量之前创建延迟

如何在SpriteKit中更改变量之前创建延迟
EN

Stack Overflow用户
提问于 2019-04-15 03:22:59
回答 1查看 62关注 0票数 0

我正在创建一个涉及玩家使用加电的游戏。每当播放器选择激活通电时,都会运行此功能。它将玩家的图像改变为使用加电的玩家的图像,并改变碰撞物理,使玩家现在对敌人免疫。

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

代码语言:javascript
复制
 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

        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-16 00:55:31

使用SKAction来创建延迟,这样你就可以在游戏时间内等待,而不是实时等待,这样任何外部电话操作都不会破坏你的游戏。

代码语言:javascript
复制
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")
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55679176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档