【问题标题】:How to get particular Sprite from sprite sheet in andEngine GLES2?如何从andEngine GLES2中的精灵表中获取特定的精灵?
【发布时间】:2015-11-03 12:07:50
【问题描述】:

我有没有按行和列管理的精灵表。但我有精灵表中每个精灵的像素坐标、高度和宽度。据我所知,我应该能够通过

获得一张特定的图像
    spaceshipTexture = new BitmapTextureAtlas(getTextureManager(),1024,512,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    playerTextureRegion= BitmapTextureAtlasTextureRegionFactory.createFromAsset(spaceshipTexture, 
            this, "spaceshipsheet.png",0,119);

这里我提供了精灵的 x 坐标和 y 坐标作为 createFromAsset 函数中的最后两个参数。它不应该返回位于精灵表中该坐标中的特定图像吗?相反,它是把整个精灵还给我。如果我没有正确获得该功能,那么请解释一下最后两个参数是什么意思?我怎样才能得到一个特定的精灵?帮助我,我是 andEngine 的新手。

【问题讨论】:

    标签: java android andengine


    【解决方案1】:

    因为您的 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 开始读取的位置。这就是为什么你仍然把所有东西都归还给你的原因。

    希望这会有所帮助。

    【讨论】:

    • 没问题!很高兴它奏效了。如果您对此感到满意,请将其标记为已接受的答案:)
    【解决方案2】:

    程序是: 设置纹理图集 - 完成; 设置纹理区域 - 完成 // 把它们想象成一张纸上的贴纸,你可以有很多,只要记住它们不能重叠,或者越过图集

    现在很容易: 你像这样设置你的精灵:

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

    如您所见,您将纹理区域作为参数放在构造函数中。这样您就不必在此处放置纹理区域的任何坐标。

    还记得将你的精灵附加到场景中:

    mScene.attachChild(yourSprite);
    

    【讨论】:

    • 我明白你说的是 Lukasz,但我想要一个单独的精灵表而不是单独的精灵(即使我的简单游戏很大,它们也会占用大量内存)所以,我把所有sprite packer 软件会在一张纸上显示精灵,它会生成一个文本文件,其中包含表格中各个精灵的坐标、宽度和高度。它们现在不按行和列大小进行管理,我想使用该表中的特定精灵。我知道如何创建一个我只是不知道的精灵;不知道我要引用该表中的特定精灵。帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多