【问题标题】:After 5 minutes of running , my libgdx game crashes and turns red运行 5 分钟后,我的 libgdx 游戏崩溃并变红
【发布时间】:2014-06-27 06:02:04
【问题描述】:
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
    }
}

我的 LibGDX 游戏,几分钟后崩溃,我不知道为什么。我已经阅读了一些关于这个问题的内容,它说解决方案是“处理”位图字体或类似的东西。我是 LibGDX 的新手,我不太了解。一个完整的解释表示赞赏。对不起我的英语不好。 这是游戏课。请,需要帮助。谢谢。

【问题讨论】:

  • 对于那些明显需要代码帮助的人,我不会投反对票。赞成。

标签: android libgdx


【解决方案1】:

你必须把你“创造”的东西,比如batch = new SpriteBatch() 放进去

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

)

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

【讨论】:

  • 同意。几乎可以肯定,OP 很快就会使堆最大化。
【解决方案2】:

每次设备准备好更新屏幕时都会调用 Render。您正在每帧创建新对象。其中一些必须手动处理,这意味着调用该对象的.dispose() 方法。

打电话

font.dispose(); 

当你完成字体以防止它耗尽所有内存时。

理想情况下,您希望在渲染循环之外创建该字体。

您应该在构造函数中创建对象,这样它们就不会在每一帧都重新创建。当然,除非这是预期的行为。

试试这样的

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;
        font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
        style = new LabelStyle(font, Color.WHITE);
        stage=new Stage();
        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);

    }

    @Override
    public void render(float delta) {


        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));
            }
        }

        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
    }

【讨论】:

  • 嗨。首先,谢谢你的回答。当我将所有对象的创建移至构造函数时,它要求我更改为:public PlayScreen( Game game)。致public PlayScreen(final Game game)。我应该怎么办?。此外,如果我移动这些对象,我会遇到问题:stage.addActor(backbutton);。它说:线程“LWJGL应用程序”中的异常......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多