Question

I am currently trying to make a game that involves a randomly generated world. I currently have a working splash screen and a working perlin noise gen with it normalized. all i need is a way to implement the code to display the images.

the code:

public void show() {
    batch = new SpriteBatch();
    World world = new World();


    int type = 0;

    for (int y = 1; y < world.getlengthy();y++){
        for (int x = 1; x < world.getlengthx();x++){
        type = world.getvalue(x, y);


        switch (type) {
            case 1:sprite = new Sprite(tex1,0,0,16,16);sprite.setPosition(x, y);batch.begin();sprite.draw(batch);batch.end();
            Gdx.app.log("", "x: " + x + " y: " + y);
            break;
            case 2:batch.begin();batch.draw(tex2, x, y);batch.end();
            break;
            case 3:batch.begin();batch.draw(tex3, x, y);batch.end();
            break;
            case 4:batch.begin();batch.draw(tex4, x, y);batch.end();
            break;
            case 5:batch.begin();batch.draw(tex5, x, y);batch.end();
            break;
            case 6:batch.begin();batch.draw(tex6, x, y);batch.end();
            break;
            case 7:batch.begin();batch.draw(tex7, x, y);batch.end();
            break;
            case 8:batch.begin();batch.draw(tex8, x, y);batch.end();
            break;
            }
        }
Was it helpful?

Solution

Your code need to be inside of the render method. The show is just called if the screen get shown the first time. (1 Time call!) So put the for loop inside of the render Cycle and it should work.

Just as a small hint. Do not create Objects inside of your render cycle sprite = new Sprite(tex1,0,0,16,16);. Always initialize all objects in the show method or inside of the constructor to save rendertime.(it does make a big differents in framerates)

Also just begin() one time befor the loop and end() after the loop.

For example something like this. I still wouldnt create Sprites inside of the render but i dont know the rest of your logic. Hope this helps! Regards

@Override
public void show() {
    batch = new SpriteBatch();
    World world = new World();
}


@Override
public void render(float delta){
    int type = 0;
    batch.begin();
    for (int y = 1; y < world.getlengthy();y++){
        for (int x = 1; x < world.getlengthx();x++){
        type = world.getvalue(x, y);


        switch (type) {
            case 1:
                sprite = new Sprite(tex1,0,0,16,16);
                sprite.setPosition(x, y);
                sprite.draw(batch);
                Gdx.app.log("", "x: " + x + " y: " + y);
            break;
            case 2:
                batch.draw(tex2, x, y);
            break;
            case 3:
                batch.draw(tex3, x, y);
            break;
            case 4:
                batch.draw(tex4, x, y);
            break;
            case 5:
                batch.draw(tex5, x, y);
            break;
            case 6:
                batch.draw(tex6, x, y);
            break;
            case 7:
                batch.draw(tex7, x, y);
            break;
            case 8:
                batch.draw(tex8, x, y);
            break;
            }
        }
        batch.end();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top