【问题标题】:Setting invincibility frame for the player为玩家设置无敌帧
【发布时间】:2013-04-24 15:01:52
【问题描述】:

在我正在创造的游戏中,我只希望僵尸能够每分钟击中玩家 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 时会使玩家死亡,但僵尸仍然会如此快速地伤害玩家,以至于洞健康酒吧盖茨被带走。

【问题讨论】:

    标签: java if-statement for-loop


    【解决方案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;
    }
    

    然后你只需在你的循环中检查这个,如果僵尸还没有准备好攻击,即使他很近也不会攻击(实际上最好在碰撞前检查冷却时间,这样更便宜)ø

    【讨论】:

      【解决方案2】:

      有一个在每一帧都被调用的方法——称之为 updateTimers 或其他什么。此方法应将玩家的 invincibilityTimer 递减一定数量。然后,如果玩家的 invincibilityTimer 不为零,他们很容易在 checkCollission 中受到伤害,这也会将 invincibilityTimer 设置为一个设定的数字。

      【讨论】:

        【解决方案3】:

        我喜欢创建一个警报类来处理诸如“等待 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);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-05
          • 2015-07-03
          相关资源
          最近更新 更多