当前位置:编程学习 > JAVA >>

[AndEngine学习教程] CollisionDetection 实体碰撞检测

1.本节要点
    通过一个可操作的精灵,来与一个固定循环变化的矩形检测是否产生碰撞,当产生碰撞的时候,矩形的颜色为红色,
   否则矩形的颜色为绿色
2.新学内容
 1.学会使用SmoothCamera类,这个类是一个平滑的照相机类,可以满足背景自动填充,平滑过渡的效果.先看看它的构成吧:
[java] 
public class SmoothCamera extends ZoomCamera  
是继承于SmoothCamera的类,而SmoothCamera从文字上就可以看出它是一个支持缩放效果的类,因此使用起来就很方便了,
构造方法为:
[java]
public SmoothCamera(final float pX, final float pY, final float pWidth, final float pHeight, final float pMaxVelocityX, final float pMaxVelocityY, final float pMaxZoomFactorChange) { 
        super(pX, pY, pWidth, pHeight); 
        this.mMaxVelocityX = pMaxVelocityX; 
        this.mMaxVelocityY = pMaxVelocityY; 
        this.mMaxZoomFactorChange = pMaxZoomFactorChange; 
 
        this.mTargetCenterX = this.getCenterX(); 
        this.mTargetCenterY = this.getCenterY(); 
 
        this.mTargetZoomFactor = 1.0f; 
    } 

为了体现效果,我刻意的把背景图片做成720*640大小,而手机的屏幕分辨率大小为800*480,但是依然可以看到全屏的效果.相当不错的哦!
以下的是我使用到的背景图片:

 
 2.为场景Scene注册刷新句柄,用来监听每次实体间是否产生碰撞.然后进行相应的事物处理,具体调用方法为:
[java] 
mScene.registerUpdateHandler(new IUpdateHandler()); 

3.内部代码初始化设计
1.本节使用到4个实体,一个是人脸,另外一个是矩形,最后2个是控制器,内部成员变量设计为:
[java] 
private static final int CAMERA_WIDTH = 800; 
    private static final int CAMERA_HEIGHT = 480; 
     
    private SmoothCamera mCamera; 
    private SpriteBackground mBackground; 
    private TiledTextureRegion mControlBaseRegion; 
    private TiledTextureRegion mControlKnobRegion; 
    private TiledTextureRegion mFaceRegion; 

2.引擎资源的加载
[java] 
@Override 
    public EngineOptions onCreateEngineOptions() { 
        // TODO Auto-generated method stub 
        mCamera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 10, 10, 3); 
        EngineOptions mEngineOptions = new  EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),mCamera); 
        return mEngineOptions; 
    } 
 
    @Override 
    public void onCreateResources( 
            OnCreateResourcesCallback pOnCreateResourcesCallback) 
            throws Exception { 
        // TODO Auto-generated method stub 
        BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(), 1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
        final TiledTextureRegion mBackgroundRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "bg.png", 0, 0, 1, 1); 
        final Sprite mBackgroundSprite = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, mBackgroundRegion, getVertexBufferObjectManager()); 
        mBackground = new SpriteBackground(mBackgroundSprite); 
         
        mControlBaseRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "onscreen_control_base.png", 720, 0, 1, 1); 
        mControlKnobRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "onscreen_control_knob.png", 848, 0, 1, 1); 
        mFaceRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "face_box.png", 912, 0, 1, 1); 
         
        mTexture.load(); 
        pOnCreateResourcesCallback.onCreateResourcesFinished(); 
    } 

4.场景设计
1.添加多点触摸支持和场景基础设施
[java] 
if(MultiTouch.isSupported(this)){ 
            this.mEngine.setTouchController(new MultiTouchController());//多点触摸       
        } 
         
        Scene mScene = new Scene(); 
        mScene.setBackground(mBackground); 
         
        final float centerX = (CAMERA_WIDTH - mFaceRegion.getWidth()) / 2; 
        final float centerY = (CAMERA_HEIGHT - mFaceRegion.getHeight()) / 2; 

2.构建相应的精灵任务:
    2.1 布置人脸:           
[java] 
final MySprite mFace = new MySprite(centerX - 50, centerY, 80, 80, mFaceRegion, getVertexBufferObjectManager()); 
         
   2.2 不知受碰撞检测的矩形
[java] 
final Rectangle mRectangle = new Rectangle(centerX + 10, centerY, 80,80,getVertexBufferObjectManager()); 
        mRectangle.registerEntityModifier(new LoopEntityModifier(new ParallelEntityModifier( 
                new ScaleModifier(2, 0.5f, 2.0f),new ScaleModifier(2, 2.0f,1.0f),new RotationModifier(4,0.0f,360.0f) ))); 
         
     添加相应的modifier后,矩形就会动态的变幻了.

   2.3 布置移动控制器和方向控制器
      
[java] 
final AnalogOnScreenControl mSpeedController = new AnalogOnScreenControl(30,CAMERA_HEIGHT -  mControlBaseRegion.getHeight() - 20, 
                mCamera, mControlBaseRegion, mControlKnobRegion, 0.1f, 100,getVertexBufferObjectManager(), 
                new IAnalogOnScreenControlListener(){ 
 
                    @Override 
         

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,