zoukankan      html  css  js  c++  java
  • AndEngine学习:CollisionDetectionExample(碰撞检测)

    首先让我们看一下测试代码:

    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,由于代码非常简单,也就不多废话了。



  • 相关阅读:
    理解serverless无服务
    书单
    服务框架
    消息队列
    幂等设计
    MyBatis 3判断不为null
    Spring实现封装自定义注解@Trimmed清除字符串前后的空格
    Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
    Eclipse错误出现:Unable to install breakpoint in... (未能解决)
    Spring Boot中application.yml与bootstrap.yml的区别(转)
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2229460.html
Copyright © 2011-2022 走看看