2013-04-07 18 views
0

(事件監聽器在另一個類中)當Game.render = true時,我得到一串不斷流出的子彈,看起來更像激光束,我希望有空隙。我的意思是,我希望子彈能夠像從機關槍發射一樣產生。我知道我應該增加一些時間或某些東西,但我不知道如何去做,以獲得我想要的效果。任何幫助將不勝感激,因爲我一直試圖讓這個工作大約一個小時。我該如何推遲每一顆子彈? (Java -Slick2d - Game)

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

我做的最常見的這種情況是添加一個時間變量,我從每次更新減去增量(變量,你必須爲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是真的子彈不會開火,直到計時器倒計時。一旦發生這種情況,定時器就會被設置,並且下一顆子彈不會被點燃,直到定時器再次倒計時。如果你把它設置得相當小,那麼每個子彈之間應該有一點差距。

+1

這是應該究竟如何做。 – 2013-04-15 11:13:50

+1

我會把它改成'time + = default_bullet_delay',這樣火焰的速度就是恆定的。 – 2013-09-14 03:45:49

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