简体   繁体   English

如何从andEngine GLES2中的精灵表中获取特定的精灵?

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

I have sprite sheet which is not managed row and column wise. 我有精灵表,不是明智的行和列。 But i have the pixel cordinates,height and width of the each sprite in the sprite sheet. 但是我在精灵表中有每个精灵的像素坐标,高度和宽度。 I far i as know i should be able to get one particular image by 我知道我应该能够通过获得一张特定的图像

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

Here i have supplied the x-cordinate and y-cordinate of sprite as last two parameter in createFromAsset function. 在这里,我提供了sprite的x坐标和y坐标作为createFromAsset函数中的最后两个参数。 Shouldn't it return me that particular image located in that coordinate in sprite sheet? 它不应该让我返回位于子画面中该坐标中的特定图像吗? Instead it is return me the whole sprite. 相反,它让我回到了整个精灵。 If i am not getting the function properly then please explain me what does those last two parameters means? 如果我无法正常使用该功能,请向我解释最后两个参数是什么意思? How can i get a particular sprite?? 我如何获得特定的精灵? Help me i am just new to andEngine. 帮帮我,我只是andEngine的新手。

Because your spritesheet is not managed row or column wise you wont benefit from BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset which allows you to set your row and column numbers. 因为您的Spritesheet并非按行或按列进行管理,所以您将无法从BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset中受益,后者可用于设置行号和列号。

However, I am in the same position as you so here is what I done (using your code). 但是,我和您的位置相同,所以这就是我所做的(使用您的代码)。

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

The last two parameters (from your initial question) are pTextureX and pTextureY which set the location to start reading from your spritesheet. 最后两个参数(来自您最初的问题)是pTextureX和pTextureY,它们设置了从Spritesheet开始读取的位置。 Which is why you were still getting everything returned to you. 这就是为什么您仍将所有东西还给您的原因。

Hope this helps. 希望这可以帮助。

the procedure is: Set the texture atlas - done; 步骤是:设置纹理图集-完成; Set the texture region - done // imagine them as a stickers on the sheet of paper, you can have a lot of them, just keep in mind that they cannot overlap, or go over the atlas 设置纹理区域-完成// //将它们想象成一张纸上的贴纸,您可以有很多贴纸,请记住它们不能重叠,也不能越过地图集

and now its easy: you set your sprite like this: 现在很容易:您可以像这样设置子画面:

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

As you can see you put your textureregion as the parametr in the construcotr. 如您所见,您将纹理区域作为构造器中的参数。 This way you do not have to put any coordinates for the texture region here. 这样,您不必在此处放置纹理区域的任何坐标。

Also remember to attach your sprite to the scene: 还请记住将精灵附加到场景:

mScene.attachChild(yourSprite);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM