我正在使用一种将xml文件加载到游戏中的方法,其级别与此类似:
private void loadLevel(int levelID)
{
final SimpleLevelLoader levelLoader = new SimpleLevelLoader(vbom);
final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.01f, 0.5f);
levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(LevelConstants.TAG_LEVEL)
{
public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException
{
final int width = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_WIDTH);
final int height = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_HEIGHT);
// TODO later we will specify camera BOUNDS and create invisible walls
// on the beginning and on the end of the level.
return GameScene.this;
}
});
levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(TAG_ENTITY)
{
public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException
{
final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_X);
final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_Y);
final String type = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_TYPE);
final Sprite levelObject;
if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM1))
{
levelObject = new Sprite(x, y, resourcesManager.platform1_region, vbom);
PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF).setUserData("platform1");
}
else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM2))
{
levelObject = new Sprite(x, y, resourcesManager.platform2_region, vbom);
final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF);
body.setUserData("platform2");
physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
}
else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM3))
{
levelObject = new Sprite(x, y, resourcesManager.platform3_region, vbom);
final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF);
body.setUserData("platform3");
physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false));
}
else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_COIN))
{
levelObject = new Sprite(x, y, resourcesManager.coin_region, vbom)
{
@Override
protected void onManagedUpdate(float pSecondsElapsed)
{
super.onManagedUpdate(pSecondsElapsed);
/**
* TODO
* we will later check if player collide with this (coin)
* and if it does, we will increase score and hide coin
* it will be completed in next articles (after creating player code)
*/
}
};
levelObject.registerEntityModifier(new LoopEntityModifier(new ScaleModifier(1, 1, 1.3f)));
}
else
{
throw new IllegalArgumentException();
}
levelObject.setCullingEnabled(true);
return levelObject;
}
});
levelLoader.loadLevelFromAsset(activity.getAssets(), "level/" + levelID + ".lvl");
}
如果我在我的级别XML文件中添加一个随机类型字符串的实体,该文件未在该方法中定义并且我运行它,我得到这个堆栈跟踪:
05-02 16:56:40.880: E/AndroidRuntime(3665): FATAL EXCEPTION: UpdateThread
05-02 16:56:40.880: E/AndroidRuntime(3665): org.andengine.util.level.exception.LevelLoaderException: java.lang.IllegalArgumentException
05-02 16:56:40.880: E/AndroidRuntime(3665): at org.andengine.util.level.LevelLoader.loadLevelFromStream(LevelLoader.java:127)
05-02 16:56:40.880: E/AndroidRuntime(3665): at org.andengine.util.level.LevelLoader.loadLevelFromAsset(LevelLoader.java:97)
05-02 16:56:40.880: E/AndroidRuntime(3665): at org.andengine.util.level.LevelLoader.loadLevelFromAsset(LevelLoader.java:88)
05-02 16:56:40.880: E/AndroidRuntime(3665): at org.andengine.util.level.LevelLoader.loadLevelFromAsset(LevelLoader.java:82)
05-02 16:56:40.880: E/AndroidRuntime(3665): at com.package.game.GameScene.loadLevel(GameScene.java:478)
05-02 16:56:40.880: E/AndroidRuntime(3665): at com.package.game.GameScene.setLevel(GameScene.java:1693)
05-02 16:56:40.880: E/AndroidRuntime(3665): at com.package.game.SceneManager$1.onTimePassed(SceneManager.java:117)
05-02 16:56:40.880: E/AndroidRuntime(3665):
我需要能够在一个级别文件的不同时间加载不同的实体,那么如何以某种方式修改此方法,以便它“忽略”未定义的实体?
答案 0 :(得分:1)
我刚刚查看了LevelParser.java方法的startElement(),您可以通过创建默认的Entity Loader来绕过Andengine中抛出IllegalArgumentException异常的位置。即,
levelLoader.setDefaultEntityLoader( new IEntityLoader()
{
@Override
public void onLoadEntity(String pEntityName, Attributes pAttributes) {
// TODO Auto-generated method stub
Log.w("setDefaultEntityLoader","Unknown Entity");
}
});
然后将调用此默认的onLoadEntity()而不是引发的异常。我在GLES 1上进行了测试,它按预期工作。
希望这有帮助。