简体   繁体   中英

How can I get my image to stop repeatedly randomly generating co-ordinate boundary values?

I'm creating a game in Java using the Slick2D library, and I'm a real beginner with this stuff.

I've been trying to get the image object I've created to randomly generate on the screen, which works, except it keeps randomly generating the co-ordinates for the image on loop. I want it to randomly generate once until another object touches it in which case it disappears and another instance of the same image object generates in a random position.

So far I've tried creating random objects for the x and y co-ordinates of the image.

    Image enemy;
    int enemyX = 800;
    int enemyY = 500;
    int RandomX;
    int RandomY;

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
enemy = new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\enemy.png");
}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        background.draw();
        blobfish.draw(RandomX, RandomY);
        enemy.draw(RandomX, RandomY);
        ninja.draw(characterX, characterY);
        g.drawString("X pos: " + characterX + "\nY pos: " + characterY, 400, 200);

        Random enemyX = new Random();
        Random enemyY = new Random();
        RandomX = enemyX.nextInt(1500);
        RandomY = enemyY.nextInt(1000);

public void update(GameContainer gc, StateBasedGame sbg, int delta) {
        enemy.startUse();
}

No error messages. However I want it to generate just once but instead it generates so many times so it looks like it's flickering on the page.

Any help is greatly appreciated!

It looks like you're updating RandomX and RandomY every time that you render your image, instead of only updating them when the enemy is touched. You usually don't want to update state inside of a render function, for the reason that it can be called many times a second. Instead, you have a recurring event handler (eg Timer) to handle state updates, and render state immutably inside a rendering method.

I've made some quick pseudocode of what you might want instead:

    Image enemy;
    int enemyX = 800;
    int enemyY = 500;
    Random random = new Random();
    int RandomX = random.nextInt(1500);
    int RandomY = random.nextInt(1000);

...

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        //draw things
        ...
        Point enemyLoc = game.getEnemy().getLocation();
        enemy.draw(enemyLoc.x, enemyLoc.y);
}
//UPDATE FUNCTION
public void ???(game, ???) {
    ...
    //update OUTSIDE of render function
    if(game.getEnemy().isTouched()) {
        game.getEnemy().setLocation(random.nextInt(1500), random.nextInt(1000));
    }
    ...
}

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