【问题标题】:body.setTransform does not work inside contact listener (andEngine and box2d)body.setTransform 在接触侦听器(andEngine 和 box2d)内不起作用
【发布时间】:2011-11-06 22:15:33
【问题描述】:

我正在尝试在与传送接触时移动玩家身体,但未执行 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));被执行。

提前致谢

【问题讨论】:

    标签: java android box2d andengine


    【解决方案1】:

    我不知道为什么在联系监听器中使用 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));
    

    并在更新时执行它

    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
    
            }
        });
    

    对我来说效果很好。

    【讨论】:

    • 虽然您不知道原因,但您已经提供了正确的解决方案:setTransform() 在联系人监听器中不起作用,因为执行联系人监听器时 box2d 的世界被锁定。因此,您不能手动移动实体。将所需任务添加到列表并在更新侦听器中执行它们是正确的解决方案。
    • Marco,你能告诉我这个规则对于改变体型是否相同?例如从动态到静态,我们是否也需要在更新时这样做?到目前为止,我还没有经历过崩溃,但想确定一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多