您当前的位置: 首页 >  游戏

SpriteKit游戏开发 Challenge 2: An invincible zombie 问题的另一种解决方法

发布时间:2016-04-20 17:05:40 ,浏览量:0

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)

该挑战的目的是僵尸碰到敌人时,将其设置为无敌模式,具体要求如下:

• You should create a variable property to track whether or not the zombie is invincible. • If the zombie is invincible, you shouldn’t bother enumerating the scene’s cat ladies. • If the zombie collides with a cat lady, don’t remove the cat lady from the scene. Instead, set the zombie as invincible. Next, run a sequence of actions that first makes the zombie blink 10 times over three seconds, then runs the block of code described below. • Theblockofcodeshouldsethiddentofalseonthezombie,makingsurehe’s visible at the end no matter what, and set the zombie as no longer invincible.

书上的实现如下:

func zombieHitEnemy(enemy: SKSpriteNode) {
    enemy.removeFromParent()
    runAction(enemyCollisionSound)

    invincible = true let blinkTimes = 10.0 let duration = 3.0 let blinkAction = SKAction.customActionWithDuration(duration) { node, elapsedTime in let slice = duration / blinkTimes let remainder = Double(elapsedTime) % slice
      node.hidden = remainder > slice / 2 } let setHidden = SKAction.runBlock() {
      self.zombie.hidden = false self.invincible = false }
    zombie.runAction(SKAction.sequence([blinkAction, setHidden]))

  }

func checkCollisions(){ if invincible { return }

    var hitEnemies: [SKSpriteNode] = []
    enumerateChildNodesWithName("enemy") { node, _ in let enemy = node as! SKSpriteNode if CGRectIntersectsRect(
        CGRectInset(node.frame, 20, 20), self.zombie.frame) {
        hitEnemies.append(enemy)
      }
    } for enemy in hitEnemies {
      zombieHitEnemy(enemy)
    }
}

我觉得有几点可以完善的地方:

  1. 每次都需要新建blinkAction,有点浪费.可以使用共享Action
  2. 僵尸变为无敌后,如果敌人出现够快还是会删除其他敌人

我把blinkAction设置为lazy的计算属性,然后直接在场景子节点枚举中处理与敌人的碰撞,然后直接跳出遍历,这可以保证只会删除第一个碰撞的敌人:

lazy var blinkAction:SKAction = { let blinkTimes = 10.0 let duration = 3.0 let blinkAction = SKAction.customActionWithDuration(duration){node,elapsedTime in let slice = duration/blinkTimes let remainder = Double(elapsedTime) % slice
            node.hidden = remainder > slice / 2 } return blinkAction
    }()

func checkCollisions(){ if zombieInvincible{ return } var hitEnemies:[SKSpriteNode] = []
        enumerateChildNodesWithName("enemy"){node,stop in let enemy = node as! SKSpriteNode if CGRectIntersectsRect(CGRectInset(enemy.frame, 20, 20), self.zombie.frame){
                hitEnemies.append(enemy)
                self.zombieInvincible = true let seq = SKAction.sequence([self.blinkAction,SKAction.runBlock(){
                        self.zombie.hidden = false self.zombieInvincible = false }])
                self.zombie.runAction(seq)
                stop.memory = true }
        } for enemy in hitEnemies{
            zombieHitEnemy(enemy)
        }
    }
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    108697博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0487s