简体   繁体   English

Box2D碰撞崩溃时,AndEngine GLES1销毁并创建对象

[英]AndEngine GLES1 Destroy and Create Object During Box2D Collision crashes app

I am working (still) on an implementation of a Chain Reaction game. 我正在(仍在)实施“连锁反应”游戏。 I have a lot of particles bouncing around the screen, and the user can tap the screen to create a single 'sticky' particle. 我在屏幕周围有很多粒子反弹,用户可以点击屏幕以创建单个“粘性”粒子。

When any normal particle hits the user's sticky particle, that normal particle needs to become sticky also, so anything hitting it becomes sticky, and so on (chain reaction right!) 当任何普通粒子碰到用户的粘性粒子时,该普通粒子也需要变得发粘,因此击中它的任何东西都将变为粘性,依此类推(链反应正确!)。

However, the first time a normal ball collides with the user's sticky ball, the app crashes with the following error in LogCat: 但是,普通球第一次与用户的粘性球相撞时,应用程序崩溃并在LogCat中出现以下错误:

Fatal signal 11 (SIGSGV) at 0x0000005c (code=1), thread 24936 (chainreaction) 致命信号11(SIGSGV)位于0x0000005c(代码= 1),线程24936(chainreaction)

Here is the function I use when a collision has been detected (I have commented the line causing the error): 这是检测到冲突时使用的功能(我已注释导致错误的行):

    private void ParticleCollision(Contact contact)
{
    if (contact.getFixtureA().getBody().getUserData() != null &&
        contact.getFixtureB().getBody().getUserData() != null) 
    {
        final String objA = (String)contact.getFixtureA().getBody().getUserData();
        Body bodyA = contact.getFixtureA().getBody();
        final String objB = (String)contact.getFixtureB().getBody().getUserData();
        Body bodyB = contact.getFixtureB().getBody();

        // Get the body objects for each of the bodies in the collision
        Body pBody2 = objA.startsWith("particle_") ? bodyA : bodyB;
        String cud = (String)pBody2.getUserData();

        // Find the normal body in the collision in our mBodyList array                        
        for (int i=0; i < mBodyList.size(); i++)
        {
            Body b = mBodyList.get(i);
            String tud = (String)b.getUserData();

            if(cud.equals(tud))
            {
                // We have a match
                // Get the cooresponding Particle (to get it's X/Y pos)
                Particle p = mParticleList.get(i);
                float x = p.getX();
                float y = p.getY();

                // Remove the particle that collided from our Lists
                mParticleList.remove(i);
                mFixtureList.remove(i);
                mBodyList.remove(i);

                // Remove the particle from the scene and the Body from the PhysicsWorld
                mScene.detachChild(p);
                mPhysicsWorld.destroyBody(b);

                // Create a new sticky particle where the old one was
                particle = new Particle(x, y, mPurpleParticleTextureRegion, 100);
                particle.setScale(0.3f);
                mScene.attachChild(particle);

                particleFixture = PhysicsFactory.createFixtureDef(10, 0.9f, 0.1f);
                    // Using the debugger, I see that this next line is causing the error
                            particle_body = PhysicsFactory.createBoxBody(mPhysicsWorld, particle, BodyType.StaticBody, particleFixture);
                particle_body.setUserData("sticky");
                mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(particle, particle_body, true, false));

                // Save this info to the sticky particle lists
                mStickyParticleList.add(particle);
                mStickyBodyList.add(particle_body);
                mStickyFixtureList.add(particleFixture);
            }
        }
    }
}

Previously I had that code in the mPhysicsWorld.setContactListener(), in the beginContact() method, but I read that you couldn't create bodies in the ContactListener. 以前,我在mPhysicsWorld.setContactListener()中的beginContact()方法中具有该代码,但是我读到您无法在ContactListener中创建主体。 So, I moved the code to the function you see, and I call that from the ContactListener. 因此,我将代码移到了您看到的函数中,并从ContactListener进行调用。

Any comments or suggestions as to what I have wrong would be greatly appreciated!! 任何关于我错的评论或建议,将不胜感激!

Thanks!! 谢谢!!

You can't change anything in the world inside the Step function. 您不能在Step函数中更改世界。 You'll need to create the new body after the Step function. 您需要在Step函数之后创建新的主体。

The same problem happens if you try to destroy a body inside the contact listener callback, and that seems to be a much more common situation, so you could try searching for problems related to removing bodies, eg. 如果您尝试破坏联系人侦听器回调中的主体,则会发生相同的问题,这似乎是更常见的情况,因此您可以尝试搜索与删除主体相关的问题,例如。 AndEngine Sprite/Box2D Body removal crashes my program with no error/exception information? AndEngine Sprite / Box2D Body删除使我的程序崩溃,没有错误/异常信息?

The typical method is to have some kind of list that can be accessed from both the main loop (where Step() is called) and inside the contact listener. 典型的方法是具有某种类型的列表,可以从主循环(在其中调用Step()的调用)和联系侦听器内部进行访问。 Make a note of what needs to be modified by adding to the list, and in the main loop immediately after the Step function, you can carry out the changes. 通过添加到列表中来记录需要修改的内容,并在紧接Step功能之后的主循环中执行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM