Search code examples
javaimagearraylistlogicside-scroller

Different image drawn for same object


I am having a problem with my images in my game. I am using an ImageLoader class which was written by my teacher. I create the two Image variables like this:

private static Image healthPic = ImageLoader.loadCompatibleImage("Health.gif");
private static Image enemyRocket = ImageLoader.loadCompatibleImage("EnemyShip2Rocket.png");;

To create the health powerup, I use an arraylist, and populate it using this code:

if (frameCount % 951 == 0){
        Random r = new Random();
        int randX = r.nextInt(width - 20)+10;
        healthP.add(new GameObject(randX, -400, 20,20,healthPic,8,0));
}

enemyWH is one variable used for both the width and height. It is equal to 35. I populate the enemy rocket arraylist using this code

if (frameCount % 300 == 0){
            for (int i = army.size()-1;i>=0;i--){
                EnemyShip curEnemy = army.get(i);
                rocketP.add(new GameObject(curEnemy.getR().x, curEnemy.getR().y, 20,20, enemyRocket,5,d));
    }
}

The 20 and 20 are the width and height, the x and y are where the enemy is at that exact location and time, enemyRocket is the Image variable, 5 is the ySpeed (how fast it moves down the screen), and d is the damage that it does to the playership. My problem is that as the game progresses, randomly, about a second or so after the enemies all fire their rockets, the picture variable that is associated with the rocket suddenly switches to the health powerup picture, so it seems like a health powerup is coming, but it is really an enemy rocket. The picture is switching from rocket to health powerup


Solution

  • Fixed me own problem by re-writing method and using a different object, not GameObject