Bezzi
Bezzi

Reputation: 260

LibGdx-Blank Screen that can't be closed

I want to make a simple flappy bird clone while I'm in vacation(The firt time I try game programming since high school) so after looking at ligdx wiki and sort of adapting the code to fit my needs I had something like this:

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

public class FlappyBall implements ApplicationListener {
    Texture ballImage;
    Texture chaoImage;
    Texture canoImage;
    OrthographicCamera camera;
    SpriteBatch batch;
    Rectangle ball;
    int score = 0;
    final float gravity = 9.0f;

    @Override
    public void create() {

        ballImage = new Texture(Gdx.files.internal("crystalball_small.png"));
        //chaoImage = new Texture(Gdx.files.internal("Flappy-Ground.png"));


        camera = new OrthographicCamera(800,480);
        camera.setToOrtho(false, 800, 480);


        batch = new SpriteBatch();


        ball = new Rectangle();
        ball.x = 128;
        ball.y = 20;
        ball.width = 64;
        ball.height = 64;

    }

    @Override
    public void dispose() {
        ballImage.dispose();
        //chaoImage.dispose();

    }

    @Override
    public void render() {
        // cleans the screen
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        // rendenring ball
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(ballImage, ball.x, ball.y);
        batch.end();

        // jump(not tested)
        if (Gdx.input.isTouched()) {
            ball.y += 10;
        }

        // apply gravity(not tested)
        while (ball.y < 480)
            ball.y -= gravity * Gdx.graphics.getDeltaTime();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

however this code ends up showing a blank screen that can't even be closed when I press the X button(in order to close it I need to terminate its process in eclipse or use the xkill command :/)

Ps.:That's my Main.java file:

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main {
    public static void main(String[] args) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
        cfg.title = "Flappy Ball";
        cfg.useGL20 = false;
        cfg.width = 800;
        cfg.height = 480;

        new LwjglApplication(new FlappyBall(), cfg);
    }
}

Ps².:The compiler shows no errors when this code is being executed

Thanks :D

Upvotes: 0

Views: 104

Answers (1)

Roysten
Roysten

Reputation: 91

Your render method will probably never finish it's first frame. The while loop will keep going until ball.y becomes greater than or equal to 480. This will never happen because you are subtracting a positive number.

        while (ball.y < 480)
        ball.y -= gravity * Gdx.graphics.getDeltaTime();

I would rather change it in something like this:

float change = gravity * Gdx.graphics.getDeltaTime();
if(ball.y - change >= 0)
   ball.y -= change;

Upvotes: 1

Related Questions