简体   繁体   中英

My tick method is messed up

I created a class called Bullets:

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

Then I created a class called 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++){

I get an error on this line

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

Then, in my Game class I did this:

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();

I get an error on this line

    bulletQualities.tick();
}

The exact error I get is this:

Exception in thread "Thread-4" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.LinkedList.checkElementIndex(Unknown Source) at java.util.LinkedList.get(Unknown Source) at mainPackage.BulletQualities.tick(BulletQualities.java:15) at mainPackage.Game.tick(Game.java:105) at mainPackage.Game.run(Game.java:78) at java.lang.Thread.run(Unknown Source)

You need to iterate the list only upto size-1 index. So change this:

for(int x = 0;x <= bulletList.size();x++){

to

for(int x = 0;x < bulletList.size();x++){

List internally stores element in an array and an array contains element from 0th index to upto its size-1 index. Anytime you will try to get bulletList.get(x) where x= size of list, it will throw an IndexOutOfBoundsException

There are better way to iterate list in Java verion 5.0 and abaove. Using enhanced for loop, you will never run into such situation. Here is how you can transform your code using enhanced for loop:

for(Bullets bullet:bulletList){
    bullet.tick();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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