Search code examples
javaif-statementrandombooleanbufferedimage

elsif with nextboolean for pictures


i am making a game with cactus pictures but when i do my code if its trying to switch between pictures it will stops and will freeze on that point the else if it does not work i also use bufferedimage for the pictures my code:

   package objectgame;

import java.awt.Graphics; 
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import util.Resource;

public class EnemiesManager {
    private List<Enemy> enemies;
    private Random random;
    
    private BufferedImage imagecactus1, imagecactus2, imagecactus3 ;
    
    public EnemiesManager() {
        enemies = new ArrayList<Enemy>();
        imagecactus1 = Resource.getResourceImage("data/cactus1.png");
        imagecactus2 = Resource.getResourceImage("data/cactus2.png");
        imagecactus3 = Resource.getResourceImage("data/cactus3.png");
        random = new Random();
        
        enemies.add(getRandomCactus());
        random = new Random();
    }
    
    public void update() {
        for(Enemy e: enemies) {
            e.update();
        }
        Enemy firstEnemy = enemies.get(0);
        if(firstEnemy.isOutOfScreen()) {
            enemies.remove(firstEnemy);
            enemies.add(getRandomCactus());
        }
    }
    
    public void draw(Graphics g) {
        for(Enemy e: enemies) {
            e.draw(g);
        }
    }
    private Cactus getRandomCactus() {
        Cactus cactus;
        cactus = new Cactus();
        cactus.setX(600);
        if(random.nextBoolean()) {
            cactus.setImage(imagecactus1);
        } 
        else{
            cactus.setImage(imagecactus2);
    }
        return cactus;
    }
}

new class

package objectgame;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import util.Resource;

public class Cactus extends Enemy{
    
    private BufferedImage image;
    private int posX, posY;
    private Rectangle rect;
    
    public Cactus() {
        image = Resource.getResourceImage("data/cactus1.png");
        posX = 200;
        posY = 83;
        rect = new Rectangle();
    }
    
    public void update() {
        posX -= 3;
        rect.x = posX;
        rect.y = posY;
        rect.width = image.getWidth();
        rect.height = image.getHeight();
    }
    @Override
    public Rectangle getBound() {
        return rect;
    }
    @Override
    public void draw(Graphics g) {
        g.drawImage(image, posX, posY, null);
    }
    
    public void setX(int x) {
        posX = x;
    }
    public void setY(int y) {
        posY = y;
    }
    public void setImage(BufferedImage images) {
        this.image = images;
    }
    @Override
    public boolean isOutOfScreen() {
        return (posX + image.getWidth() < 0);
    }
}

what is the problem this is the code from the anamation manager and the details from the picture. I have tried many tutorials but none where working correct always slightly off the purpose for the random bool. I use eclipse so I don't get any errors specific to the location where the error happens. I also did use debugger but non are working. So I really need your guys help.


Solution

  • A clean approach is as follows:

    private Cactus getRandomCactus() {
        Cactus cactus = new Cactus();
        cactus.setX(600);
        BufferedImage [] images = {imagecactus1, imagecactus2, imagecactus3};
        cactus.setImage(images[random.nextInt(3)]);
        return cactus;
    }
    

    Note: random.nextInt(3) returns an int from 0 to 2 i.e. either 0 or 1 or 2.