目标:
1.了解紋理
2.了解紋理區域
3.建立資源回調函數onCreateResources
4.了解BitmapTexture
5.了解Sprite
---------------------------------------------------
由于本例子使用了紋理,且通過在assets下加載,是以你需要在assets建立gfx目錄,然後放一張名為face_box.png的圖檔(注意,請保持長寬比1:1)。
如果成功,将會出現一張靜止的圖。
package org.andengine.examples;
import java.io.IOException;
import java.io.InputStream;
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.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga
*
* @author Nicolas Gramlich
* @since 11:54:51 - 03.04.2010
*/
public class SpriteExample extends SimpleBaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
// ===========================================================
// Fields
// ===========================================================
private ITexture mTexture;
private ITextureRegion mFaceTextureRegion;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
try {
this.mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/face_box.png");
}
});
this.mTexture.load();
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
} catch (IOException e) {
Debug.e(e);
}
}
@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;
/* Create the face and add it to the scene. */
final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
scene.attachChild(face);
return scene;
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
整個邏輯大緻為:
(1)從資源檔案中加載紋理
(2)使用紋理得到紋理區域
(3)使用紋理區域渲染精靈
其中:
mTexture 紋理通過兩步,實作紋理加載:
mTexture = new BitmapTexture(TextureManager, IInputStreamOpener);
mTexture.load();
mFaceTextureRegion 紋理區域,通過紋理工廠和紋理來建立
mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
精靈:Sprite 需要 : 位置的兩個參數,紋理區域,對象buffer
Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager())
-------------------------------------------------------------
擴充閱讀:
Sprite是AndEngine中最重要的概念之一,
它用于表示可以獨立繪制的東西,
它也是一個Entity。
Entity (實體)
作為一切虛拟可見的東西,實體包含了場景,包含了可見物體的通用屬性。這些屬性通過getter和setter通路和設定。
這些屬性包含下面這些元素:
float mX, mY:實體的目前位置
float mInitialX, mInitialY: 實體的初始位置
boolean mVisible :實體目前是否可見
boolean mIgnoreUpdate : 實體是否需要更新
int mZindex: 實體處于需要顯示的實體棧的位置
IEntity mParent:實體的父類(實際是個接口)
SmartList<IEntity> mChildren :實體包含的孩子們(并非繼承上的子類,更像是空間上一個大的空間包含若幹小的空間)
EntityModifierList mEntityModifiers: 一系列的實體修飾符(Modifiers)
UpdateHandlerList mUpdateHandlers:一組用于更新實體的Handlers
float mRed, mGreen, mBlue : 實體顔色
float mAlpha:透明度
float mRotation : 旋轉角度
float mRotationCenterX, mRotationCenterY: 旋轉中心
float mScale : 縮放
float mScaleCenterX, mScaleCenterY:縮放中心