1.本节要点:
1.之前绘制的动画场景都是单人物的,这节主要讲解如何实现多个AnimatedSprite在共用一个BitmapTextureAtlas下的应用
2.之前的精灵动画都没有交互性的,这节要实现触摸可以拖动精灵
3.实现屏幕的多点触摸
2.Texture的创建
1.本节用到4个精灵贴图:
要合理的利用BitmapTextureAtlas这个类进行封装这四个贴图成一个大的texture,首先了解下它的构造:
-
-
-
-
-
- public BitmapTextureAtlas(final TextureManager pTextureManager, final int pWidth, final int pHeight, final TextureOptions pTextureOptions) throws IllegalArgumentException {
- this(pTextureManager, pWidth, pHeight, BitmapTextureFormat.RGBA_8888, pTextureOptions, null);
- }
这里的BitmapTextureAtlas是指在内存中贴图集合,可以将多个图片同时放置在这里,本例子就是将四张大贴图放置期中;
另外一个值得注意的东西就是TiledTextureRegion,它可以从BitmapTextureAtlas中"扣"出一张贴图
3.细节处理
1.内部变量设置
- private static final int CAMERA_WIDTH = 800;
- private static final int CAMERA_HEIGHT = 480;
-
- private Camera mCamera;
- private RepeatingSpriteBackground mBackground;
- private BitmapTextureAtlas mTexture;
- private TiledTextureRegion mBanana;
- private TiledTextureRegion mFrog;
- private TiledTextureRegion mHelicopter;
- private TiledTextureRegion mSnapDragon;
2.四张贴图中的分辨路分别为:132x70,96x32,96x84,400x180
考虑到BitmapTextureAtlas 的长与宽只能为2的n次方大小,所以要把贴图集设置为512x256大小
- mTexture = new BitmapTextureAtlas(getTextureManager(), 512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
然后要把四张精灵贴图放置在mTexture里面就需要动下脑筋了,为了更加合理的放置,我把4张图片放置在photoshop里,然后创建一张
512x256大小的图片,尝试拖动他们进去排布,最终比较合理的排布为:
程序上实现的方法为:
- mSnapDragon = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "snapdragon_tiled.png", 0, 0, 4, 3);
- mHelicopter = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "helicopter_tiled.png", 400, 0, 2, 2);
- mBanana = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "banana_tiled.png", 0, 180, 4, 2);
- mFrog = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "frog.png", 132, 180, 3, 1);
-
-
-
- mTexture.load();
3.创建可以支持触摸拖动的AnimatedSprite精灵
自己根据需要创建TouchAnimatedSprite,并且集成AnimatedSprite的所有方法
- public class TouchAnimatedSprite extends AnimatedSprite{
-
- public TouchAnimatedSprite(float pX, float pY, float pWidth,
- float pHeight, ITiledTextureRegion pTiledTextureRegion,
- VertexBufferObjectManager pVertexBufferObjectManager) {
- super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager);
-
- }
- @Override
- public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
- float pTouchAreaLocalX, float pTouchAreaLocalY) {
-
- switch(pSceneTouchEvent.getAction()){
- case TouchEvent.ACTION_DOWN:
- case TouchEvent.ACTION_MOVE:
- this.setPosition(pSceneTouchEvent.getX()-this.getWidth()/2, pSceneTouchEvent.getY()-this.getHeight()/2);
- return true;
-
-
- }
- return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
- }
-
-
- }
有了TouchAnimatedSprite这个类,就可以书写相关的初始化工作了,在onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)方法
中添加场景等初始化资源
- Scene mScene = new Scene();
- ground(mBackground);
接着在Scene中创建4个动画精灵
- final TouchAnimatedSprite myBanana = new TouchAnimatedSprite(100, 50,64,64,mBanana, getVertexBufferObjectManager());
- myBanana.animate(150);
-
- final TouchAnimatedSprite myFrog = new TouchAnimatedSprite(100,CAMERA_HEIGHT-100,64,64,mFrog,getVertexBufferObjectManager());
- myFrog.animate(90);
-
- final TouchAnimatedSprite myHelicopter = new TouchAnimatedSprite(CAMERA_WIDTH - 140, CAMERA_HEIGHT - 100, 84,84,mHelicopter,getVertexBufferObjectManager());
- myHelicopter.animate(new long[]{100,200,150,400},0,3,true);
-
- final TouchAnimatedSprite mySnapDragon = new TouchAnimatedSprite(CAMERA_WIDTH - 100,50,85,85,mSnapDragon,getVertexBufferObjectManager());
- mySnapDragon.animate(90);
然后把四个精灵动画添加到场景中
- mScene.attachChild(myBanana);
- mScene.attachChild(myFrog);
- mScene.attachChild(myHelicopter);
- mScene.attachChild(mySnapDragon);
场景中的人物布置好了之后,接着个场景中的人如添加触摸操作支持
- mScene.registerTouchArea(myBanana);
- mScene.registerTouchArea(myFrog);
- mScene.registerTouchArea(myHelicopter);
- mScene.registerTouchArea(mySnapDragon);
- mScene.setTouchAreaBindingOnActionDownEnabled(true);
至此,整个设计已经完成了,但是屏幕只可以支持单点触摸的方式,毕竟咱们的手机大多数都是
支持多点触摸的,这样岂不是浪费并且不好玩.为了支持多点触摸可以在onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)方法
初始化mScene后添加如下方法:
- if(MultiTouch.isSupported(this))
- {
- this.mEngine.setTouchController(new MultiTouchController());
- this.mEngine.getEngineOptions().getTouchOptions().setNeedsMultiTouch(true);
- }
这样做了之后是不是很完美了呢?哈哈哈~~~~~~~~~~~~~~~~~~~~~
好了,废话少说,看看效果再说吧!
本节源代码下载地址:http://download.csdn.net/detail/cen616899547/4707665
转自:http://blog.csdn.net/cen616899547/article/details/8133266