使用TexturePacker(AndEngine)加载纹理
我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设问题是您尝试在执行
onLoadResources()
之前初始化Sprite spriteS
。这样的事情可能会对您有所帮助:
您还可以将精灵初始化移动到创建场景的方法。
PS这只是示例代码。它并不意味着提供干净或良好的风格。
I assume that the problem is that you try to initialize the
Sprite spriteS
beforeonLoadResources()
is executed.Something like this may help you:
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.