Why are didBeginContact called multiple times?

前端 未结 6 1182
孤独总比滥情好
孤独总比滥情好 2020-11-27 22:18

In an iOS game that uses Sprite Kit along with the contact detection in Sprite Kit\'s build-in physics engine, I decrease the Hero\'s number lives by one each time he gets i

6条回答
  •  不知归路
    2020-11-27 23:03

    Here is an option that makes the player invulnerable after being hit for a set time:

    A. Create a variable that makes the player invulnerable to losing a life after being hit for a few seconds.

    1. Create a global Boolean variable called isInvuln (set to FALSE) and an NSTimeInterval called invulnTime.
    2. In the method that handles the player and enemy making contact, check to see if isInvuln is False before taking a life. (if isInvuln is true ... do nothing)
    3. If isInvuln is false, take a life then set isInvuln to true.

       if(self.isInvuln == FALSE){
            self.player.lives-=1;
            self.isInvuln = True;}
      
    4. Add to your updateWithCurrentTime:

       if(self.isInvuln==True){
       self.invulnTime += timeSinceLast;}
      
       if (self.invulnTime > 3) {             
           self.isInvuln = FALSE:}
           self.invulnTime= 0;
      

    This will make it so that when an enemy and player collide, the player loses a life and becomes invulnerable 3 seconds. After that 3 seconds, the player can take damage again. If the enemy contacts the player within the 3 invulnerable seconds, the contact method does nothing. Hope this helps spark ideas to tackle your problem.

提交回复
热议问题