gpt4 book ai didi

android - LibGDX 启动画面中的空指针

转载 作者:行者123 更新时间:2023-11-29 18:02:21 25 4
gpt4 key购买 nike

我有一个启动类,它应该只显示启动图像,但它会导致空指针异常。当我在这个方法起作用的地方制作其他 Java 应用程序时,它令人激动。我已经阅读了大量文档以查看我哪里出错了,但我似乎找不到它。

Splash.java class
package com.me.fypapplication;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
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;

public class Splash implements Screen{

private SpriteBatch spriteBatch;
private Texture splashTexture;
private Sprite sprite;

private Game myGame;

private OrthographicCamera camera;

public Splash (Game g)
{
myGame = g;
}

public void create() {

camera = new OrthographicCamera(800,400);

splashTexture = new Texture(Gdx.files.internal("data/splashtest.gif"));

sprite = new Sprite(splashTexture);
sprite.setSize(512, 512);
sprite.setPosition(256, 256);

spriteBatch = new SpriteBatch();

splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);


}

@Override
public void render(float delta) {

Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

camera.update();

spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();

}

如果有人能提供帮助,我们将不胜感激!

编辑!忘记错误

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.me.fypapplication.Splash.render(Splash.java:51)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:190)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)

最佳答案

你的相机是空的,因为没有任何东西在调用 create()。如果您正在实现 libgdx Screen 并且需要初始化一些东西,那么您应该在调用 render() 之前覆盖 show() 方法。

关于android - LibGDX 启动画面中的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15247500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com