Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用LevelLoader方法';忽略';未定义的实体?_Java_Android_Xml_Andengine - Fatal编程技术网

Java 如何使用LevelLoader方法';忽略';未定义的实体?

Java 如何使用LevelLoader方法';忽略';未定义的实体?,java,android,xml,andengine,Java,Android,Xml,Andengine,我正在使用一种将xml文件加载到游戏中的方法,其级别与此非常类似: private void loadLevel(int levelID) { final SimpleLevelLoader levelLoader = new SimpleLevelLoader(vbom); final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.01f, 0.5f); levelLoader.registerEntityLoa

我正在使用一种将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");

我需要能够在不同的时间从一个级别的文件中加载不同的实体,那么我如何才能以某种方式修改此方法,使其只是“忽略”未定义的实体?

我刚刚查看了LevelParser.java方法startElement(),通过创建默认实体加载器,可以绕过在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上测试了它,它工作正常


希望这有帮助。

捕获抛出的
IllegalArgumentException
。这是
LevelLoader.java
的第127行,在抛出异常的那一行之前,
try
捕获的一部分会去哪里。基于stacktrace,即
LevelLoader.java
的第127行,它应该放在level loader方法的何处?只要在LevelLoader.loadLevelFromAsset()调用之前,顺序就不是关键的。为XML中的每个对应实体名称调用相关的onLoadEntity接口实例,它们在实际解析XML的方法(loadLevelFromAsset)之前注册的顺序无关紧要。在调用levelLoader.loadLevelFromAsset()作为最后一个注册的加载程序之前,我可能会将其放在这里,这样,如果您注册了很多加载程序,我就可以在代码中更轻松地找到它。
levelLoader.setDefaultEntityLoader( new IEntityLoader()
{

    @Override
    public void onLoadEntity(String pEntityName, Attributes pAttributes) {
        // TODO Auto-generated method stub
        Log.w("setDefaultEntityLoader","Unknown Entity");
    }

});