0

我正在使用 AndEngine 和 Box2d 制作游戏。精灵/身体随机生成并放置在场景中。我知道我可以计算这些区域并查看它们是否重叠,但这似乎很费力。有没有一种简单的方法来检测一个精灵/身体是否正在另一个内部创建?就像是:

boolean outside = false;
while(!outside){
try{
randx = random.nextInt(650) + 25;
randy = random.nextInt(400) + 25;
sprite = new Sprite(randx,randy,spriteTR,getVertexBufferObjectManager())
scene.attachChild(sprite);
outside = true;
}catch(){}

或者尝试/捕获不起作用?

4

1 回答 1

0

是的,您需要做的是创建两个精灵,然后进行碰撞检测。有一种简单的方法可以做到这一点,而通过 box2d 有一种更复杂的方法。

简单的方法:

sprite1.collidesWith(sprite2);

如果两个精灵相互接触,它应该返回 true。

通过 Box2d,您将使用称为ContactListener的东西来检测碰撞。

            ContactListener contactListener = new ContactListener()
            {
                    @Override
                    public void beginContact(Contact contact)
                    {   
                    }

                    @Override
                    public void endContact(Contact contact)
                    {  
                    }

                    @Override
                    public void preSolve(Contact contact, Manifold oldManifold)
                    {
                    }

                    @Override
                    public void postSolve(Contact contact, ContactImpulse impulse)
                    {  
                    }
            };

Inside beginContact, you will want to do your code for randomizing the locations of the sprites again (or whatever other algorithm you want to employ). The other methods give you additional functionality, for example you can employ endContact when the two objects stop overlapping each other.

For more information and a detailed tutorial see: http://www.andengine.org/forums/tutorials/contact-listener-t5903.html

于 2012-07-05T12:05:36.750 回答