I’m trying to move player body while contact with teleport but setTransform isn’t executed.This is my contact listener
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 class has method
public void moveTo(int x, int y)
{
body.setTransform(new Vector2(x/32,y/32), 0);
}
and it works fine but isn’t executed inside contact listener. And I’m sure contact is occured because it enters the “if” block and pl.setLinearVelocity(new Vector2(-4.5f,0)); is executed.
Thanks in advance
I don’t know why it’s impossieble to use setTransform inside contact listener but I solved this problem in this way. Created class for tasks
public class moveBodyTask {
}
then inside contack listener i just add new task to list
and execute it while update
for me it works fine.