您好,登錄后才能下訂單哦!
Spirte簡介
sprite可以說是游戲中的主角,我們建立各種圖片,人物的顯示都離不開它。sprite類很豐富,如果看過源代碼的不難發(fā)現(xiàn),可用的類型很多:基礎(chǔ)精靈Spirte、動畫精靈AnimatedSpirte、按鍵精靈ButtonSprite.
你可以有多種選擇,可以直接使用AnimatedSprite來繪制人物動畫,可以使用ButtonSprite來繪制按鈕,也可以繼承Sprite類實現(xiàn)自己的精靈。因此,我們在此介紹Sprite的使用,首先讓我們了解下精靈的相關(guān)知識:
紋理Texture 和 BitmapTextureAtlas
要繪制一個精靈sprite, 必須要加載其紋理Texture。紋理就是繪制在Sprite對象上的位圖,OGEngine引擎在內(nèi)存中以Texture對象的形式儲存所有紋理,并通過TextureManager來管理游戲中的所有Texture對象。
BitmapTextureAtlas 可以被認(rèn)為是一個包含很多不同紋理的地圖,主要是把一下貼圖粘在一起組合成一張大的Texture.
首先了解下它的構(gòu)造:
public BitmapTextureAtlas(final TextureManager pTextureManager, final int pWidth, final int pHeight) {
this(pTextureManager, pWidth, pHeight, BitmapTextureFormat.RGBA_8888);
}
復(fù)制代碼
因為在OGEngine中載入的貼圖的寬高必須是2的整次冪,所有可以發(fā)現(xiàn)pWidth,pHeight的值都是遵循的。如果寬高值不是2的整次冪,會拋出IllegalArgumentException異常??墒窃谟螒蛑惺褂玫膱D片寬高是多種多樣的,不可能都是2整次冪。所以有了貼圖區(qū)域TextureRegion的概念。另外一個值得注意的東西就是TextureRegion,它可以從BitmapTextureAtlas中"扣"出一張貼圖
/**創(chuàng)建2次冪高和寬的紋理圖片組合對象*/
BitmapTextureAtlas bgTexture = new BitmapTextureAtlas(
getEngine().getTextureManager(), 2048, 128);
/**生成紋理區(qū)域bgRegion*/
TextureRegion bgRegion = BitmapTextureAtlasTextureRegionFactory.
createFromAsset(bgTexture, getActivity().getAssets(),"gfx/bg.jpg",0,0);
/**在引擎中加載紋理組合*/
getEngine().getTextureManager().loadTexture(bgTexture);
復(fù)制代碼
(其中圖片資源bg.jpg需要放到asset目錄下的gfx文件夾里)
創(chuàng)建精靈sprite
(一)普通sprite
sprite是Entity的子類,精靈在游戲中是人或者物體的視覺表現(xiàn)形式,“精靈”一詞本來指放置在游戲背景上且?guī)в袆赢嫷莫毩D形形象。它的大部分方法還是來自Entity類
1) 縮放:
setScaleX(final float pScaleX)
setScaleY(final float pScaleY)
setScale(final float pScale)
//分別對應(yīng)X方向,Y方向,和整體縮放。
復(fù)制代碼
2) 透明度:
setAlpha(float pAlpha) //對應(yīng)的透明度
復(fù)制代碼
3) 設(shè)置位置:
setPosition(float pX, float pY)
setPosition(IEntity pOtherEntity)
//前面一個直接給出坐標(biāo)即可,這個常用在圖片拖拽,和移動位置時使用;也可以直接設(shè)置相對坐標(biāo),
//如:setLeftPositionX(float pX); setTopPositionY(float pY);
//后面會給一個IEntity的對象,然后它會取得IEntity的對象的坐標(biāo)值來賦給當(dāng)前精靈的坐標(biāo)值
復(fù)制代碼
4) 旋轉(zhuǎn):
setRotation(float pRotation)
//參數(shù)是角度,即45度(45f),90度(90f),180度(180f)
/**輔助的設(shè)置還有旋轉(zhuǎn)的中心點*/
setRotationCenterX(final float pRotationCenterX)://X坐標(biāo)
setRotationCenterY(final float pRotationCenterY)://Y坐標(biāo)
setRotationCenter(final float pRotationCenterX, final float pRotationCenterY)://X坐標(biāo)Y坐標(biāo)
復(fù)制代碼
在Scene中創(chuàng)建精靈:
/**創(chuàng)建精靈對象*/
Sprite bg = new Sprite(0, 0, BG, this.getVertexBufferObjectManager());
復(fù)制代碼
把精靈添加到場景中
/**在場景里面添加精靈*/
this.attachChild(bg);
復(fù)制代碼
(二)動畫精靈AnimatedSprite
一.用處
OGEngine封裝了一個TiledSprite類,可以傳入TiledTextureRegion的紋理以構(gòu)造一個可以連續(xù)播放的精靈,但必須要先制作好一張動畫序列圖片,俗稱Tiled圖。而AnimateSprite(動畫精靈)繼承于TiledSprite,用來描述一些幀動畫資源。AnimatedSprite的構(gòu)造方法中需要的ITiledTextureRegion是Tiled而不是普通的TextureRegion,也就是說,是基于單幀動畫資源的!
二.TiledTextureRegion資源TileTextureRegion的一般用法,就是一張大圖,是由很多單幀圖片構(gòu)成的,一般是通過調(diào)用BitmapTextureAtlasTextureRegionFactory中的createTiledFromAsset(...)方法創(chuàng)建的。
/** 創(chuàng)建2次冪高和寬的紋理圖片組合對象 */
BitmapTextureAtlas bgTexture = new BitmapTextureAtlas(getEngine()
.getTextureManager(), 1024, 1024);
/** 生成紋理區(qū)域mTextureRegion */
TiledTextureRegion mTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(bgTexture,
this.getActivity().getAssets(),
"gfx/snapdragon_tiled.png", 0, 0, 4, 3);
/** 在引擎中加載紋理組合 */
getEngine().getTextureManager().loadTexture(bgTexture);
復(fù)制代碼
這是一般用法,正是這些靜態(tài)方法,讓我們忽略了TiledTextureRegion自身的構(gòu)造方法
public TiledTextureRegion(final ITexture pTexture, final ITextureRegion ... pTextureRegions)
復(fù)制代碼
三.歸根結(jié)底
其實要想找到這個問題的解決方法很簡單,就是一步一步跟下來,從BitmapTextureAtlasTextureRegionFactory的createTiledFromAsset(),到TextureRegionFactory的createTiledFromSource(),再到TiledTextureRegion的create(),當(dāng)你看到下面的代碼時,腦筋一轉(zhuǎn),就知道歸根結(jié)底還是調(diào)用的TiledTextureRegion的構(gòu)造方法了。。。
四.創(chuàng)建動畫精靈
/**創(chuàng)建精靈對象*/
AnimateSprite bg = new AnimateSprite(0, 0, mTextureRegion ,
this.getVertexBufferObjectManager());
復(fù)制代碼
五.把精靈添加到場景中
/**在場景里面添加精靈*/
this.attachChild(bg);
/**設(shè)置精靈動畫播放頻率*/
bg.animate(100);
復(fù)制代碼
(三)按鈕精靈
ButtonSprite和AnimateSprite類似,ButtonSprite的構(gòu)造函數(shù)中需要的也是TiledTextureRegion,區(qū)別就是ButtonSprite分為3種狀態(tài),NORMAL,PRESSED,DISABLED,默認(rèn)狀態(tài)為NORMAL。按下的時候會顯示第2幀動畫,不可按時顯示第3幀動畫。創(chuàng)建ButtonSprite和往scene中添加方法:
/** 創(chuàng)建精靈對象 */
ButtonSprite bg2 = new ButtonSprite(100, 100, mSnapdragonTextureRegion, this.getVertexBufferObjectManager());
/**在場景里面添加精靈*/
this.attachChild(bg2);
復(fù)制代碼
ButtonSprite添加觸摸監(jiān)聽必須要先取消觸摸阻塞,
/** 取消觸摸阻塞 */
bg2.setIgnoreTouch(false);
bg2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
// TODO Auto-generated method stub
Log.v("tag", "bg2");
}
});
http://www.eoeandroid.com/forum-863-1.html
www.ogengine.com
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。