body.setTransform 在接触侦听器(andEngine 和 box2d)内不起作用

发布于 2024-12-02 00:53:17 字数 2204 浏览 0 评论 0原文

我试图在接触传送时移动玩家身体,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2024-12-09 00:53:17

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

public class 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

        }
    });

它工作正常。

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 {

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

}

}

then inside contack listener i just add new task to list

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

and execute it while 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

        }
    });

for me it works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文