您的位置:首页 > 其它

AndEngine Example(4):SpriteRemoveExample

2014-03-17 15:22 375 查看
目标:

1. 了解纹理 BitmapTextureAtlas

2. 纹理区域工场 BitmapTextureAtlasTextureRegionFactory

3. 锁 EngineLock

4. 移除精灵

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;

/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga
*
* @author Nicolas Gramlich
* @since 11:54:51 - 03.04.2010
*/
public class SpriteRemoveExample extends SimpleBaseGameActivity implements IOnSceneTouchListener {
// ===========================================================
// Constants
// ===========================================================

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

// ===========================================================
// Fields
// ===========================================================

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

// ===========================================================
// Constructors
// ===========================================================

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@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;
}

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}


BitmapTextureAtlas 是一种Texture(纹理)

BitmapTextureAtlas extends TextureAtlas

TextureAtlas<T extends ITextureAtlasSource> extends Texture

(上一篇中使用的是 ITexture 对象,texture = new BitmapTexture(...) 它也是是extends自Texture)

BitmapTextureAtlasTextureRegionFactory

设定AssetBasePath 路径,然后读取下面的"face_box.png" ,变成纹理,然后给mBitmapTextureAtlas,返回的则是一块纹理区域mFaceTextureRegion。

(这样就创建了 纹理 和 纹理区域)。

加锁: 关键区域需要加锁,例如移除精灵

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

engineLock.lock();

...

engineLock.unlock();

移除精灵:

1 先取消精灵与场景的绑定 detachChild

2 回收精灵dispose()

3 将对象置为空 = null

---------------------------------------------------------------------

扩展阅读:

场景 (Scenes)

AndEngine提供了一个场景类来让我们可以进行游戏场景的初始化。

Scenes拥有如下属性:

Scene mParentScene : 每个场景能够拥有可选的父场景

Scene mChildScene :每个场景能够拥有可选的子场景

SmartList<ITouchArea> mTouchAreas: (触碰区域):场景能够知道接收了用户触摸的区域

IOnSceneTouchListener mOnSceneTouchListener: 场景被触摸的监听器

IOnAreaTouchListener mOnAreaTouchListener: 区域被触摸的监听器

RunnableHandler mRunnableHandler: 场景拥有的RunnableHandler.

IBackground mBackground : 每个场景都有背景,它是黑色实心的。

boolean mOnAreaTouchTraversalBackToFront: 是否需要响应触摸事件。

Layers of graphics can be attached to a Scene as children of the Scene.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: