andengine中的小技巧

来源:互联网 发布:阿里云 弹性ip bgp带宽 编辑:程序博客网 时间:2024/06/09 16:16

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哈哈~




未完,待续。。。。