sprite可以说是游戏中的主角,我们建立各种图片,人物的显示都离不开它。sprite类很丰富,如果看过源代码的不难发现,可用的类型很多:
你可以有多种选择,可以直接使用AnimatedSprite来绘制人物动画,可以使用ButtonSprite来绘制按钮,也可以继承Sprite类实现自己的精灵。因此,我们将分期介绍Sprite的使用。
最简单的Sprite实现——AnimatedSprite人物动画
对于如果建立andengine工程及实现方法的写法不再解释,还有问题的朋友看前文。
看一下onCreateResources方法的资源加载,这里的背景图片采用了另一种方式来创建,至于两者的区别也很简单:
它不支持拖动和缩放哦!当然位置也不用怎么考虑,直接从左上角开始绘制。
如果你的整个背景需要有缩放,移动效果,比如很大的一张地图,我们想左右拖拽,或者缩放的话,采用RepeatingSpriteBackground是不支持的。(场景中其它的精灵还是可以被缩放的,唯独背景没变化)
到底要怎么使用,还是根据你的需要来选择吧。
- public void onCreateResources(
- OnCreateResourcesCallback pOnCreateResourcesCallback)
- throws Exception {
- // 采用RepeatingSpriteBackground来加载背景
- // 480,800:屏幕的大小
- // AssetBitmapTextureAtlasSource.create(this.getAssets(),"background.png");
- this.background = new RepeatingSpriteBackground(800, 480,
- getTextureManager(), AssetBitmapTextureAtlasSource.create(
- this.getAssets(), "background.png"),
- getVertexBufferObjectManager());
- // 加载一下精灵图片,该图片取自其AndEngineExamples自带例子中
- BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(
- getTextureManager(), 64, 32, TextureOptions.DEFAULT);
- // 0,0:代表图片从哪个位置开始取起始点剪裁
- // 2,1:2代表一行有2个元素,1代表一共有1行
- mSpriteTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
- .createTiledFromAsset(mBitmapTextureAtlas, this,
- "face_circle_tiled.png", 0, 0, 2, 1);
- mBitmapTextureAtlas.load();
- pOnCreateResourcesCallback.onCreateResourcesFinished();
- }
再看一下onCreateScene方法创建的场景,注意背景的加载方式与之前的变化
- public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
- throws Exception {
- Scene mScene = new Scene();
- // 创建精灵
- // 100,100:精灵的初始坐标
- // mSpriteTiledTextureRegion:精灵的帧序列图片,注意它的顺序,应是从左到右,从上到下的顺序,要不就乱套了
- AnimatedSprite face = new AnimatedSprite(100, 100,
- mSpriteTiledTextureRegion, getVertexBufferObjectManager());
- // 为精灵建立动画机制,当然这个设置也可以写在AnimatedSprite构造方法的参数中
- // new long[] { 200, 200 }:每一帧保持的时间,比如说我这里的笑脸只有两帧,则分别为他们设置200ms一帧
- // 注意:这个数组的长度一定要和你的帧序列的数量保持一致!
- // 0,1:第一帧是哪个,最后一帧是哪个,然后它就从0到1播放下去
- // true:是否循环播放,如果你的动画不是一次性就播完的话,就设置这里为true吧
- face.animate(new long[] { 200, 200 }, 0, 1, true);
- // 设置背景
- mScene.setBackground(background);
- mScene.attachChild(face);
- pOnCreateSceneCallback.onCreateSceneFinished(mScene);
- }
最后运行一下吧
以下是源代码:
- package com.testsprite;
- import org.andengine.engine.camera.Camera;
- import org.andengine.engine.options.EngineOptions;
- import org.andengine.engine.options.EngineOptions.ScreenOrientation;
- import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
- import org.andengine.entity.scene.Scene;
- import org.andengine.entity.scene.background.RepeatingSpriteBackground;
- import org.andengine.entity.sprite.AnimatedSprite;
- 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.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
- import org.andengine.opengl.texture.region.TiledTextureRegion;
- import org.andengine.ui.activity.BaseGameActivity;
- public class TestSprite extends BaseGameActivity {
- private static final int CAMERA_WIDTH = 800;
- private static final int CAMERA_HEIGHT = 480;
- private RepeatingSpriteBackground background;
- private TiledTextureRegion mSpriteTiledTextureRegion;
- public EngineOptions onCreateEngineOptions() {
- Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
- EngineOptions mEngineOptions = new EngineOptions(true,
- ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
- mCamera);
- return mEngineOptions;
- }
- public void onCreateResources(
- OnCreateResourcesCallback pOnCreateResourcesCallback)
- throws Exception {
- // 采用RepeatingSpriteBackground来加载背景
- // 480,800:屏幕的大小
- // AssetBitmapTextureAtlasSource.create(this.getAssets(),"background.png");
- this.background = new RepeatingSpriteBackground(800, 480,
- getTextureManager(), AssetBitmapTextureAtlasSource.create(
- this.getAssets(), "background.png"),
- getVertexBufferObjectManager());
- // 加载一下精灵图片,该图片取自其AndEngineExamples自带例子中
- BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(
- getTextureManager(), 64, 32, TextureOptions.DEFAULT);
- // 0,0:代表图片从哪个位置开始取起始点剪裁
- // 2,1:2代表一行有2个元素,1代表一共有1行
- mSpriteTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
- .createTiledFromAsset(mBitmapTextureAtlas, this,
- "face_circle_tiled.png", 0, 0, 2, 1);
- mBitmapTextureAtlas.load();
- pOnCreateResourcesCallback.onCreateResourcesFinished();
- }
- public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
- throws Exception {
- Scene mScene = new Scene();
- // 创建精灵
- // 100,100:精灵的初始坐标
- // mSpriteTiledTextureRegion:精灵的帧序列图片,注意它的顺序,应是从左到右,从上到下的顺序,要不就乱套了
- AnimatedSprite face = new AnimatedSprite(100, 100,
- mSpriteTiledTextureRegion, getVertexBufferObjectManager());
- // 为精灵建立动画机制,当然这个设置也可以写在AnimatedSprite构造方法的参数中
- // new long[] { 200, 200 }:每一帧保持的时间,比如说我这里的笑脸只有两帧,则分别为他们设置200ms一帧
- // 注意:这个数组的长度一定要和你的帧序列的数量保持一致!
- // 0,1:第一帧是哪个,最后一帧是哪个,然后它就从0到1播放下去
- // true:是否循环播放,如果你的动画不是一次性就播完的话,就设置这里为true吧
- face.animate(new long[] { 200, 200 }, 0, 1, true);
- // 设置背景
- mScene.setBackground(background);
- mScene.attachChild(face);
- pOnCreateSceneCallback.onCreateSceneFinished(mScene);
- }
- public void onPopulateScene(Scene pScene,
- OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
- pOnPopulateSceneCallback.onPopulateSceneFinished();
- }
- }