AndEngine之DEMO学习(四)SpriteRemoveExample

package org.andengine.examples;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.widget.Toast;


public class SpriteRemoveExample extends SimpleBaseGameActivity implements IOnSceneTouchListener {

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

 private BitmapTextureAtlas mBitmapTextureAtlas;
 private ITextureRegion mFaceTextureRegion;
 private Sprite mFaceToRemove;

 @Override
 public EngineOptions onCreateEngineOptions() {
  Toast.makeText(this, "Touch the screen to safely remove the sprite.", Toast.LENGTH_LONG).show();

  final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

  return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
 }

 @Override
 public void onCreateResources() {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

  this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
  this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
  this.mBitmapTextureAtlas.load();
 }

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

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

  /* Calculate the coordinates for the face, so its centered on the camera. */
  final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
  final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

  this.mFaceToRemove = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
  scene.attachChild(this.mFaceToRemove);

  scene.setOnSceneTouchListener(this);

  return scene;
 }

 @Override
 public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
  if(this.mFaceToRemove == null) {
   return false;
  }

  final EngineLock engineLock = this.mEngine.getEngineLock();
  engineLock.lock();

  /* Now it is save to remove the entity! */
  pScene.detachChild(this.mFaceToRemove);
  this.mFaceToRemove.dispose();
  this.mFaceToRemove = null;

  engineLock.unlock();
  return true;
 }
}

这是AndEngine中的第四个例子,这个又是一个看似简单的例子,将一个精灵加入引擎,注册一个屏触摸事件,目的是要让这个精灵消失。这回登场的新成员是TextureAtlas。这个对象可以理解成有多个源(ITextureAtlasSource)组成的一幅大纹理,引擎给我们实现了BitMap的一系列源,我们也可以自定义其他种类的源。这副大纹理通过BitmapTextureAtlasTextureRegionFactory工厂,最后生产了TextureRegion提供给精灵使用,集体的类图如下:

AndEngine之DEMO学习(四)SpriteRemoveExample_第1张图片

最后一点就是在移除实体的时候需要将引擎锁定,否则和出现资源同步的问题。ok,是一个很简单的例子没有什么多说的,最近博客基本没有更新,主要是因为暗黑3的出现,我又沉迷了=。=

你可能感兴趣的:(AndEngine)