我该怎样延迟每个子弹? (Java -Slick2d - 游戏)

时间:2013-04-07 07:32:41

标签: java slick2d

(事件监听器在另一个类中)当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++;

        }
    }
}

2 个答案:

答案 0 :(得分:4)

在这种情况下我最常做的是添加一个时间变量,我从每次更新中减去delta(你拥有的变量),直到它低于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为真,子弹也不会在计时器倒计时之前发射。一旦发生这种情况,计时器就会被设置,并且下一个项目符号无法启动,直到计时器再次倒计时。如果你把它设置得相当小,那么每个子弹之间应该有一点差距。

答案 1 :(得分: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;
    }
}