0

(事件监听器在另一个类中)当 Game.render = true 时,我得到一个看起来更像激光束的持续子弹流,我希望有间隙。我的意思是,我希望生成的子弹就像是用机关枪发射的一样。我知道我应该添加一个时间或其他东西,但我不知道如何做到这一点,以获得我想要的效果。任何帮助将不胜感激,因为我一直试图让它工作大约一个小时。

import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class Bullet {

// ArrayList
@SuppressWarnings("unchecked")
static ArrayList<Bullet> arrL = new ArrayList();

public Bullet(int x , int y) throws SlickException{



}

public static void update(GameContainer gc, int u) throws SlickException{

// when the left mouse button is clicked Game.fire = true, the key listener is in   another class
    if(Game.fire){

        Bullet b = new Bullet(5,5);
        arrL.add(b);
        reloaded = false;

    }   if(!reloaded){



    }           
}

public static void renderBullets(GameContainer gc, Graphics g, int x, int y) {

         // draws a new bullet for every 'bullet object' in  the ArrayList called arrL
        for(Bullet b : arrL){

            g.drawRect(x,y,10,10);
            x++;

        }
    }
}
4

2 回答 2

4

在这种情况下,我最常做的是添加一个时间变量,我从每次更新中减去 delta(你作为 u 的变量),直到它低于 0,此时我将其重置:

// Set this to whatever feels right for the effect you're trying to achieve.
// A larger number will mean a longer delay.
private static int default_bullet_delay = 500;

private static int time = 0;

public static void update (GameContainer gc, int u) throws SlickException {
    time -= u;
    if (time <= 0 && Game.fire) {
        fireBullet();                 // Replace this with your code for firing the bullet
        time = default_bullet_delay;  // Reset the timer
    }

    // The rest of the update loop...
}

基本上,即使 Game.fire 为真,子弹也不会在计时器倒计时之前发射。一旦发生这种情况,就会设置计时器,并且在计时器再次倒计时之前下一个子弹无法发射。如果将其设置为相当小的值,那么每个子弹之间应该有一点间隙。

于 2013-04-13T00:18:53.150 回答
0

我就是这样做的:

boolean reloaded = false;  // Start with a reload becouse weapon not reload itself
float reloadTime = 100;     // This is the "timer" (the value doesnt matter)
float startReloadTime = 100; // This is the actual reload time

private void shoot(int delta, float screenWidth, float screenHeight) {

    // reload if not reloaded
    if (!reloaded) {
        reloadTime -= 0.5f * delta; // As I said, this is the timer

        // If the reload finished, set the timer back to the reload time
        if (reloadTime <= 0) {
            reloaded = true; // reloaded, we can shoot
            reloadTime = startReloadTime;
        }
    }

    // Shoot only if reloaded
    if (reloaded) {

        // This is some direction calculating, ignore for now
        float mouseWorldX = (x + (mouseX - screenWidth / 2));
        float mouseWorldY = (y + (mouseY - screenHeight / 2));

        float randomX = (float) Math.random() * (2000 / mouseWorldX);
        float randomY = (float) Math.random() * (2000 / mouseWorldY);

        mouseWorldX += randomX;
        mouseWorldY += randomY;

        // Add a new bullet element to the world (where will be rendered)
        world.add(new Shot(world, camera, x + width / 2, y + height / 2, mouseWorldX - 2.5f, mouseWorldY - 2.5f, 5, 5, new Color(1, 0, 0, 1f)));

        // We need to reload
        reloaded = false;
    }
}
于 2013-04-14T13:20:15.917 回答