andengine中的小技巧

andengine中,有很多小技巧,下面将为大家介绍一些

一、图片的创建
之前的例子当中,我们在创建图片的时候,都要预先创建一个区域BitmapTextureAtlas,例如之前在创建坦克sprite例子中,我们为坦克sprite创建了一个BitmapTextureAtlas:

				BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(
				getTextureManager(), 128, 256, TextureOptions.DEFAULT);
		mSpriteTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(mBitmapTextureAtlas, this, "tank.png", 0,
						0, 1, 1);
		mBitmapTextureAtlas.load();

而坦克这张图片的实际大小为:73*132,因此有部分区域浪费掉了。如果我们再创建一个精灵为它再建立一个BitmapTextureAtlas,又会有一部分区域浪费掉了。实际上是我们对内存的消耗。因此,我们可以建立一张很大BitmapTextureAtlas,然后将其它图片加载到这一张上来。这里有一点需要注意:BitmapTextureAtlas并不是可以无限创建大小的,它也是有极限的。因此,如果图片很多很大的话,还是分多个BitmapTextureAtlas来创建吧。

关于如何在一个BitmapTextureAtlas上创建多个图片的方法,可以看一看andengine的例子,我这里随便摘取了一段供大家参考:
		this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(1024,
				1024, TextureOptions.DEFAULT);
		this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mAutoParallaxBackgroundTexture, this,
						"gfx/parallax_background_layer_front.png", 0, 0);
		this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mAutoParallaxBackgroundTexture, this,
						"gfx/parallax_background_layer_back.png", 0, 188);
		this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mAutoParallaxBackgroundTexture, this,
						"gfx/parallax_background_layer_mid.png", 0, 669);


解释一下:
首先,创建一个1024*1024的BitmapTextureAtlas,然后分别在这个上面建立3张图片。

第一张创建在mAutoParallaxBackgroundTexture的左上角,坐标位置为0,0点。其原始图片大小为:640*187;

第二张创建在mAutoParallaxBackgroundTexture的0,188位置,意思就是排在640*187图片的下面,即187+1=188。其原始图片大小为:640*480;

第三张创建在mAutoParallaxBackgroundTexture的0,669位置,意思就是排在640*480图片的下面,即188+480+1=669;

看明白了吗,O(∩_∩)O哈哈~




未完,待续。。。。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值