【问题标题】:LibGdx-Blank Screen that can't be closedLibGdx-无法关闭的空白屏幕
【发布时间】:2014-03-05 04:11:21
【问题描述】:

我想在度假时制作一个简单的飞扬小鸟克隆(我从高中开始第一次尝试游戏编程),所以在查看了 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

【问题讨论】:

    标签: java libgdx


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 2014-01-13
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      相关资源
      最近更新 更多