[AndEngine学习教程] 第5节 可触摸移动的Sprite

本文介绍如何在共享BitmapTextureAtlas的基础上实现多个AnimatedSprite的显示,并通过TouchAnimatedSprite类实现触摸拖动功能,同时支持多点触摸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/cen616899547/article/details/8133266

 

1.本节要点:

1.之前绘制的动画场景都是单人物的,这节主要讲解如何实现多个AnimatedSprite在共用一个BitmapTextureAtlas下的应用

2.之前的精灵动画都没有交互性的,这节要实现触摸可以拖动精灵

3.实现屏幕的多点触摸

2.Texture的创建

1.本节用到4个精灵贴图:



要合理的利用BitmapTextureAtlas这个类进行封装这四个贴图成一个大的texture,首先了解下它的构造:

  1. /**
  2. * Uses {@link BitmapTextureFormat#RGBA_8888}.
  3. *
  4. * @param pTextureOptions the (quality) settings of this {@link BitmapTextureAtlas}.
  5. */
  6. public BitmapTextureAtlas(final TextureManager pTextureManager,final int pWidth,final int pHeight,final TextureOptions pTextureOptions) throws IllegalArgumentException {
  7. this(pTextureManager, pWidth, pHeight, BitmapTextureFormat.RGBA_8888, pTextureOptions,null);
  8. }
/**
* Uses {@link BitmapTextureFormat#RGBA_8888}.
 *
* @param pTextureOptions the (quality) settings of this {@link BitmapTextureAtlas}.
 */
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.内部变量设置

  1. private staticfinal int CAMERA_WIDTH =800;
  2. private staticfinal int CAMERA_HEIGHT =480;
  3. private Camera mCamera;
  4. private RepeatingSpriteBackground mBackground;
  5. private BitmapTextureAtlas mTexture;
  6. private TiledTextureRegion mBanana;
  7. private TiledTextureRegion mFrog;
  8. private TiledTextureRegion mHelicopter;
  9. private TiledTextureRegion mSnapDragon;
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大小

  1. mTexture = new BitmapTextureAtlas(getTextureManager(),512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
     mTexture = new BitmapTextureAtlas(getTextureManager(), 512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);	

然后要把四张精灵贴图放置在mTexture里面就需要动下脑筋了,为了更加合理的放置,我把4张图片放置在photoshop里,然后创建一张

512x256大小的图片,尝试拖动他们进去排布,最终比较合理的排布为:


程序上实现的方法为:

  1. mSnapDragon = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture,this, "snapdragon_tiled.png",0, 0,4, 3);
  2. mHelicopter = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture,this, "helicopter_tiled.png",400, 0,2, 2);
  3. mBanana = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture,this, "banana_tiled.png",0, 180,4, 2);
  4. mFrog = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture,this, "frog.png",132, 180,3, 1);
  5. mTexture.load();
                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的所有方法

  1. public class TouchAnimatedSpriteextends AnimatedSprite{
  2. public TouchAnimatedSprite(float pX,float pY, float pWidth,
  3. float pHeight, ITiledTextureRegion pTiledTextureRegion,
  4. VertexBufferObjectManager pVertexBufferObjectManager) {
  5. super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager);
  6. // TODO Auto-generated constructor stub
  7. }
  8. @Override
  9. public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
  10. float pTouchAreaLocalX,float pTouchAreaLocalY) {
  11. // TODO Auto-generated method stub
  12. switch(pSceneTouchEvent.getAction()){
  13. case TouchEvent.ACTION_DOWN:
  14. case TouchEvent.ACTION_MOVE:
  15. this.setPosition(pSceneTouchEvent.getX()-this.getWidth()/2, pSceneTouchEvent.getY()-this.getHeight()/2);
  16. return true;
  17. }
  18. return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
  19. }
  20. }
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);
			// TODO Auto-generated constructor stub
		}
		@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
			// TODO Auto-generated method stub
			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)方法

中添加场景等初始化资源

  1. Scene mScene = new Scene();
  2. ground(mBackground);
                Scene mScene = new Scene();
		mScene.setBackground(mBackground);

接着在Scene中创建4个动画精灵

  1. final TouchAnimatedSprite myBanana =new TouchAnimatedSprite(100,50,64,64,mBanana, getVertexBufferObjectManager());
  2. myBanana.animate(150);
  3. final TouchAnimatedSprite myFrog =new TouchAnimatedSprite(100,CAMERA_HEIGHT-100,64,64,mFrog,getVertexBufferObjectManager());
  4. myFrog.animate(90);
  5. final TouchAnimatedSprite myHelicopter =new TouchAnimatedSprite(CAMERA_WIDTH - 140, CAMERA_HEIGHT - 100,84,84,mHelicopter,getVertexBufferObjectManager());
  6. myHelicopter.animate(new long[]{100,200,150,400},0,3,true);
  7. final TouchAnimatedSprite mySnapDragon =new TouchAnimatedSprite(CAMERA_WIDTH - 100,50,85,85,mSnapDragon,getVertexBufferObjectManager());
  8. mySnapDragon.animate(90);
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);


 

然后把四个精灵动画添加到场景中

  1. mScene.attachChild(myBanana);
  2. mScene.attachChild(myFrog);
  3. mScene.attachChild(myHelicopter);
  4. mScene.attachChild(mySnapDragon);
                mScene.attachChild(myBanana);
		mScene.attachChild(myFrog);
		mScene.attachChild(myHelicopter);
		mScene.attachChild(mySnapDragon);


场景中的人物布置好了之后,接着个场景中的人如添加触摸操作支持

  1. mScene.registerTouchArea(myBanana);
  2. mScene.registerTouchArea(myFrog);
  3. mScene.registerTouchArea(myHelicopter);
  4. mScene.registerTouchArea(mySnapDragon);
  5. mScene.setTouchAreaBindingOnActionDownEnabled(true);
                mScene.registerTouchArea(myBanana);
		mScene.registerTouchArea(myFrog);
		mScene.registerTouchArea(myHelicopter);
		mScene.registerTouchArea(mySnapDragon);
		mScene.setTouchAreaBindingOnActionDownEnabled(true);


至此,整个设计已经完成了,但是屏幕只可以支持单点触摸的方式,毕竟咱们的手机大多数都是

支持多点触摸的,这样岂不是浪费并且不好玩.为了支持多点触摸可以在onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)方法

初始化mScene后添加如下方法:

  1. if(MultiTouch.isSupported(this))//添加多点触摸支持
  2. {
  3. this.mEngine.setTouchController(new MultiTouchController());
  4. this.mEngine.getEngineOptions().getTouchOptions().setNeedsMultiTouch(true);
  5. }
        if(MultiTouch.isSupported(this))//添加多点触摸支持
	    {
	     this.mEngine.setTouchController(new MultiTouchController());
	     this.mEngine.getEngineOptions().getTouchOptions().setNeedsMultiTouch(true);		
	    }

这样做了之后是不是很完美了呢?哈哈哈~~~~~~~~~~~~~~~~~~~~~

好了,废话少说,看看效果再说吧!





本节源代码下载地址:http://download.youkuaiyun.com/detail/cen616899547/4707665

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值