body.setTransform在联系人侦听器(andEngine和box2d)中不起作用

我试图移动玩家身体,而与teleport联系,但setTransform不执行。这是我的联系人的听众

mPhysicsWorld.setContactListener(new ContactListener() { @Override public void beginContact(Contact contact) { final Fixture fixtureA = contact.getFixtureA(); final Body bodyA = fixtureA.getBody(); final Fixture fixtureB = contact.getFixtureB(); final Body bodyB = fixtureB.getBody(); if(bodyA.getUserData().equals("Player") || bodyB.getUserData().equals("Player") ) { for(int i = 0; i < telList.size(); i++) { if(bodyA.getUserData() == telList.get(i)) { Teleport tl = telList.get(i); if(tl.look.getX() > pl.look.getX()) { pl.moveTo(150, 320); pl.setLinearVelocity(new Vector2(-4.5f,0)); }else { pl.moveTo(150, 320); pl.setLinearVelocity(new Vector2(4.5f,0)); } break; }else if(bodyB.getUserData() == telList.get(i)) { Teleport tl = telList.get(i); if(tl.look.getX() > pl.look.getX()) { pl.moveTo(150, 320); pl.setLinearVelocity(new Vector2(-4.5f,0)); }else { pl.moveTo(150, 320); pl.setLinearVelocity(new Vector2(4.5f,0)); } break; } } } } @Override public void endContact(Contact contact) { } }); 

Player类有方法

 public void moveTo(int x, int y) { body.setTransform(new Vector2(x/32,y/32), 0); } 

它工作正常,但不是在联系侦听器内执行。 我确定发生了联系,因为它进入了“if”块和pl.setLinearVelocity(new Vector2(-4.5f,0)); 被执行。

提前致谢

Solutions Collecting From Web of "body.setTransform在联系人侦听器(andEngine和box2d)中不起作用"

我不知道为什么在接触监听器中使用setTransform是不可能的,但是我用这种方法解决了这个问题。 创build任务类

公共类moveBodyTask {

 Player pl; float x; float y; boolean direction; moveBodyTask(Player b, float x1, float y1, boolean d) { pl = b; x = x1; y = y1; direction = d; } public void move() { pl.moveTo(x, y); if(direction) pl.setLinearVelocity(new Vector2(5,0)); else pl.setLinearVelocity(new Vector2(-5,0)); } 

}

然后里面contack监听器我只是添加新的任务列表

  taskList.add(new moveBodyTask(pl, x+TILE_SIZE, y, true)); 

并在更新时执行它

 scene.registerUpdateHandler(new IUpdateHandler() { @Override public void onUpdate(float pSecondsElapsed) { if(!taskList.isEmpty()) { for(int i = 0; i < taskList.size(); i++) { taskList.get(i).move(); } taskList.clear(); } } @Override public void reset() { // TODO Auto-generated method stub } }); 

对我来说,它工作正常。