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)
{
}
});
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));被执行。
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么不可能在联系人侦听器中使用 setTransform 但我以这种方式解决了这个问题。为任务创建类
public class moveBodyTask {
}
然后在接触侦听器中我只需将新任务添加到列表中
并在更新时执行它,
它工作正常。
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.