android - 运行 5 分钟后,我的 libgdx 游戏崩溃并变成红色

标签 android libgdx

public class PlayScreen implements Screen{

    Stage stage;

    LabelStyle style;
    BitmapFont font;

    TextureAtlas backbuttonatlas;
    TextButtonStyle backbuttonstyle;
    TextButton backbutton;
    Skin backskin;

    SpriteBatch batch;
    Texture pibe;
    Sprite sprite;
    Vector2 position;
    Game game;

    Texture texture;

    public PlayScreen(Game game){
        this.game=game;
    }

    @Override
    public void render(float delta) {

    stage=new Stage();

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


    if(Gdx.input.isKeyPressed(Keys.W))
    {
        position.x+=5f;
    }
    if(Gdx.input.isKeyPressed(Keys.A))
    {
        position.y-=5f;
    }
    if(Gdx.input.isKeyPressed(Keys.S))
    {
        position.x-=5f;
    }
    if(Gdx.input.isKeyPressed(Keys.D))
    {
        position.y+=5f;
    }

    if(Gdx.input.isTouched()==true)
    {
        if(Gdx.input.getY()>Gdx.graphics.getHeight()/2)
        {
        position.x-=5;
        }
        if(Gdx.input.getY()<Gdx.graphics.getHeight()/2)
        {
            position.x+=5;
        }
        if(Gdx.input.getX()>Gdx.graphics.getWidth()/2)
        {
            position.y+=5;
        }
        if(Gdx.input.getX()<Gdx.graphics.getWidth()/2)
        {
            position.y-=5;
        }

        if(Gdx.input.isKeyPressed(Keys.BACK))
        {
            game.setScreen(new MainMenu(game));
        }
    }

    font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
    style = new LabelStyle(font, Color.WHITE);

    backskin = new Skin();
    backbuttonatlas = new TextureAtlas("buttons/backbutton.pack");
    backskin.addRegions(backbuttonatlas);

    backbuttonstyle = new TextButtonStyle();
    backbuttonstyle.up = backskin.getDrawable("backbutton");
    backbuttonstyle.over = backskin.getDrawable("backbuttonpressed");
    backbuttonstyle.down = backskin.getDrawable("backbuttonpressed");
    backbuttonstyle.font = font;

    backbutton = new TextButton("", backbuttonstyle);
    backbutton.setWidth((float) (Gdx.graphics.getHeight()/8));
    backbutton.setHeight((float) (Gdx.graphics.getHeight()/8));
    backbutton.setPosition((Gdx.graphics.getWidth()/20), (float) (Gdx.graphics.getHeight()-(Gdx.graphics.getWidth()/8)));

    backbutton.addListener(new InputListener(){

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int backbutton) {
                game.setScreen(new MainMenu(game));
            return true;
        };});

    batch=new SpriteBatch();

    stage.addActor(backbutton);

    Gdx.input.setInputProcessor(stage);

    batch.begin();
    batch.draw(texture, 0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    batch.draw(pibe,(position.y/2-pibe.getWidth()/2),(position.x/2-pibe.getHeight()/2));
    batch.end();

    stage.act();
    stage.draw();

    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void show() {
        texture = new Texture("cielo.png");

        pibe = new Texture("superman (100x52).jpg");
        position = new Vector2(Gdx.graphics.getHeight(),Gdx.graphics.getWidth());
    }

    @Override
    public void hide() {
        // TODO Auto-generated method stub
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
    }
}

My LibGDX Game, collapse after a few minutes and I don't know why. I have read a little about the problem, and it says that the solution is to "dispose" the bitmapfont, or something like that. I'm new in LibGDX and I don't understand so much. A full explanation is appreciated. Sorry for my poor English. This is the Play Class. Please, need help. Thanks.

最佳答案

你必须把像 batch = new SpriteBatch() 这样的“创造”东西放在里面

@覆盖 公共(public)无效创建(){

)

您创建了数十亿个导致内存问题的 SpriteBatch。

关于android - 运行 5 分钟后,我的 libgdx 游戏崩溃并变成红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24440686/

相关文章:

java - Libgdx 更改窗口大小而不禁用桌面上的调整大小

android - libgdx - 在删除应用程序或更新应用程序后是否保存首选项文件?

eclipse - Libgdx + Eclipse + Admob

android - Fragment 内的 ViewPager 不会恢复子 fragment 的状态

android - 如何在Android中及时收听变化

javascript - 如果 React Native 中没有互联网连接,则获取存储数据

android - 使用 chrome 进行远程调试仅适用于较旧的人行横道版本 [cordova 4]

Android:如何将自定义元素放在圆圈/目标上

java - 左键单击时指针下 Sprite 的鼠标坐标偏移补偿问题

java - 在 LIBGDX 中实现游戏 handle Controller ?