我的应用程序在前五分钟运行良好,但在一段时间后它崩溃了。`
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,我用来运行我的程序没有给出任何错误,因为我的应用程序崩溃了。 提前致谢
`
最佳答案
在渲染循环中,每次循环运行时都会创建一个新的 BitmapFont
对象。尽管 libgdx 管理某些对象的内存,this链接表示 BitmapFont
在使用完毕后需要进行处理,否则您的游戏将继续为每个新对象分配内存。因为您已经在 create
方法中创建了 font
,所以您不需要再次实例化它。当您使用完它后,您可以在您已经使用 font.dispose()
之类的方法设置的 dispose()
方法中处理它。
关于java - 运行 5 分钟后,我的 libgdx 游戏崩溃并变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21963356/