【问题标题】:After 5 minutes of running , my libgdx game crashes and turns black运行 5 分钟后,我的 libgdx 游戏崩溃并变黑
【发布时间】:2014-02-23 02:21:08
【问题描述】:

我的应用在前五分钟运行良好,但一段时间后它崩溃了。`

package com.me.fixGame;

import java.util.Random;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;


import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.sun.jmx.snmp.tasks.Task;


public class fixGame implements ApplicationListener {
    SpriteBatch batch;
    SpriteBatch spriteBatch;
    Texture trash; 
    Texture paper;
    SpriteBatch spritebatch;
    Vector2 position;
    Vector2 size;
    Vector2 size2;
    Vector2 pos;
    Rectangle bounds;
    Rectangle bounds2;
    float delay = 1; // seconds
boolean counted= false;
    int score = 3;
    String myScore;
    CharSequence str; // = myScore;
    BitmapFont font;
    boolean collision = false;
    @Override
    public void create() {  
        float delaySeconds = 1;
        spriteBatch = new SpriteBatch();
    trash = new Texture(Gdx.files.internal("data/trash.png"));
    paper = new Texture(Gdx.files.internal("data/paper1.jpg"));
    position = new Vector2(100, 50);
    pos = new Vector2(54, 14);
    batch = new SpriteBatch();
    BitmapFont font = new BitmapFont();

    size2 = new Vector2(trash.getWidth() ,trash.getHeight() );
    //size2.y = trash.getHeight();
    //size2.x = trash.getWidth();
    size = new Vector2(paper.getWidth() ,paper.getHeight());

    bounds= new Rectangle(pos.x, pos.y, size.x, size.y);
    bounds2= new Rectangle(position.x, position.y, size2.x, size2.y);

    }

    @Override
    public void dispose() {

    }
    public void update(){
        bounds.set(pos.x, pos.y, size.x, size.y);
        bounds2.set(position.x, position.y, size2.x, size2.y);

        position.x = position.x -2- Gdx.input.getAccelerometerX();
    }




    @Override
    public void render() {

        if(bounds.overlaps(bounds2)){
            collision=true; 
            counted=true;
        }else{
            collision=false;
        }

        if(collision==true){

        } 

        if(pos.y<640){
            counted=false;
        } else if(pos.y > 640 && collision==false && counted==false){
            counted=true;
            score= score-1;
            myScore = "Your Score: " + score;
            str = myScore;
        }


        BitmapFont font = new BitmapFont();
          update();
            Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        pos.y=pos.y-12;
        if(pos.y<0){
            pos.y = 700;
            Random randomGenerator = new Random();
              pos.x = randomGenerator.nextInt(500);
        }



        batch.begin();
        batch.draw(paper, pos.x, pos.y);
        batch.draw(trash, position.x, position.y);

        font.setColor(0.0f, 0.0f, 1.0f,1.0f);
        font.draw(batch, str, 300,250);
                batch.end();

    }

     @Override  
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

我不确定发生了什么,任何帮助将不胜感激。我想知道的另一件事,eclipse,我用来运行我的程序没有给出任何错误,但我的应用程序崩溃了。 提前致谢

`

【问题讨论】:

    标签: java android crash libgdx


    【解决方案1】:

    在您的渲染循环中,每次循环运行时,您都会创建一个新的BitmapFont 对象。虽然 libgdx 管理一些对象的内存,this 链接说BitmapFont 需要在使用完成后进行处理,否则您的游戏将继续为每个新对象分配内存。由于您已经在 create 方法中创建了 font,因此您不需要再次实例化它。使用完毕后,您可以在 dispose() 方法中处理它,您已经使用类似 font.dispose() 的方法设置了它。

    【讨论】:

    • 是的。通常,在 init 完成后尽可能避免调用 new。使用池来重用对象等。
    • 很遗憾,当这种情况发生时,libgdx 没有发出错误消息......不过,这只是解决了我一整天的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多