我創建了一個類叫子彈:我剔方法搞砸
public class Bullets {
private double x;
private double y;
private BufferedImage bulletImage;
Bullets(double x, double y){
this.x = x;
this.y = y;
ImageLoader loader = new ImageLoader();
SpriteSheet ss = new SpriteSheet(loader.loadImage("/Pics/TheSpriteSheet.png"));
bulletImage = ss.grabImage(2, 1, 32, 32);
}
public void render(Graphics g){
g.drawImage(bulletImage, (int)x, (int)y, null);
}
public void tick(){
y--;
}
}
然後,我創建了一個類名爲BulletQualities:
package mainPackage;
import java.awt.Graphics;
import java.util.LinkedList;
public class BulletQualities {
Bullets b;
private LinkedList<Bullets> bulletList = new LinkedList<Bullets>();
public void addBullet(Bullets b){
bulletList.add(b);
}
public void tick(){
for(int x = 0;x <= bulletList.size();x++){
我得到這一行的錯誤
bulletList.get(x).tick();
}
}
public void render(Graphics g){
for(int x = 0;x <= bulletList.size(); x++){
bulletList.get(x).render(g);
}
}
public void removeBullet(Bullets bullet){
bulletList.remove(bullet);
}
}
然後,在我的遊戲課上我做了這個:
Bullets b = new Bullets(playerClass.getX(), playerClass.getY());
BulletQualities bulletQualities = new BulletQualities();
public void init(){
bulletQualities.addBullet(b);
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
////////////////////////////////////////////
g.drawImage(background, 0, 0, this);
playerClass.render(g);
bulletQualities.render(g);
////////////////////////////////////////////
g.dispose();
bs.show();
}
public void tick(){
playerClass.tick();
我得到一個錯誤,在這條線
bulletQualities.tick();
}
確切的錯誤我得到的是這樣的:在線程
例外 「線程4」 java.lang.IndexOutOfBoundsException:指標:1,尺寸: (未知源) at mainPackage.BulletQualities.tick(BulletQualities.java:15) at mainPackage.Game.tick(Game .java:105) at mainPackage.Game.run(Game.java:7 8) at java.lang.Thread.run(未知來源)
非常感謝!對此,我真的非常感激! –
@LLLobster: - 如果答案對你有幫助,那麼你可以接受答案。 (點擊時右邊的刻度線變成綠色),因爲它可以幫助其他人在將來面對同樣的問題 –