我正在使用 andengine 开发一款 Android 游戏。基本上,我使用 2 个 ArrayList 来存储 2 种类型的 Sprite 。我在运行时添加和删除两种类型的 Sprite,以响应用户交互。但是,我会遇到随机崩溃,仅出现以下错误代码:
10-09 12:11:13.532: A/libc(8015): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)
10-09 12:11:13.572: V/AndEngine(8015): org.andengine.input.touch.TouchEvent$TouchEventPool<TouchEvent> was exhausted, with 2 item not yet recycled. Allocated 1 more.
10-09 12:11:13.572: V/AndEngine(8015): org.andengine.util.adt.pool.PoolUpdateHandler$1<TouchEventRunnablePoolItem> was exhausted, with 2 item not yet recycled. Allocated 1 more.
10-09 12:11:13.602: V/AndEngine(8015): org.andengine.input.touch.TouchEvent$TouchEventPool<TouchEvent> was exhausted, with 3 item not yet recycled. Allocated 1 more.
10-09 12:11:13.602: V/AndEngine(8015): org.andengine.util.adt.pool.PoolUpdateHandler$1<TouchEventRunnablePoolItem> was exhausted, with 3 item not yet recycled. Allocated 1 more.
10-09 12:11:13.622: V/AndEngine(8015): org.andengine.input.touch.TouchEvent$TouchEventPool<TouchEvent> was exhausted, with 4 item not yet recycled. Allocated 1 more.
10-09 12:11:13.622: V/AndEngine(8015): org.andengine.util.adt.pool.PoolUpdateHandler$1<TouchEventRunnablePoolItem> was exhausted, with 4 item not yet recycled. Allocated 1 more.
10-09 12:11:16.195: V/AndEngine(8015): org.andengine.input.touch.TouchEvent$TouchEventPool<TouchEvent> was exhausted, with 5 item not yet recycled. Allocated 1 more.
10-09 12:11:16.195: V/AndEngine(8015): org.andengine.util.adt.pool.PoolUpdateHandler$1<TouchEventRunnablePoolItem> was exhausted, with 5 item not yet recycled. Allocated 1 more.
10-09 12:11:16.275: V/AndEngine(8015): org.andengine.input.touch.TouchEvent$TouchEventPool<TouchEvent> was exhausted, with 6 item not yet recycled. Allocated 1 more.
10-09 12:11:16.275: V/AndEngine(8015): org.andengine.util.adt.pool.PoolUpdateHandler$1<TouchEventRunnablePoolItem> was exhausted, with 6 item not yet recycled. Allocated 1 more.
当我继续在屏幕上移动手指时,TouchEvent
池警告继续弹出,但游戏本身已挂起。老实说,我不知道是什么原因造成的!我环顾四周,甚至无法通过单一操作查明崩溃原因。
我创建/删除 Sprite 的方法如下:
类型ASprite:
- 在
TimerHandler
内创建 - 删除
ContactListener
内部,生成一个runOnUpdateThread()
Runnable
TypeBSprite:
- 在重写的
onSceneTouchEvent()
内创建,因为 Activity 扩展了IOnSceneTouchListener
。 - 删除
ContactListener
内部,生成一个runOnUpdateThread()
Runnable
每次创建 Sprite
时,它都会添加到其各自的 ArrayList
中。当需要删除时,通过 ContactListener
将其从 ArrayList
中删除。
任何帮助/想法将不胜感激!谢谢!
编辑:通过一些试验和错误,我非常确定问题出在 TypeBSprite
编辑:我已经实现了我的 TypeBSprite 创建,如下所示:
mEngine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
AnimatedSprite sprite = new AnimatedSprite(sX, sY, mSpriteRegion, getVertexBufferObjectManager());
sprite.setRotation(sRotation);
mScene.attachChild(sprite);
Body body = PhysicsFactory.createBoxBody(mPhysicsWorld, sprite, BodyType.StaticBody, MY_FIXTURE);
sprite.setUserData("spiteB");
body.setUserData(sprite);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true));
}
});
最佳答案
想通了!问题在于注册 runOnUpdateThread
Runnable
和实际执行它之间的时间间隔。问题在于,针对同一碰撞多次调用 ContactListener
,因此用于删除主体的 runOnUpdateThread
在同一个对象上被多次调用。
为了解决这个问题,我让 ContactListener
将 Sprite 的 UserData
设置为“已删除”。当对同一个对象再次调用 ContactListener
时,“if (...)
语句会比较 Sprite
的 UserData
会忽略它,因为它应该已经在删除的路上了。
希望这对将来的人有帮助!
关于android - AndEngine:获取 SIGSEGV 和 TouchEventPool 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12804350/