Question

I'm using a for loop to implement the loading and handling of Sprite objects for display for a keyboard for a game of hangman. The loop makes it through to the 4th iteration and crashes. The error it gives me says:

Texture must not exceed the bounds of the atlas

This should actually work as all the images are 64x64 and the atlas is declared as such:

this.mAtlas[i] = new BitmapTextureAtlas(this.getTextureManager(),256, 256,TextureOptions.BILINEAR);

I'm using an array of atlases and an array of textures in which to load the images and the I load the atlas. After that I'm then passing the texture into a custom class that implements sprite. And finally I attach the loaded sprite into the scene. Here is the whole code for the loop:

for(int i = 0; i < 28; i++)
{
    String name = Integer.toString(i);
    name+= ".png";
    this.mAtlas[i] = new BitmapTextureAtlas(this.getTextureManager(),256, 256,TextureOptions.BILINEAR);
    this.mTexture[i] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAtlas[i], this, name, (i*64) + 5,0);
    this.mAtlas[i].load();
    if(i % 13 == 0)
    {               
        yPos -= 64;
    }
    if(i < 26)
    {
        letterPass = alphabet.substring(i);
    }
    else if(i == 26)
    {
        letterPass = "BackSpace";
    }
    else if(i == 27)
    {
        letterPass = "return";
    }
    letters[i] = new Letter((i * 64)+ 5.0f, yPos, this.mTexture[i].getHeight(), this.mTexture[i].getHeight(), this.mTexture[i], this.mEngine.getVertexBufferObjectManager());
    letters[i].setLetter(letterPass);
    mScene.attachChild(letters[i]);     
}

The line where the crash occurs is:

this.mTexture[i] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAtlas[i], this, name, (i*64) + 5,0);

I cannot seem to figure out why it's crashing and I'd appreciate any help

Was it helpful?

Solution

You texture atlas is 256x256 pixels large. Your sprites are 64x64 pixels and you create an atlas for each of them... That means you are wasting a lot of space. And it doesn't even work because on this line:

    this.mTexture[i] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAtlas[i], this, name, (i*64) + 5,0);

You are placing the texture onto atlas at position [i * 64 + 5, 0]. I bet it fails on 4th texture. 3 * 64 + 5 +64 = 261, you are out of bounds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top