1.本节要点
通过一个可操作的精灵,来与一个固定循环变化的矩形检测是否产生碰撞,当产生碰撞的时候,矩形的颜色为红色,
否则矩形的颜色为绿色
2.新学内容
1.学会使用SmoothCamera类,这个类是一个平滑的照相机类,可以满足背景自动填充,平滑过渡的效果.先看看它的构成吧:
public class SmoothCamera extends ZoomCamera是继承于SmoothCamera的类,而SmoothCamera从文字上就可以看出它是一个支持缩放效果的类,因此使用起来就很方便了,
构造方法为:
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; }
以下的是我使用到的背景图片:
2.为场景Scene注册刷新句柄,用来监听每次实体间是否产生碰撞.然后进行相应的事物处理,具体调用方法为:
mScene.registerUpdateHandler(new IUpdateHandler());
3.内部代码初始化设计
1.本节使用到4个实体,一个是人脸,另外一个是矩形,最后2个是控制器,内部成员变量设计为:
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;
@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(); }
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.1 布置人脸:
final MySprite mFace = new MySprite(centerX - 50, centerY, 80, 80, mFaceRegion, getVertexBufferObjectManager());2.2 不知受碰撞检测的矩形
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 布置移动控制器和方向控制器
final AnalogOnScreenControl mSpeedController = new AnalogOnScreenControl(30,CAMERA_HEIGHT - mControlBaseRegion.getHeight() - 20, mCamera, mControlBaseRegion, mControlKnobRegion, 0.1f, 100,getVertexBufferObjectManager(), new IAnalogOnScreenControlListener(){ @Override public void onControlChange( BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY) { // TODO Auto-generated method stub if(pValueX == 0 && pValueY == 0){ mFace.setVelocity(0, 0); } else { mFace.setVelocity(pValueX*100, pValueY*100); } } @Override public void onControlClick( AnalogOnScreenControl pAnalogOnScreenControl) { // TODO Auto-generated method stub } }); final AnalogOnScreenControl mRotationController = new AnalogOnScreenControl(CAMERA_WIDTH - mControlBaseRegion.getWidth() - 30, CAMERA_HEIGHT - mControlBaseRegion.getHeight() - 20, mCamera, mControlBaseRegion, mControlKnobRegion, 0.1f, 100,getVertexBufferObjectManager(), new IAnalogOnScreenControlListener(){ @Override public void onControlChange( BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY) { // TODO Auto-generated method stub if(pValueX == 0 && pValueY == 0){ mFace.setRotation(0.0f); } else { mFace.setRotation(MathUtils.radToDeg(MathUtils.atan2(pValueY, pValueX))); } } @Override public void onControlClick( AnalogOnScreenControl pAnalogOnScreenControl) { // TODO Auto-generated method stub } });2.4 把所有角色搬到场景中
mSpeedController.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); mRotationController.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); mScene.attachChild(mFace); mScene.attachChild(mRectangle); mScene.setChildScene(mSpeedController); mSpeedController.setChildScene(mRotationController); pOnCreateSceneCallback.onCreateSceneFinished(mScene);
mScene.registerUpdateHandler(new IUpdateHandler(){ @Override public void onUpdate(float pSecondsElapsed) { // TODO Auto-generated method stub if(mRectangle.collidesWith(mFace)){ mRectangle.setColor(1.0f, 0.0f, 0.0f); } else { mRectangle.setColor(0.0f,1.0f,0.0f); } } @Override public void reset() { // TODO Auto-generated method stub } });有了这个监听后,场景每次的刷新都可以添加处理事件了.所以基本上就完成任务了,呵呵
6.辅助自定义类
public class MySprite extends Sprite{ public MySprite(float pX, float pY, float pWidth, float pHeight, ITextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) { super(pX, pY, pWidth, pHeight, pTextureRegion, pVertexBufferObjectManager); // TODO Auto-generated constructor stub } private float mVelocityX = 0; private float mVelocityY = 0; @Override protected void onManagedUpdate(float pSecondsElapsed) { // TODO Auto-generated method stub this.mX += mVelocityX * pSecondsElapsed; this.mY += mVelocityY * pSecondsElapsed; this.setPosition(mX, mY); super.onManagedUpdate(pSecondsElapsed); } void setVelocity(float vX, float vY){ mVelocityX = vX; mVelocityY = vY; } }
通过碰撞检测运行的结果如下:
本例子源代码:http://download.csdn.net/detail/cen616899547/4737237