我目前正在尝试使用libgdx在android上创建一个应用程序。
该应用程序在Android工作室的模拟器上工作正常,但当我使用库box2d进行world.destroy(body)时,我的真实手机出现了崩溃。 更确切地说,在游戏执行过程中,应用程序会在碰撞后摧毁2个人体,就像它想要的那样,但在1秒后应用程序崩溃。
这是我的侦听员的代码。
@Override
public void beginContact(Contact contact) {
Body a = contact.getFixtureA().getBody();
Body b = contact.getFixtureB().getBody();
if ((BodyUtils.bodyIsWater(b) && BodyUtils.bodyIsEnemy(a))|| (BodyUtils.bodyIsEnemy(b) && BodyUtils.bodyIsWater(a))){
if (!remove.contains(a)) {
remove.add(a);
}
if (!remove.contains(b)) {
remove.add(b);
}
iswater = false;
}
else if(BodyUtils.bodyIsGround(b) && BodyUtils.bodyIsWater(a)){
if(!remove.contains(a)) {
remove.add(a);
}
iswater=false;
}
else if(BodyUtils.bodyIsGround(a) && BodyUtils.bodyIsWater(b)){
if(!remove.contains(b)) {
remove.add(b);
}
iswater=false;
}
a=null;
b=null;
}
关于代码的评论:水是射击敌人的子弹。删除是arraylist。
这是我的行为方法的代码,它破坏身体内部的身体以消灭(碰撞方法,但身体摧毁到arraylist)。 destroy调用进入for循环。
@Override
public void act(float delta) {
super.act(delta);
// Fixed timestep
accumulator += delta;
while (accumulator >= delta) {
world.step(TIME_STEP, 10, 10);
accumulator -= TIME_STEP;
}
for (int i = 0; i < remove.size(); i++) {
world.destroyBody(remove.get(i));
remove.remove(i);
/*for (int n = 0; i < y.getJointList().size; n++){
world.destroyJoint(y.getJointList().get(n).joint);
}*/
}
if(add%100==0 && !iswater) {
Array<Body> bodies = new Array<Body>(world.getBodyCount());
world.getBodies(bodies);
for (Body body : bodies) {
update(body);
}
}
if(add%80==0){
createEnemy();
}
add++;
}
以下是更新方法的内容:
private void update(Body body) {
if (BodyUtils.bodyIsEnemy(body) && !iswater) {
EnemyUserData z = (EnemyUserData) body.getUserData();
Vector2 m=body.getPosition();
if(m.x<23f){
createWater(z,m);
iswater=true;
}
}
}
首先,我试图找出问题所在的位置,并且我发现如果没有破坏机构的应用程序没有崩溃。所以知道我知道destroy函数会让应用程序崩溃。
这是Sigsegv日志:
04-03 19:01:50.068 25361-25404/com.mygdx.game A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x7a99fd63c0 in tid 25404 (GLThread 3290)
[ 04-03 19:01:50.076 402: 402 W/ ]
debuggerd: handling request: pid=25361 uid=10171 gid=10171 tid=25404
04-03 19:01:50.174 25501-25501/? A/DEBUG: *** *** *** *** *** *** *** ***
*** *** *** *** *** *** *** ***
04-03 19:01:50.174 25501-25501/? A/DEBUG: Build fingerprint: 'HUAWEI/PRA-LX1/HWPRA-H:7.0/HUAWEIPRA-LX1/C432B196:user/release-keys'
04-03 19:01:50.174 25501-25501/? A/DEBUG: Revision: '0'
04-03 19:01:50.174 25501-25501/? A/DEBUG: ABI: 'arm64'
04-03 19:01:50.174 25501-25501/? A/DEBUG: pid: 25361, tid: 25404, name: GLThread 3290 >>> com.mygdx.game <<<
04-03 19:01:50.174 25501-25501/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x7a99fd63c0
04-03 19:01:50.174 25501-25501/? A/DEBUG: x0 0000007a99fd63c0 x1 00000000000000b8 x2 0000007899f841c0 x3 0000000000000002
04-03 19:01:50.174 25501-25501/? A/DEBUG: x4 0000000000000001 x5 0000000000000001 x6 0000000000000000 x7 0000000000000000
04-03 19:01:50.174 25501-25501/? A/DEBUG: x8 000000789a2e8098 x9 0000000000000000 x10 0000000000000001 x11 0000000000000001
04-03 19:01:50.174 25501-25501/? A/DEBUG: x12 000000789a91d718 x13 000000789a91d7b4 x14 00000078b7ab1150 x15 0000000000000060
04-03 19:01:50.174 25501-25501/? A/DEBUG: x16 000000789a2e7a18 x17 000000789a2b99d8 x18 0000000000000001 x19 0000007899f841c0
04-03 19:01:50.175 25501-25501/? A/DEBUG: x20 00000078b6888760 x21 0000007899f84200 x22 0000000000000006 x23 00000078b0a86fa0
04-03 19:01:50.175 25501-25501/? A/DEBUG: x24 0000000000000048 x25 604eae760b11fb4a x26 00000078b80d5698 x27 00000078b80d5600
04-03 19:01:50.175 25501-25501/? A/DEBUG: x28 000000789a91d7b0 x29 000000789a91d6d0 x30 000000789a2bd004
04-03 19:01:50.175 25501-25501/? A/DEBUG: sp 000000789a91d6a0 pc 000000789a2b9a34 pstate 0000000080000000
04-03 19:01:50.180 25501-25501/? A/DEBUG: backtrace:
04-03 19:01:50.180 1295-1316/? I/Bluetooth_framework: BluetoothManagerService:Message: 401
04-03 19:01:50.181 25501-25501/? A/DEBUG: #00 pc 000000000001ba34 /data/app/com.mygdx.game-1/lib/arm64/libgdx-box2d.so (_ZN16b2BlockAllocator8AllocateEi+92)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #01 pc 000000000001f000 /data/app/com.mygdx.game-1/lib/arm64/libgdx-box2d.so (_ZN7b2World10CreateBodyEPK9b2BodyDef+48)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #02 pc 000000000002f6d0 /data/app/com.mygdx.game-1/lib/arm64/libgdx-box2d.so (Java_com_badlogic_gdx_physics_box2d_World_jniCreateBody+160)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #03 pc 00000000000db790 /system/lib64/libart.so (art_quick_generic_jni_trampoline+144)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #04 pc 00000000000d21b4 /system/lib64/libart.so (art_quick_invoke_stub+580)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #05 pc 00000000000dee80 /system/lib64/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+204)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #06 pc 000000000028cbf0 /system/lib64/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+312)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #07 pc 0000000000286cac /system/lib64/libart.so (_ZN3art11interpreter6DoCallILb1ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+444)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #08 pc 00000000005556e4 /system/lib64/libart.so (MterpInvokeDirectRange+384)
04-03 19:01:50.181 25501-25501/? A/DEBUG: #09 pc 00000000000c4f94 /system/lib64/libart.so (ExecuteMterpImpl+15252)
[ 04-03 19:01:50.536 402: 402 W/ ]
debuggerd: resuming target 25361
我还测试了一个与我的手机具有相同Android版本的模拟器,并且它有效。 (它是版本7 api 24)。所以这不是问题所在。
ps:我不会摧毁两次尸体(我已经放了一些条件来避免它)。我是libgdx的菜鸟,请帮忙&lt; 3(抱歉我的英文不好!)
编辑:回答&#34; Subrata M&#34;的评论中的问题。如果我在更新和createEnemy()之后放置了destroy方法,那么这就是logcat。
04-03 20:26:59.973 29708-29747/com.mygdx.game A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x7a9a07b400 in tid 29747 (GLThread 3385)
04-03 20:26:59.974 2497-2497/? I/WearableService: Wearable Services stopping
[ 04-03 20:26:59.976 402: 402 W/ ]
debuggerd: handling request: pid=29708 uid=10171 gid=10171 tid=29747
04-03 20:27:00.017 1600-1600/? W/HwKeyguardDragHelper: AnimationBlocked
04-03 20:27:00.025 1600-1600/? I/EventCenter: EventCenter Get :android.intent.action.TIME_TICK
04-03 20:27:00.032 1600-1600/? W/HwKeyguardDragHelper: AnimationBlocked
04-03 20:27:00.039 1600-1600/? E/DateLunarView: mDateString is: mar 3 avr
04-03 20:27:00.042 1600-1600/? E/DateLunarView: mDateString is: mar 3 avr
04-03 20:27:00.054 2572-2572/? I/HwLauncher: Model onReceive intent=Intent { act=android.intent.action.TIME_TICK flg=0x50000014 (has extras) }
04-03 20:27:00.054 2572-2572/? I/HwLauncher: Model onReceive user=UserHandle{0}
04-03 20:27:00.079 29868-29868/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
04-03 20:27:00.079 29868-29868/? A/DEBUG: Build fingerprint: 'HUAWEI/PRA-LX1/HWPRA-H:7.0/HUAWEIPRA-LX1/C432B196:user/release-keys'
04-03 20:27:00.079 29868-29868/? A/DEBUG: Revision: '0'
04-03 20:27:00.079 29868-29868/? A/DEBUG: ABI: 'arm64'
04-03 20:27:00.079 29868-29868/? A/DEBUG: pid: 29708, tid: 29747, name: GLThread 3385 >>> com.mygdx.game <<<
04-03 20:27:00.079 29868-29868/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x7a9a07b400
04-03 20:27:00.079 29868-29868/? A/DEBUG: x0 0000007a9a07b400 x1 00000000000000b8 x2 0000007899b36980 x3 0000000000000002
04-03 20:27:00.079 29868-29868/? A/DEBUG: x4 0000000000000001 x5 0000000000000001 x6 0000000000000000 x7 0000000000000000
04-03 20:27:00.079 29868-29868/? A/DEBUG: x8 000000789775e098 x9 0000000000000000 x10 0000000000000001 x11 0000000000000001
04-03 20:27:00.079 29868-29868/? A/DEBUG: x12 000000789aa1a7e8 x13 000000789aa1a884 x14 00000078b7ab1150 x15 0000000000000060
04-03 20:27:00.079 29868-29868/? A/DEBUG: x16 000000789775da18 x17 000000789772f9d8 x18 0000000000000001 x19 0000007899b36980
04-03 20:27:00.079 29868-29868/? A/DEBUG: x20 00000078b687a270 x21 0000007899b369c0 x22 0000000000000006 x23 00000078b6a13fa0
04-03 20:27:00.079 29868-29868/? A/DEBUG: x24 0000000000000048 x25 604eae760b11fb4a x26 00000078a908ca98 x27 00000078a908ca00
04-03 20:27:00.079 29868-29868/? A/DEBUG: x28 000000789aa1a880 x29 000000789aa1a7a0 x30 0000007897733004
04-03 20:27:00.079 29868-29868/? A/DEBUG: sp 000000789aa1a770 pc 000000789772fa34 pstate 0000000080000000
04-03 20:27:00.087 1600-1600/? W/ExpandableNotificationRow: setActionsBackground,null == view,mPublicLayout
04-03 20:27:00.089 1600-1600/? W/ExpandableNotificationRow: setActionsBackground,null == view,mPublicLayout
04-03 20:27:00.092 1600-1600/? W/ExpandableNotificationRow: setActionsBackground,null == view,mPublicLayout
04-03 20:27:00.094 1600-1600/? W/ExpandableNotificationRow: setActionsBackground,null == view,mPublicLayout
04-03 20:27:00.097 1600-1600/? W/HwKeyguardDragHelper: AnimationBlocked
04-03 20:27:00.098 29868-29868/? A/DEBUG: backtrace:
04-03 20:27:00.098 29868-29868/? A/DEBUG: #00 pc 000000000001ba34 /data/app/com.mygdx.game-2/lib/arm64/libgdx-box2d.so (_ZN16b2BlockAllocator8AllocateEi+92)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #01 pc 000000000001f000 /data/app/com.mygdx.game-2/lib/arm64/libgdx-box2d.so (_ZN7b2World10CreateBodyEPK9b2BodyDef+48)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #02 pc 000000000002f6d0 /data/app/com.mygdx.game-2/lib/arm64/libgdx-box2d.so (Java_com_badlogic_gdx_physics_box2d_World_jniCreateBody+160)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #03 pc 00000000000db790 /system/lib64/libart.so (art_quick_generic_jni_trampoline+144)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #04 pc 00000000000d21b4 /system/lib64/libart.so (art_quick_invoke_stub+580)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #05 pc 00000000000dee80 /system/lib64/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+204)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #06 pc 000000000028cbf0 /system/lib64/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+312)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #07 pc 0000000000286cac /system/lib64/libart.so (_ZN3art11interpreter6DoCallILb1ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+444)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #08 pc 00000000005556e4 /system/lib64/libart.so (MterpInvokeDirectRange+384)
04-03 20:27:00.098 29868-29868/? A/DEBUG: #09 pc 00000000000c4f94 /system/lib64/libart.so (ExecuteMterpImpl+15252)
04-03 20:27:00.123 2572-2572/? E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
04-03 20:27:00.131 2572-2572/? E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
04-03 20:27:00.136 2572-2572/? E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
[ 04-03 20:27:00.390 402: 402 W/ ]
debuggerd: resuming target 29708
EDIT2:我让两位朋友测试了这款应用。第一个有1加3t并没有崩溃。另一个有华为伴侣9,它崩溃了。
答案 0 :(得分:0)
这肯定是box2d分配问题,而不是手机/特定问题。您可以做的是在销毁它时将body对象设置为null,您将看到正在发生的事情。
试试这个:
for (int i = 0; i < remove.size(); i++) {
world.destroyBody(remove.get(i));
remove.get(i) = null;
/*for (int n = 0; i < y.getJointList().size; n++){
world.destroyJoint(y.getJointList().get(n).joint);
}*/
}
remove.clear();