java - How to have LevelLoader method 'ignore' undefined entities? -
i using method of loading xml file game level similar this:
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 specify camera bounds , create invisible walls // on beginning , on end of 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 * later check if player collide (coin) * , if does, increase score , hide coin * 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");
}
if add entity random type string in level xml file not defined in method , run it, stacktrace:
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): @ org.andengine.util.level.levelloader.loadlevelfromstream(levelloader.java:127) 05-02 16:56:40.880: e/androidruntime(3665): @ org.andengine.util.level.levelloader.loadlevelfromasset(levelloader.java:97) 05-02 16:56:40.880: e/androidruntime(3665): @ org.andengine.util.level.levelloader.loadlevelfromasset(levelloader.java:88) 05-02 16:56:40.880: e/androidruntime(3665): @ org.andengine.util.level.levelloader.loadlevelfromasset(levelloader.java:82) 05-02 16:56:40.880: e/androidruntime(3665): @ com.package.game.gamescene.loadlevel(gamescene.java:478) 05-02 16:56:40.880: e/androidruntime(3665): @ com.package.game.gamescene.setlevel(gamescene.java:1693) 05-02 16:56:40.880: e/androidruntime(3665): @ com.package.game.scenemanager$1.ontimepassed(scenemanager.java:117) 05-02 16:56:40.880: e/androidruntime(3665):
i need able load different entities @ different time 1 level file, how can modify method somehow kind of "ignores" entities not defined?
i have looked in levelparser.java method startelement(), can bypass illegalargumentexception exception thrown in andengine creating default entity loader. i.e.,
levelloader.setdefaultentityloader( new ientityloader() { @override public void onloadentity(string pentityname, attributes pattributes) { // todo auto-generated method stub log.w("setdefaultentityloader","unknown entity"); } });
this default onloadentity() called instead of exception being raised. tested on gles 1 , works expected.
hope helps.
Comments
Post a Comment