سؤال

I use TexturePacker. I packed my textures and I have this file:

  • sample.png
  • sample.tps
  • sample.xml
  • sample.java

These resources are assets/gfx/sample. I connected to project sample.java.
I have this code:

Textures.java

public class Textures {

private ITexture mSpritesheetTexture;
private TexturePackTextureRegionLibrary mSpritesheetTexturePackTextureRegionLibrary;
private TextureRegion faceTextureRegion;
private TexturePack spritesheetTexturePack;

public Textures(final BaseGameActivity activity, final Engine engine) {
    try {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        this.spritesheetTexturePack = new TexturePackLoader(activity, "sample/").loadFromAsset(activity, "faces.xml");
        this.mSpritesheetTexture = spritesheetTexturePack.getTexture();
        this.mSpritesheetTexturePackTextureRegionLibrary = spritesheetTexturePack.getTexturePackTextureRegionLibrary();
        this.faceTextureRegion = this.mSpritesheetTexturePackTextureRegionLibrary.get(faces.FACES_ID);

        engine.getTextureManager().loadTexture(this.mSpritesheetTexture);
    } catch (final TexturePackParseException e) {
        Log.e("Error", String.valueOf(e));
    }
}

public TextureRegion getT(){
    return faceTextureRegion;
}

}

Main.java

...
Textures textures;
...
@Override
public void onLoadResources() {
    textures = new Textures(this, getEngine());
}

Define sprite and I get an error on this line:

final Sprite spriteS = new Sprite(50, 50, textures.getT());  

E/AndroidRuntime(391): Caused by: java.lang.NullPointerException
E/AndroidRuntime(391): at com.web.scene.AboutScene.<init>(AboutScene.java:24)

What is my mistake?

هل كانت مفيدة؟

المحلول

I assume that the problem is that you try to initialize the Sprite spriteS before onLoadResources() is executed.

Something like this may help you:

Textures textures;
Sprite spriteS;
...
@Override
public void onLoadResources() {
    textures = new Textures(this, getEngine());
    spriteS = new Sprite(50, 50, textures.getT());  
}

You might also move the sprite init to the method where the scene is created.

P.S. It is only example code. It is not meant to provide clean or good style.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top