的body.setTransform不能在接触监听器(andEngine和box2d)内工作



我试图移动玩家的身体,而与心灵传送接触,但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) 
        {
        }
        });

播放器类有方法

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

,它工作得很好,但没有在联系人侦听器内执行。我确信接触发生了,因为它进入了"if"块和pl.setLinearVelocity(new Vector2(-4.5f,0));是执行。

Thanks in advance

我不知道为什么不可能在联系人侦听器内使用setTransform,但我以这种方式解决了这个问题。为任务创建的类

公共类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));
}

}

然后在联系人监听器中添加新任务到列表

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

并在update

时执行
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
        }
    });

最新更新