1

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.

3
  • I think you should explain in more detail what do you mean by "it will stop and nothing works" - does it crash? does the result differ for what you were hoping for? Showing the context where the variable "random" is declared can also help. best of luck. Commented Apr 2, 2020 at 9:55
  • yes it does crash when the new cactus come the game will freeze
    – kajvans
    Commented Apr 2, 2020 at 11:03
  • do you want the whole class for the cactus
    – kajvans
    Commented Apr 2, 2020 at 11:12

3 Answers 3

0

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.

14
  • i use BufferedImage when i try this i does not work
    – kajvans
    Commented Apr 2, 2020 at 15:19
  • @kajvans - The solution is still valid. Just replace Image with BufferedImage. I've updated my answer. Try now and feel free to comment in case of any doubt/issue. Commented Apr 2, 2020 at 15:23
  • if i try now the game starts with freezing
    – kajvans
    Commented Apr 2, 2020 at 17:17
  • There seems to be a problem somewhere else in your code. In order to help you further, I need to see your complete code. Commented Apr 2, 2020 at 17:19
  • 1
    My guess is that either image number 3 is broken or you have a memory issue. Try setting only that image for the cactus and see if it works Commented Apr 3, 2020 at 12:11
0

There are a couple reasons you could be running into issues, the main one being the fact that you're called Random.nextBoolean() multiple times. When you do that, it gives you a new value each time. You'd want to set a variable equal to Random.nextBoolean().

Also, since you appear to be choosing between three different images, rather than two. For this, I recommend you use Random.nextInt instead:

private Cactus getRandomCactus() {
Cactus cactus;
cactus = new Cactus();
int randomInt = random.nextInt(0, 3); // this only called random.nextInt once, and gives me a random number between 0 and 2
cactus.setX(600);
if(randomInt == 0) {
    cactus.setImage(imagecactus1);
} 
else if (randomInt == 1) {
    cactus.setImage(imagecactus2);
}
else {
    cactus.setImage(imagecactus3);
}
    return cactus;
}
1
  • if i try your code i get many erros the game does not work he sets the pictures together and than nothing moves
    – kajvans
    Commented Apr 2, 2020 at 11:03
0

the problem what that there where 2 files called cactus3.png and also there was one space to much. thanks for thinking with me .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.