简体   繁体   English

如何延迟每发子弹? (Java -Slick2d-游戏)

[英]How can I delay each bullet? (Java -Slick2d - Game)

(The event listener is in another class) When Game.render = true, i get a constant stream of bullets that look more like a laser-beam, i want there to be gaps. (事件侦听器在另一个类中)当Game.render = true时,我会得到源源不断的子弹,这些子弹看起来更像激光束,我希望两者之间存在间隙。 What I mean is that I would like the bullets to be generated as if they were being fired from a machine gun. 我的意思是,我希望生成子弹,就像从机枪发射子弹一样。 I know i should add a time or something, but I'm not sure how to do that, to get the effect I want. 我知道我应该添加一些时间,但是我不确定该怎么做才能获得想要的效果。 Any help would be greatly appreciated because I've been trying to get this to work for about an hour now. 任何帮助将不胜感激,因为我一直在努力使其工作约一个小时。

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++;

        }
    }
}

What I do most frequently in this kind of situation is add a time variable that I subtract the delta (the variable you have as u) from every update until it goes below 0, at which point I reset it: 在这种情况下,我最常做的事情是添加一个时间变量,该变量会从每次更新中减去增量(您具有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...
}

Basically, even if Game.fire is true the bullet won't fire until the timer counts down. 基本上,即使Game.fire是true,在计时器倒数之前,子弹也不会发射。 Once that happens, the timer is set and the next bullet can't fire until the timer counts down again. 一旦发生这种情况,便会设置计时器,直到下一次计时器倒数,下一颗子弹才能发射。 If you set this to something reasonably small then there should be a bit of a gap between each bullet. 如果将其设置为合理的较小值,则每个项目符号之间应存在一定的距离。

This is how I did this: 这就是我这样做的方式:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM