Andengine physicsWorld contactlistener不起作用

时间:2014-12-24 15:01:49

标签: java andengine

我不知道该怎么做,contactlistener已经不再适用了。 问题是我首先使用Simpleleveloader来加载我的精灵 - 在此期间Contactlistener没有问题。 但后来我想将Simpleleveloader改为我自己写的东西(因为我想要无穷无尽的级别,而不是预先制作的xml),而且这里的联系人听众不会工作!

以下是Leveloader: 代码:

 private void loadiLevel(int levelID)
 {
  final SimpleLevelLoader levelLoader = new SimpleLevelLoader(vbom);

  final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.01f, 0.5f);
  camera.setBounds(0, 480, 2500, 40); // here we set camera bounds
  camera.setBoundsEnabled(true);
  levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(LevelConstants.TAG_LEVEL)
  {
     public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException
     {
        final int width = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_WIDTH);
        final int height = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_HEIGHT);

        return GameScene.this;
     }
  });

  levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(TAG_ENTITY)
  {
     public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException
     {
        final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_X);
        final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_Y);
        final String type = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_TYPE);

        final Sprite levelObject;

        if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM1))
        {
           levelObject = new Sprite(x, y, resourcesManager.platform1_region, vbom);
           PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF).setUserData("platform1");
        }
        else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM2))
        {
           levelObject = new Sprite(x, y, resourcesManager.platform2_region, vbom);
           final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF);
           body.setUserData("platform2");
           physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
        }
        else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLAYER))
        {
           player = new Player(x, y, vbom, camera, physicsWorld)
           {
              @Override
              public void onDie()
              {
                 scoreText.detachSelf();
                 BackButton.detachSelf();
              }
           };
           levelObject = player;
        }
        else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_ZOMBIE))
        {
           zombie = new Zombie(x, y, vbom, camera, physicsWorld);
           levelObject = zombie;
        }
        else
        {
           throw new IllegalArgumentException();
        }

        levelObject.setCullingEnabled(true);

        return levelObject;
     }
  });

  levelLoader.loadLevelFromAsset(activity.getAssets(), "level/1.lvl");
 }

这是当前的一个(&#39;除了Contactlistener之外完全有效!&#39;): 代码:

  private void createLevel(){
  loadLevel("player",0,200);
  loadLevel("platform1",0,40   );
  loadLevel("platform1",350,40);
  loadLevel("platform1",700,40);
  loadLevel("platform2",1050,40);
  loadLevel("platform2",1400,40);
  loadLevel("platform1",1750,40);
  //loadLevel("zombie",700,200);
  }

private void loadLevel( final String tipus, final int number,final int numer){
  camera.setBounds(0, 480, 250000, 40);
  camera.setBoundsEnabled(true);
  final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.01f, 0.5f);
              final int y = 0 + numer;
              final int x = 0   +number;
              final String type = tipus;                     
              final Sprite levelObject;   

        if (type.equals("platform1"))
        {
           levelObject = new Sprite(x, y, resourcesManager.platform1_region, vbom);
           PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF).setUserData("platform1");
        }
        else if (type.equals("platform2"))
        {
           levelObject = new Sprite(x, y, resourcesManager.platform2_region, vbom);
           final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF);
           body.setUserData("platform2");
           physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
        }
        else if (type.equals("player"))
        {
           player = new Player(x, y, vbom, camera, physicsWorld)
           {
              @Override
              public void onDie()
              {
                 scoreText.detachSelf();
                 BackButton.detachSelf();
              }
           };
           levelObject = player;
        }

        else if (type.equals("zombie"))
        {
           zombie = new Zombie(x, y, vbom, camera, physicsWorld);
           levelObject = zombie;
        }
        else
        {
           throw new IllegalArgumentException();
        }

        levelObject.setCullingEnabled(true);
        attachChild(levelObject);
  }

顺便说一句,这就是我创建物理世界的方式:

private void createPhysics()
   {
  physicsWorld = new PhysicsWorld(new Vector2(0, -25.5f), true);
  physicsWorld.setContactListener(contactListener());
  registerUpdateHandler(physicsWorld);
  engine.registerUpdateHandler(new FPSLogger());
   }    

private ContactListener contactListener()
{
    ContactListener contactListener = new ContactListener()
    {
        public void beginContact(Contact contact)
        {
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();

            if (x1.getBody().getUserData() != null && x2.getBody().getUserData() != null)
            {
                if (x2.getBody().getUserData().equals("player"))
                {
                    player.increaseFootContacts();
                    if(firstTouch){
                        player.Run();
                    }
                    }
            if (x1.getBody().getUserData().equals("platform2") && x2.getBody().getUserData().equals("player"))
            {
                    engine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback()
                    {                                   
                        public void onTimePassed(final TimerHandler pTimerHandler)
                        {
                            pTimerHandler.reset();
                            engine.unregisterUpdateHandler(pTimerHandler);
                            x1.getBody().setType(BodyType.DynamicBody);

                        }
                    }));
            }

            }
        }

        public void endContact(Contact contact)
        {
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();

            if (x1.getBody().getUserData() != null && x2.getBody().getUserData() != null)
            {
                if (x2.getBody().getUserData().equals("player"))
                {
                    player.decreaseFootContacts();


                }
            }
        }

        public void preSolve(Contact contact, Manifold oldManifold)
        {

        }

        public void postSolve(Contact contact, ContactImpulse impulse)
        {

        }
    };
    return contactListener;
}
希望你能帮助我! 谢谢!

0 个答案:

没有答案