andengine编程之sprite(一)

sprite可以说是游戏中的主角,我们建立各种图片,人物的显示都离不开它。sprite类很丰富,如果看过源代码的不难发现,可用的类型很多:



你可以有多种选择,可以直接使用AnimatedSprite来绘制人物动画,可以使用ButtonSprite来绘制按钮,也可以继承Sprite类实现自己的精灵。因此,我们将分期介绍Sprite的使用。

最简单的Sprite实现——AnimatedSprite人物动画

对于如果建立andengine工程及实现方法的写法不再解释,还有问题的朋友看前文。

看一下onCreateResources方法的资源加载,这里的背景图片采用了另一种方式来创建,至于两者的区别也很简单:
它不支持拖动和缩放哦!当然位置也不用怎么考虑,直接从左上角开始绘制。
如果你的整个背景需要有缩放,移动效果,比如很大的一张地图,我们想左右拖拽,或者缩放的话,采用RepeatingSpriteBackground是不支持的。(场景中其它的精灵还是可以被缩放的,唯独背景没变化)

到底要怎么使用,还是根据你的需要来选择吧。

[java]  view plain copy
  1. public void onCreateResources(  
  2.         OnCreateResourcesCallback pOnCreateResourcesCallback)  
  3.         throws Exception {  
  4.   
  5.     // 采用RepeatingSpriteBackground来加载背景  
  6.     // 480,800:屏幕的大小  
  7.     // AssetBitmapTextureAtlasSource.create(this.getAssets(),"background.png");  
  8.     this.background = new RepeatingSpriteBackground(800480,  
  9.             getTextureManager(), AssetBitmapTextureAtlasSource.create(  
  10.                     this.getAssets(), "background.png"),  
  11.             getVertexBufferObjectManager());  
  12.   
  13.     // 加载一下精灵图片,该图片取自其AndEngineExamples自带例子中  
  14.     BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(  
  15.             getTextureManager(), 6432, TextureOptions.DEFAULT);  
  16.     // 0,0:代表图片从哪个位置开始取起始点剪裁  
  17.     // 2,1:2代表一行有2个元素,1代表一共有1行  
  18.     mSpriteTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory  
  19.             .createTiledFromAsset(mBitmapTextureAtlas, this,  
  20.                     "face_circle_tiled.png"0021);  
  21.     mBitmapTextureAtlas.load();  
  22.   
  23.     pOnCreateResourcesCallback.onCreateResourcesFinished();  
  24. }  


再看一下onCreateScene方法创建的场景,注意背景的加载方式与之前的变化
[java]  view plain copy
  1. public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  2.         throws Exception {  
  3.     Scene mScene = new Scene();  
  4.   
  5.     // 创建精灵  
  6.     // 100,100:精灵的初始坐标  
  7.     // mSpriteTiledTextureRegion:精灵的帧序列图片,注意它的顺序,应是从左到右,从上到下的顺序,要不就乱套了  
  8.     AnimatedSprite face = new AnimatedSprite(100100,  
  9.             mSpriteTiledTextureRegion, getVertexBufferObjectManager());  
  10.     // 为精灵建立动画机制,当然这个设置也可以写在AnimatedSprite构造方法的参数中  
  11.     // new long[] { 200, 200 }:每一帧保持的时间,比如说我这里的笑脸只有两帧,则分别为他们设置200ms一帧  
  12.     // 注意:这个数组的长度一定要和你的帧序列的数量保持一致!  
  13.     // 0,1:第一帧是哪个,最后一帧是哪个,然后它就从0到1播放下去  
  14.     // true:是否循环播放,如果你的动画不是一次性就播完的话,就设置这里为true吧  
  15.     face.animate(new long[] { 200200 }, 01true);  
  16.   
  17.     // 设置背景  
  18.     mScene.setBackground(background);  
  19.     mScene.attachChild(face);  
  20.   
  21.     pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  22. }  


最后运行一下吧

以下是源代码:
[java]  view plain copy
  1. package com.testsprite;  
  2.   
  3. import org.andengine.engine.camera.Camera;  
  4. import org.andengine.engine.options.EngineOptions;  
  5. import org.andengine.engine.options.EngineOptions.ScreenOrientation;  
  6. import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;  
  7. import org.andengine.entity.scene.Scene;  
  8. import org.andengine.entity.scene.background.RepeatingSpriteBackground;  
  9. import org.andengine.entity.sprite.AnimatedSprite;  
  10. import org.andengine.opengl.texture.TextureOptions;  
  11. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;  
  12. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;  
  13. import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;  
  14. import org.andengine.opengl.texture.region.TiledTextureRegion;  
  15. import org.andengine.ui.activity.BaseGameActivity;  
  16.   
  17. public class TestSprite extends BaseGameActivity {  
  18.     private static final int CAMERA_WIDTH = 800;  
  19.     private static final int CAMERA_HEIGHT = 480;  
  20.   
  21.     private RepeatingSpriteBackground background;  
  22.   
  23.     private TiledTextureRegion mSpriteTiledTextureRegion;  
  24.   
  25.     public EngineOptions onCreateEngineOptions() {  
  26.         Camera mCamera = new Camera(00, CAMERA_WIDTH, CAMERA_HEIGHT);  
  27.         EngineOptions mEngineOptions = new EngineOptions(true,  
  28.                 ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),  
  29.                 mCamera);  
  30.         return mEngineOptions;  
  31.     }  
  32.   
  33.     public void onCreateResources(  
  34.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  35.             throws Exception {  
  36.   
  37.         // 采用RepeatingSpriteBackground来加载背景  
  38.         // 480,800:屏幕的大小  
  39.         // AssetBitmapTextureAtlasSource.create(this.getAssets(),"background.png");  
  40.         this.background = new RepeatingSpriteBackground(800480,  
  41.                 getTextureManager(), AssetBitmapTextureAtlasSource.create(  
  42.                         this.getAssets(), "background.png"),  
  43.                 getVertexBufferObjectManager());  
  44.   
  45.         // 加载一下精灵图片,该图片取自其AndEngineExamples自带例子中  
  46.         BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(  
  47.                 getTextureManager(), 6432, TextureOptions.DEFAULT);  
  48.         // 0,0:代表图片从哪个位置开始取起始点剪裁  
  49.         // 2,1:2代表一行有2个元素,1代表一共有1行  
  50.         mSpriteTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory  
  51.                 .createTiledFromAsset(mBitmapTextureAtlas, this,  
  52.                         "face_circle_tiled.png"0021);  
  53.         mBitmapTextureAtlas.load();  
  54.   
  55.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  56.     }  
  57.   
  58.     public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  59.             throws Exception {  
  60.         Scene mScene = new Scene();  
  61.   
  62.         // 创建精灵  
  63.         // 100,100:精灵的初始坐标  
  64.         // mSpriteTiledTextureRegion:精灵的帧序列图片,注意它的顺序,应是从左到右,从上到下的顺序,要不就乱套了  
  65.         AnimatedSprite face = new AnimatedSprite(100100,  
  66.                 mSpriteTiledTextureRegion, getVertexBufferObjectManager());  
  67.         // 为精灵建立动画机制,当然这个设置也可以写在AnimatedSprite构造方法的参数中  
  68.         // new long[] { 200, 200 }:每一帧保持的时间,比如说我这里的笑脸只有两帧,则分别为他们设置200ms一帧  
  69.         // 注意:这个数组的长度一定要和你的帧序列的数量保持一致!  
  70.         // 0,1:第一帧是哪个,最后一帧是哪个,然后它就从0到1播放下去  
  71.         // true:是否循环播放,如果你的动画不是一次性就播完的话,就设置这里为true吧  
  72.         face.animate(new long[] { 200200 }, 01true);  
  73.   
  74.         // 设置背景  
  75.         mScene.setBackground(background);  
  76.         mScene.attachChild(face);  
  77.   
  78.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  79.     }  
  80.   
  81.     public void onPopulateScene(Scene pScene,  
  82.             OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {  
  83.         pOnPopulateSceneCallback.onPopulateSceneFinished();  
  84.   
  85.     }  
  86. }  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值