簡體   English   中英

如何從andEngine GLES2中的精靈表中獲取特定的精靈?

[英]How to get particular Sprite from sprite sheet in andEngine GLES2?

我有精靈表,不是明智的行和列。 但是我在精靈表中有每個精靈的像素坐標,高度和寬度。 我知道我應該能夠通過獲得一張特定的圖像

    spaceshipTexture = new BitmapTextureAtlas(getTextureManager(),1024,512,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    playerTextureRegion= BitmapTextureAtlasTextureRegionFactory.createFromAsset(spaceshipTexture, 
            this, "spaceshipsheet.png",0,119);

在這里,我提供了sprite的x坐標和y坐標作為createFromAsset函數中的最后兩個參數。 它不應該讓我返回位於子畫面中該坐標中的特定圖像嗎? 相反,它讓我回到了整個精靈。 如果我無法正常使用該功能,請向我解釋最后兩個參數是什么意思? 我如何獲得特定的精靈? 幫幫我,我只是andEngine的新手。

因為您的Spritesheet並非按行或按列進行管理,所以您將無法從BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset中受益,后者可用於設置行號和列號。

但是,我和您的位置相同,所以這就是我所做的(使用您的代碼)。

spaceshipTexture = new BitmapTextureAtlas(getTextureManager(), 1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

playerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spaceshipTexture, 
        this, "spaceshipsheet.png", 0, 0); // start reading from the start of your spritesheet, so you can set multiple sprites

playerTextureRegion.set(0, 109, 20, 20); // pTextureX, pTextureY, pTextureWidth, pTextureHeight -- here you grab the sprite from the spritesheet

最后兩個參數(來自您最初的問題)是pTextureX和pTextureY,它們設置了從Spritesheet開始讀取的位置。 這就是為什么您仍將所有東西還給您的原因。

希望這可以幫助。

步驟是:設置紋理圖集-完成; 設置紋理區域-完成// //將它們想象成一張紙上的貼紙,您可以有很多貼紙,請記住它們不能重疊,也不能越過地圖集

現在很容易:您可以像這樣設置子畫面:

 Sprite yourSprite = new Sprite(x, y, playerTextureRegion, vertexBufferObjectManager);

如您所見,您將紋理區域作為構造器中的參數。 這樣,您不必在此處放置紋理區域的任何坐標。

還請記住將精靈附加到場景:

mScene.attachChild(yourSprite);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM