有时,大约20次尝试中,游戏就崩溃了。
这对我来说是没有意义的。第一次启动后,我尝试重现错误500次左右。但没有成功。重启游戏后,游戏在第12次尝试后崩溃。这只影响Windows。我无法在2个不同的Android手机上重现这个错误,即使在尝试了大约500次之后。
我已经找了8个多小时的类似错误,但他们没有接近我的问题,也没有帮助我。
我到处都有System.out.print指令,1h后发现游戏崩溃的原因是以下一行:body = world.createBody(bdef)。
我尝试了所有建议的解决方案,在所有地方添加了安全if语句,并尝试了互联网上所有可以想象到的建议(你可以在下面的代码中看到),没有任何帮助。
public class Coins extends SpawnAble {
private TextureRegion textureRegion[][];
private Texture texture;
private int framecount = 0;
private int frame = 0;
private int startcount;
public Coins(Playscreen playscreen, float x, float y) {
super(playscreen, x, y);
texture = new Texture("spr_coin_strip4.png");
textureRegion = TextureRegion.split(texture, 16, 16);
setRegion(textureRegion[0][frame]);
startcount = (int) (y * 100) / 16 * 10 - 64;
System.out.println(startcount);
}
@Override
public void defineSpawnAble() {
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.KinematicBody;
System.out.println("Body Created Kinematic");
bdef.position.set(getX() + getWidth() / 2, getY() + getHeight() / 2);
System.out.println("set position "+world.isLocked());
if (body == null) {
body = world.createBody(bdef);
}
System.out.println("set crated");
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(6 / Statics.PPM);
fdef.isSensor = true;
fdef.shape = shape;
fdef.filter.categoryBits = BIT_FILTER.SPAWNABLE_BIT;
fdef.filter.maskBits = BIT_FILTER.PLAYER_BIT | BIT_FILTER.BRICK_BIT | BIT_FILTER.DEFAULT_BIT | BIT_FILTER.OBJEKT_BIT | BIT_FILTER.MOVING_BIT;
body.createFixture(fdef).setUserData(this);
System.out.println("set userdata");
}
@Override
public void use() {
System.out.println("spawnable used");
playscreen.getPlayer().collectGem();
if (!destroyed && !toDestroyed) {
if (body != null) {
destroy();
}
}
}
@Override
public void update(float dt) {
super.update(dt);
if(!destroyed) {
if (startcount >= 0) {
startcount--;
}
if (!toDestroyed && !destroyed) {
framecount++;
if ((framecount % 15) == 0) {
frame++;
if (frame > 3) {
frame = 0;
}
}
}
setRegion(textureRegion[0][frame]);
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);
}
}
@Override
public void draw(Batch batch) {
if (startcount <= 0 && !destroyed) {
super.draw(batch);
}
}
}
public class SpawnDef {
public Vector2 position;
public Class<?> type;
public SpawnDef(Vector2 position, Class<?> type){
this.position = position;
this.type = type;
}
}
public abstract class SpawnAble extends Sprite {
protected Playscreen playscreen;
protected World world;
protected boolean toDestroyed;
protected boolean destroyed;
protected Body body;
public SpawnAble(Playscreen playscreen, float x, float y){
this.playscreen = playscreen;
this.world = playscreen.getWorld();
setPosition(x, y);
setBounds(getX(), getY(), 16 / Statics.PPM, 16 / Statics.PPM);
defineSpawnAble();
toDestroyed = false;
destroyed = false;
}
public void update(float dt){
if (toDestroyed && !destroyed){
destroyed = true;
if (body != null) {
world.destroyBody(body);
}
}
}
public void draw(Batch batch){
if (!destroyed){
super.draw(batch);
}
}
public abstract void defineSpawnAble();
public abstract void use();
public void destroy(){
toDestroyed = true;
}
}
//Mainclass
public void spawn(SpawnDef sdef){
tospawn.add(sdef); //Arraylist
}
[...]
public void handleSpawning(){
if (!tospawn.isEmpty()){
SpawnDef spawnDef = tospawn.get(tospawn.size()-1);
if (spawnDef.type == Coins.class){
System.out.println("spawning....");
spawnAbles.add(new Coins(Playscreen.this, spawnDef.position.x, spawnDef.position.y)); //Arraylist
System.out.println("spawned");
tospawn.remove(tospawn.size()-1);
}
}
}
[...]
@Override
public void render(float delta) {
world.step(1 / 60f, 6,2);
[...]
handleSpawning();
[...]
for (SpawnAble spawnAble : spawnAbles){
spawnAble.update(dt);
}
}
[...]
for (SpawnAble spawnAble : spawnAbles){
spawnAble.draw(jumpAndRun.batch);
}
[...]
System.out.println(""+spawnAbles.size() + "|" + tospawn.size());
错误和日志
spawned
spawnable update beginn
spawnable update ende
spawnable zeichnen beginn
spawnable zeichnen ende
2|1
world stepped
spawning....
Body Created Kinematic
set position false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006e9cbbb1, pid=2292, tid=0x0000000000000b8c
#
# JRE version: OpenJDK Runtime Environment (8.0_212-b04) (build 1.8.0_212-release-1586-b04)
# Java VM: OpenJDK 64-Bit Server VM (25.212-b04 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [gdx-box2d64.dll+0xbbb1]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\flori\Desktop\JumpandRun\Game\hs_err_pid2292.log
#
# If you would like to submit a bug report, please visit:
#
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
AL lib: (EE) alc_cleanup: 1 device not closed
Process finished with exit code 1
----------------------------------------------------------------------------
Log: https://pastebin.com/ep0NZnJQ (too long to post here)
我非常感谢任何形式的想法。
我自己解决了这个问题。如果你有这个问题,不要试图改变jre的版本,Libgdx需要1.8。Libgdx需要1.8版本。
如果你有这个问题,我真的很抱歉,这个问题可能有很多原因。我在这里列举了几个原因。
在我的案例中,我错误地处理了一个物体。