Java 我的滴答法搞砸了
我创建了一个名为子弹的类:Java 我的滴答法搞砸了,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我创建了一个名为子弹的类: 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
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);
}
}
bulletQualities.tick();
}
我得到的确切错误是:
线程“thread-4”java.lang.IndexOutOfBoundsException中的异常:索引:1,大小:1
位于java.util.LinkedList.checkElementIndex(未知源)
位于java.util.LinkedList.get(未知源)
在mainPackage.BulletQualities.tick(BulletQualities.java:15)处
在mainPackage.Game.tick(Game.java:105)
运行mainPackage.Game.run(Game.java:78)
在java.lang.Thread.run(未知源)中,您只需要迭代列表,直到索引大小为1。因此,改变这一点:
for(int x = 0;x <= bulletList.size();x++){
非常感谢你!我真的很感激@如果答案对你有帮助,那么你可以接受答案。(单击时变为绿色的右勾号),因为它可以帮助其他人在将来面临相同的问题
for(int x = 0;x <= bulletList.size();x++){
for(int x = 0;x < bulletList.size();x++){
for(Bullets bullet:bulletList){
bullet.tick();
}