我正在使用一种将 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):
我需要能够在不同的时间从一个级别的文件中加载不同的实体,那么我怎样才能以某种方式修改这个方法,以便它只是一种“忽略”未定义的实体?