使用TexturePacker(AndEngine)加载纹理

发布于 2025-01-07 15:28:20 字数 1839 浏览 0 评论 0原文

我使用TexturePacker。我打包了纹理,并有以下文件:

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

这些资源是 assets/gfx/sample。我连接到项目 sample.java
我有以下代码:

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());
}

定义精灵,我在这一行收到错误:

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)

我的错误是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2025-01-14 15:28:20

我假设问题是您尝试在执行 onLoadResources() 之前初始化 Sprite spriteS

这样的事情可能会对您有所帮助:

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

您还可以将精灵初始化移动到创建场景的方法。

PS这只是示例代码。它并不意味着提供干净或良好的风格。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文