您的位置:首页 > 其它

AndEngine Example(3):SpriteExample

2014-03-17 10:51 246 查看
目标:

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:缩放中心
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: