android - AndEngine:获取 SIGSEGV 和 TouchEventPool 事件

标签 android andengine

我正在使用 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 (...) 语句会比较 SpriteUserData 会忽略它,因为它应该已经在删除的路上了。

希望这对将来的人有帮助!

关于android - AndEngine:获取 SIGSEGV 和 TouchEventPool 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12804350/

相关文章:

android - 在Andengine中退出或退出游戏

android - 通过 Scene Transition 维护 "fitsSystemWindows"窗口插入

Android Studio midi 合成器编译错误, Protocol Buffer

Android:为什么不让每个方法都同步?

android - 如何在 AndEngine 中创建 ScrollView

java - AndEngine - 检查与 Sprite 或 Physics Body 的重叠?

java - 为什么这个 for 循环不起作用?

android - 如何在 gradle.kts 中设置全局变量?

java - AndEngine GenericPool 带有计时器,从池中添加 Sprite

java - Andengine 多人扩展 make 抛出错误,但在 Intellij IDEA 编辑器窗口中不显示