在学习过程中,发现的例子基本都是类似这种,但对纹理图的相关说法感觉没有说透(我对这点的纠结源于如何取到第二个图,而不是第一个图引发,如下图。于是进行了一系列的查找和阅读。)
BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(),64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA); TiledTextureRegion mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "face_circle_tiled.png", 0, 0,2,1);对于纹理图的拼接都比较好理解,也就是多幅图拼接成一张大的内存图。
那么 TiledTextureRegion又是什么样的形式呢?看一下对象定义
protected int mCurrentTileIndex; protected final int mTileCount; protected final ITextureRegion[] mTextureRegions;
public static TiledTextureRegion create(final ITexture pTexture, final int pTextureX, final int pTextureY, final int pTextureWidth, final int pTextureHeight, final int pTileColumns, final int pTileRows, final boolean pRotated) { final ITextureRegion[] textureRegions = new ITextureRegion[pTileColumns * pTileRows]; final int tileWidth = pTextureWidth / pTileColumns; final int tileHeight = pTextureHeight / pTileRows; for (int tileColumn = 0; tileColumn < pTileColumns; tileColumn++) { for (int tileRow = 0; tileRow < pTileRows; tileRow++) { final int tileIndex = tileRow * pTileColumns + tileColumn; final int x = pTextureX + tileColumn * tileWidth; final int y = pTextureY + tileRow * tileHeight; textureRegions[tileIndex] = new TextureRegion(pTexture, x, y, tileWidth, tileHeight, pRotated); } } return new TiledTextureRegion(pTexture, false, textureRegions); }
TiledTextureRegion mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "face_circle_tiled.png", 0, 0,2,1);
从调用这个方法的上一步,可以看到
final TiledTextureRegion tiledTextureRegion = TiledTextureRegion.create(pTextureAtlas, pTextureX, pTextureY, pTextureAtlasSource.getTextureWidth(), pTextureAtlasSource.getTextureHeight(), pTileColumns, pTileRows, pRotated); pTextureAtlas.addTextureAtlasSource(pTextureAtlasSource, pTextureX, pTextureY); return tiledTextureRegion;
假如像往上的例子,往上的例子没有"注1"
private static class Ball extends AnimatedSprite{ float mVelocityX = BALL_VELOCITY;//球的x方向速度 float mVelocityY = BALL_VELOCITY ;//球的y方向速度 public Ball(float pX, float pY, float pWidth, float pHeight, ITiledTextureRegion pTiledTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) { super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager); // TODO Auto-generated constructor stub this.setCurrentTileIndex(1);//add by danielinbiti 注1 mX = 100; mY = 100; } }
在AnimatedSprite的上上....级父类TiledSprite中有这么一行
public ITextureRegion getTextureRegion() { return this.getTiledTextureRegion().getTextureRegion(this.mCurrentTileIndex); } protected void draw(final GLState pGLState, final Camera pCamera) { this.mTiledSpriteVertexBufferObject.draw(GLES20.GL_TRIANGLES, this.mCurrentTileIndex * TiledSprite.VERTICES_PER_TILEDSPRITE, TiledSprite.VERTICES_PER_TILEDSPRITE); }