我当前正在尝试使用Android上的libgdx进行申请。
该应用程序在Android Studio的仿真器上正常工作,但是当我使用libly 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。
这是我的行为方法的代码,它破坏了我的身体阵列内部的身体以破坏(碰撞方法,而是销毁阵列列表的碰撞方法(。销毁的呼叫已进入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++;
}
这是更新methode的内容:
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;
}
}
}
首先,我试图弄清楚出现问题的位置,我发现如果A不破坏尸体,该应用不会崩溃。所以知道我知道销毁功能使应用程序崩溃。
这是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(对不起我的英语不好!(
编辑:在" subrata m"评论中回答一个问题,如果我在更新之后放置了销毁方法,则是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:我请两个朋友测试该应用程序。第一个有一个OnePlus 3T,并且不会崩溃。另一个有华为的伴侣9,它崩溃了。
这肯定是一个Box2D分配问题,而不是电话/发出特定问题。您可以做的是将身体对象设置为无效,当您销毁它时,您将看到正在发生的事情。
尝试以下操作:
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();