您的位置:首页 > 其它

AndEngine学习:CollisionDetectionExample(碰撞检测)

2011-10-30 19:48 423 查看
首先让我们看一下测试代码:

public class CollisionDetectionTestActivity extends BaseGameActivity
{
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

private Camera mCamera;

@Override
public Engine onLoadEngine()
{
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));

return engine;
}

@Override
public void onLoadResources()
{

}

@Override
public Scene onLoadScene()
{
this.mEngine.registerUpdateHandler(new FPSLogger());

final Scene scene = new Scene();
scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

int centerX = CAMERA_WIDTH / 2;
int centerY = CAMERA_HEIGHT / 2;

final LoopEntityModifier entityModifier =
new LoopEntityModifier(
new ParallelEntityModifier(
new RotationModifier(12, 0, 360)
));

final Rectangle rectangle = new Rectangle(centerX - 50, centerY - 16, 64, 64);
rectangle.registerEntityModifier(entityModifier);

final Line line = new Line(centerX, centerY, 32, 32);
line.registerEntityModifier(entityModifier);

scene.attachChild(rectangle);
scene.attachChild(line);

scene.registerUpdateHandler(new IUpdateHandler(){

@Override
public void onUpdate(float pSecondsElapsed)
{
if(rectangle.collidesWith(line))
{
rectangle.setColor(1, 0, 0);
}
else
{
rectangle.setColor(0, 1, 0);
}

if(line.collidesWith(rectangle))
{
line.setColor(1, 0, 0);
}
else
{
line.setColor(0, 1, 0);
}
}

@Override
public void reset()
{

}});

return scene;
}

@Override
public void onLoadComplete()
{

}

}


  OK,让我们逐一解释。

  

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

private Camera mCamera;

@Override
public Engine onLoadEngine()
{
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));

return engine;
}

@Override
public void onLoadResources()
{

}

@Override
public void onLoadComplete()
{

}


  这些应该没什么问题。

  我们主要看

public Scene onLoadScene();


  这个函数。

  在这个函数里边我们定义了一个矩形和一条直线,并且让他们两个旋转

final LoopEntityModifier entityModifier =
new LoopEntityModifier(
new ParallelEntityModifier(
new RotationModifier(12, 0, 360)
));

final Rectangle rectangle = new Rectangle(centerX - 50, centerY - 16, 64, 64);
rectangle.registerEntityModifier(entityModifier);

final Line line = new Line(centerX, centerY, 32, 32);
line.registerEntityModifier(entityModifier);

scene.attachChild(rectangle);
scene.attachChild(line);


  然后就是碰撞检测的部分了,当碰撞的时候,让矩形和直线都改变颜色。

scene.registerUpdateHandler(new IUpdateHandler(){

@Override
public void onUpdate(float pSecondsElapsed)
{
if(rectangle.collidesWith(line))
{
rectangle.setColor(1, 0, 0);
}
else
{
rectangle.setColor(0, 1, 0);
}

if(line.collidesWith(rectangle))
{
line.setColor(1, 0, 0);
}
else
{
line.setColor(0, 1, 0);
}
}


  OK,由于代码非常简单,也就不多废话了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: