0

I am in my second year of programming, and only know Java. However for my class, we have to create a vertical side-scroller. My set-up is that I have an ArrayList for enemy ships, and an ArrayList for player rockets. I draw the 2 rockets in precise spots on the player ship in the render(Graphics2D g) method, and the enemy ships in the same method. The rocket ArrayList is populated by

arsenal.add(r);
arsenal.add(r2);

in which r and r2 are rockets with the parameters (x,y,w,h), and arsenal is the rocket arraylist. I have set them to be drawn in very specific places, at the different ends of the spaceship. In the update() method, I have this method:

if (arsenal.size() < 0){
        arsenal.add(new Rocket(p.currentX(), p.getHalfY(),30,30));
        arsenal.add(new Rocket(p.getHalfX(), p.getHalfY(), 30, 30));
    }

This is to make sure that when the arraylist gets empty, the rockets are re-added and redrawn to the coordinates that correspond to the ends of the spaceship. The only way the rockets disappear is when they go off-screen, or collide with the enemy. I have put this code in to check for that:

for(int x = army.size()-1; x >= 0; x--){
        if (army.get(x).getR().y > 1024){
            army.remove(x);
        }
        if(p.intersects(army.get(x).getR())){
            army.remove(x);
            p.changeH(10);
        }
        for (int q = arsenal.size()-1; q>=0;q++){
            if (arsenal.get(q).intersects(army.get(x).getR())){
                score+=20;
                army.remove(x);
                arsenal.remove(q);
            }
        }
}

This whole method goes through the enemy ship arraylist "army" and checks to see if (1) the enemy ship has gone past the set height, (2) if the player ship collides with the enemy ship, and (3) if a rocket in the arsenal arraylist has collided with an enemyship. All collisions are done using rectangles. The p.changeH(10); command is to lower the player's health by 10, and the score +=20; is to increase the score. For all these checks, I make sure that the enemyship removes itself from the arraylist and thus becomes undrawn, and then if the rockets go off-screen or collide, they also remove themselves from the arraylist and hypothetically undraw themselves.Last thing I want to mention is that the enemyship arralust becomes populated by:

if (frameCount % 100 == 0){
            Random r = new Random();
            int randX = r.nextInt(width - 10);
            army.add(new EnemyShip(randX, -200 , 47, 47, enemyP1));
}

It takes in the parameters (x,y,w,h,Image) and is drawn off-screen first to make it seem like a side-scroller. The x variable is random, so that the ships do not all appear in the same place. frameCount is incremented by 1 every update, and the update() method is called 60 times a second.

After this huge introduction to my project, my problem is that whenever I run the game, the game just stops after a couple seconds, and eclipse gives me an error at the code:

if (arsenal.get(q).intersects(army.get(x).getR())){

I cannot figure out the problem. Any help is greatly appreciated. If more of the game code is needed, just let me know! Thanks!

EDIT:

GameScreen.update(GameScreen.java:108)

takes me to the line:

 if (arsenal.get(q).intersects(army.get(x).getR())){

which is part of the loop:

for (int q = arsenal.size()-1; q>=0;q++){
            if (arsenal.get(q).intersects(army.get(x).getR())){
                score+=20;
                army.remove(x);
                arsenal.remove(q);
            }
}

In the constructor is where I place the commands

arsenal.add(r);
arsenal.add(r2);

This adds them to the arraylist. In the render(Graphics2D g) method, I draw them using:

for (Rocket r :arsenal){
            r.draw(g);
}

In the rocket constructor, p.currentX() is the player's current x coordinate, and p.getHalfY() and p.getHalfX() are self explanatory. They are coded so that the rocket r appears on one side, and rocket r2 is on the other side. I have also created two boolean variables, rMove and r2move. These aid in the smooth movement of the rocket, and also tell it when to move:

if (!r2move){
        r2.setP(p.getR().x + 40, currentY + 12);
}
    if(!rMove){
        r.setP(p.currentX() - 10, currentY + 12);
}

setP(x,y) sets the rocket's position to the original place. I have talked to my teacher about this, and he said that I am sort of using both arraylists and separate objects, however I do not understand the problem. If you need any more relevant code, let me know. This line of code also tells the rocket when to move, and that would be when the space button is fired:

if (rMove && r2move){
    for (Rocket p:arsenal){
        p.move();
    }
}
if(r.getR().y < 0){     
        rMove = false;
        space = false;
}
if (r2.getR().y < 0){
        r2move = false;
        space = false;
}
archangel
  • 112
  • 9
  • 2
    What error does Eclipse show you? – Tanuj Mathur Sep 04 '14 at 00:24
  • Exception in thread "Thread-5" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at GameScreen.update(GameScreen.java:108) – archangel Sep 04 '14 at 00:39
  • So for a start we need to see `GameScreen.update(GameScreen.java:108) ` plus any other relevant code. – Scary Wombat Sep 04 '14 at 00:54

1 Answers1

1

What the exception is telling you is that it has an ArrayList with 2 elements in it, which means its valid indexes are 0 and 1. But your code is calling the .get() method on this ArrayList with an index value of 2, which obviously doesn't exist and results in this exception.

Looking at the code, the problem seems to be with how you are initializing and using the loop variable q in the following lines:

for (int q = arsenal.size()-1; q>=0;q++){
            if (arsenal.get(q).intersects(army.get(x).getR()))

Let's assume arsenal has 2 elements in it and hence a size of 2. As a result, q will get initialized with the value 1. The if statement will be invoked, and arsenal.get(1) will be called. This is a valid index for the arsenal Arraylist, and so this code will pass without any error.

In the next pass of the loop, q will be incremented by 1 and will become 2. As per your loop check condition, 2 > 0, and execution will continue to the next line. The if statement will be invoked again, and arsenal.get(2) will be called. Since 2 is an invalid index for arsenal which has only 2 items, the exception you see will be thrown.

You can fix this issue by decrementing the value of q instead of incrementing it in the for loop.

Tanuj Mathur
  • 1,408
  • 10
  • 20