2013-04-24 75 views
0

在游戏中,我正在创造我只想让僵尸能够每分钟击中玩家2次,而不是带走洞穴健康栏,因为它会损害玩家的速度。为玩家设置无敌框架

public void checkCollision(){ 
    Rectangle r3 = player.getBounds(); 
    for(int i = 0; i < zombie.size(); i++){ 
     Zombie z = (Zombie) zombie.get(i); 
     Rectangle r2 = z.getBounds(); 
     if(r3.intersects(r2)){ 
      if(!player.getInvincibility()){ 
       player.setHealth(player.getHealth() - 10); 
       player.setInvincibility(true); 
      } 
     } 
    } 
} 

这是检查玩家和僵尸的碰撞的代码。我已经做到这样,玩家只会受到10点伤害,但是玩家永远不会再受到伤害。我曾尝试使用if语句来检查玩家是否无敌,并且在if语句中有一个for循环,当int达到30 000时会使玩家死亡,但僵尸仍然会对玩家造成如此之快的伤害健康酒吧的盖茨被带走。

回答

0

有一个被称为每帧的方法 - 称之为updateTimers或其他。该方法应该使玩家的invincibilityTimer减少一定数量。然后,如果玩家具有非零不败定时器,他们很容易受到checkCollission中的伤害,这也会将invincibilityTimer设置为一个设定的数字。

1

对僵尸使用攻击冷却时间。

在我的游戏我有类似

public boolean isReadyToAttack() { 
    boolean ret; 
    long delta = System.currentTimeMillis() - t0; 
    timer += delta; 
    if (timer > attackCooldown) { 
     timer = 0; 
     ret = true; 
    } else { 
     ret = false; 
    } 
    t0 = System.currentTimeMillis(); 
    return ret; 
} 

然后你只需在你的循环检查这一点,如果僵尸还没有准备好,他不会,即使他是接近攻击(其实这是更好检查碰撞前的冷却时间,它便宜)ø

0

我喜欢做一个报警类来处理诸如“等10帧,然后打印'Hello world!到控制台“:

public class Alarm { 
    //'timer' holds the frames left until the alarm goes off. 
    int timer; 
    //'is_started' is true if the alarm has ben set, false if not. 
    boolean is_started; 
    public Alarm() { 
     timer = 0; 
     is_started = false; 
    } 
    public void set(int frames) { 
     //sets the alarm to go off after the number of frames specified. 
     timer = frames; 
     is_started = true; 
    } 
    public void tick() { 
     //CALL THIS EVERY FRAME OR ELSE THE ALARM WILL NOT WORK! Decrements the timer by one if the alarm has started. 
     if (is_started) { 
      timer -= 1; 
     } 
    } 
    public void cancel() { 
     //Resets the frames until the alarm goes off to zero and turns is_started off 
     timer = 0; 
     is_started = false; 
    } 
    public boolean isGoingOff() { 
     //Call this to check if the alarm is going off. 
     if (timer == 0 && is_started == true) { 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
} 

你可以让一个无敌框架本身(假设玩家有一个报警叫invincibility_alarm并且它被设置为当一个僵尸击中玩家30帧):

//Pretend this is your gameloop: 
while (true) { 
    if (player.invincibility_alarm.isGoingOff()) { 
     player.setInvincibility(false); 
     player.invincibility_alarm.cancel(); 
    } 
    player.invincibility_alarm.tick(); 
    Thread.sleep(10); 
}