溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

OGEngine游戲開發(fā)之精靈(sprite)

發(fā)布時間:2020-10-02 14:49:00 來源:網(wǎng)絡(luò) 閱讀:397 作者:橙游Orange 欄目:開發(fā)技術(shù)

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)造:

  1. public BitmapTextureAtlas(final TextureManager pTextureManager, final int pWidth, final int pHeight) {

  2.                 this(pTextureManager, pWidth, pHeight, BitmapTextureFormat.RGBA_8888);

  3. }

復(fù)制代碼

因為在OGEngine中載入的貼圖的寬高必須是2的整次冪,所有可以發(fā)現(xiàn)pWidth,pHeight的值都是遵循的。如果寬高值不是2的整次冪,會拋出IllegalArgumentException異常??墒窃谟螒蛑惺褂玫膱D片寬高是多種多樣的,不可能都是2整次冪。所以有了貼圖區(qū)域TextureRegion的概念。另外一個值得注意的東西就是TextureRegion,它可以從BitmapTextureAtlas中"扣"出一張貼圖

  1. /**創(chuàng)建2次冪高和寬的紋理圖片組合對象*/

  2. BitmapTextureAtlas bgTexture = new BitmapTextureAtlas(

  3. getEngine().getTextureManager(), 2048, 128);


  4. /**生成紋理區(qū)域bgRegion*/

  5. TextureRegion bgRegion = BitmapTextureAtlasTextureRegionFactory.

  6. createFromAsset(bgTexture, getActivity().getAssets(),"gfx/bg.jpg",0,0);


  7. /**在引擎中加載紋理組合*/

  8. getEngine().getTextureManager().loadTexture(bgTexture);

復(fù)制代碼

(其中圖片資源bg.jpg需要放到asset目錄下的gfx文件夾里)


創(chuàng)建精靈sprite


(一)普通sprite

sprite是Entity的子類,精靈在游戲中是人或者物體的視覺表現(xiàn)形式,“精靈”一詞本來指放置在游戲背景上且?guī)в袆赢嫷莫毩D形形象。它的大部分方法還是來自Entity類

1) 縮放:

  1. setScaleX(final float pScaleX)

  2. setScaleY(final float pScaleY)

  3. setScale(final float pScale)

  4. //分別對應(yīng)X方向,Y方向,和整體縮放。

復(fù)制代碼


2) 透明度:

  1. setAlpha(float pAlpha) //對應(yīng)的透明度

復(fù)制代碼

3) 設(shè)置位置:

  1. setPosition(float pX, float pY) 

  2. setPosition(IEntity pOtherEntity)

  3. //前面一個直接給出坐標(biāo)即可,這個常用在圖片拖拽,和移動位置時使用;也可以直接設(shè)置相對坐標(biāo),

  4. //如:setLeftPositionX(float pX); setTopPositionY(float pY); 

  5. //后面會給一個IEntity的對象,然后它會取得IEntity的對象的坐標(biāo)值來賦給當(dāng)前精靈的坐標(biāo)值

復(fù)制代碼


4) 旋轉(zhuǎn):

  1. setRotation(float pRotation)

  2. //參數(shù)是角度,即45度(45f),90度(90f),180度(180f)

  3. /**輔助的設(shè)置還有旋轉(zhuǎn)的中心點*/

  4. setRotationCenterX(final float pRotationCenterX)://X坐標(biāo)

  5. setRotationCenterY(final float pRotationCenterY)://Y坐標(biāo)

  6. setRotationCenter(final float pRotationCenterX, final float pRotationCenterY)://X坐標(biāo)Y坐標(biāo)

復(fù)制代碼


Scene中創(chuàng)建精靈:

  1. /**創(chuàng)建精靈對象*/

  2. Sprite bg = new Sprite(0, 0, BG, this.getVertexBufferObjectManager());

復(fù)制代碼


把精靈添加到場景中

  1. /**在場景里面添加精靈*/ 

  2. this.attachChild(bg);

復(fù)制代碼



(二)動畫精靈AnimatedSprite


一.用處

OGEngine封裝了一個TiledSprite類,可以傳入TiledTextureRegion的紋理以構(gòu)造一個可以連續(xù)播放的精靈,但必須要先制作好一張動畫序列圖片,俗稱Tiled。而AnimateSprite(動畫精靈)繼承于TiledSprite,用來描述一些幀動畫資源。AnimatedSprite的構(gòu)造方法中需要的ITiledTextureRegionTiled而不是普通的TextureRegion,也就是說,是基于單幀動畫資源的!


二.TiledTextureRegion資源TileTextureRegion的一般用法,就是一張大圖,是由很多單幀圖片構(gòu)成的,一般是通過調(diào)用BitmapTextureAtlasTextureRegionFactory中的createTiledFromAsset(...)方法創(chuàng)建的。

  1. /** 創(chuàng)建2次冪高和寬的紋理圖片組合對象 */

  2. BitmapTextureAtlas bgTexture = new BitmapTextureAtlas(getEngine()

  3.                                 .getTextureManager(), 1024, 1024);


  4. /** 生成紋理區(qū)域mTextureRegion */

  5. TiledTextureRegion mTextureRegion = BitmapTextureAtlasTextureRegionFactory

  6.                                 .createTiledFromAsset(bgTexture,

  7.                                         this.getActivity().getAssets(),

  8.                                   "gfx/snapdragon_tiled.png", 0, 0, 4, 3);


  9. /** 在引擎中加載紋理組合 */

  10. getEngine().getTextureManager().loadTexture(bgTexture);

復(fù)制代碼




這是一般用法,正是這些靜態(tài)方法,讓我們忽略了TiledTextureRegion自身的構(gòu)造方法

  1. public TiledTextureRegion(final ITexture pTexture, final ITextureRegion ... pTextureRegions)

復(fù)制代碼


三.歸根結(jié)底

其實要想找到這個問題的解決方法很簡單,就是一步一步跟下來,從BitmapTextureAtlasTextureRegionFactorycreateTiledFromAsset(),到TextureRegionFactorycreateTiledFromSource(),再到TiledTextureRegioncreate(),當(dāng)你看到下面的代碼時,腦筋一轉(zhuǎn),就知道歸根結(jié)底還是調(diào)用的TiledTextureRegion的構(gòu)造方法了。。。

四.創(chuàng)建動畫精靈

  1. /**創(chuàng)建精靈對象*/

  2. AnimateSprite bg = new AnimateSprite(0, 0, mTextureRegion , 

  3. this.getVertexBufferObjectManager());

復(fù)制代碼



五.把精靈添加到場景中

  1. /**在場景里面添加精靈*/ 

  2. this.attachChild(bg);

  3. /**設(shè)置精靈動畫播放頻率*/

  4. 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中添加方法:

  1. /** 創(chuàng)建精靈對象 */

  2. ButtonSprite bg2 = new ButtonSprite(100, 100, mSnapdragonTextureRegion, this.getVertexBufferObjectManager());

  3. /**在場景里面添加精靈*/ 

  4. this.attachChild(bg2);

復(fù)制代碼

ButtonSprite添加觸摸監(jiān)聽必須要先取消觸摸阻塞,

  1. /** 取消觸摸阻塞 */

  2. bg2.setIgnoreTouch(false);

  3. bg2.setOnClickListener(new OnClickListener() {

  4.         @Override

  5.         public void onClick(ButtonSprite pButtonSprite,

  6.                         float pTouchAreaLocalX, float pTouchAreaLocalY) {

  7.         // TODO Auto-generated method stub

  8.                 Log.v("tag", "bg2");

  9.         }

  10. });

 http://www.eoeandroid.com/forum-863-1.html

www.ogengine.com



向AI問一下細(xì)節(jié)

免責(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)容。

AI