LibGdx-Blank Screen无法关闭

时间:2014-03-04 19:51:27

标签: java libgdx

我想在度假期间制作一个简单易碎的鸟类克隆(我从高中开始尝试游戏编程的第一次)所以在看了ligdx wiki并根据我的需要调整代码后,我有类似的东西这样:

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() {
    }
}

然而,当我按下X按钮时,此代码最终显示一个甚至无法关闭的空白屏幕(为了关闭它我需要在eclipse中终止其进程或使用xkill命令:/)

Ps.:那是我的Main.java文件:

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².:编译器在执行此代码时没有显示错误

谢谢:D

1 个答案:

答案 0 :(得分:1)

你的渲染方法可能永远不会完成它的第一帧。 while循环将一直持续到ball.y变得大于或等于480.这将永远不会发生,因为你正在减去一个正数。

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

我宁愿用这样的东西改变它:

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