为什么我得到arrayindexoutofboundsexception?

时间:2012-07-02 08:40:04

标签: android andengine indexoutofboundsexception

我正在使用AndEngine制作游戏,我不知道为什么我会收到此错误。它似乎以前工作,我不知道什么代码更改导致错误。有人有什么想法吗?

    07-02 01:34:44.747: E/AndroidRuntime(19252): java.lang.ArrayIndexOutOfBoundsException
07-02 01:34:44.747: E/AndroidRuntime(19252):    at com.prattia.webs.testgfx5.TestGFX5Activity$2.onAreaTouched(TestGFX5Activity.java:249)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.entity.scene.Scene.onAreaTouchEvent(Scene.java:409)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:328)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.engine.Engine.onTouchScene(Engine.java:452)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.engine.Engine.onTouchEvent(Engine.java:438)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.input.touch.controller.BaseTouchController$TouchEventRunnablePoolItem.run(BaseTouchController.java:102)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:54)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:1)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.util.adt.pool.PoolUpdateHandler.onUpdate(PoolUpdateHandler.java:88)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.input.touch.controller.BaseTouchController.onUpdate(BaseTouchController.java:62)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.engine.Engine.onUpdate(Engine.java:584)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
07-02 01:34:44.747: E/AndroidRuntime(19252):    at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)

我的代码中的那部分:

private void createFiller(float x, float y) {
        fillerNum++;
        Log.e("Creating Filler",Integer.toString(fillerNum));
        filler[fillerNum].active=true;
        filler[fillerNum].sprite = new Sprite(x-fillerTR.getWidth()/2,y - fillerTR.getHeight()/2,fillerTR,getVertexBufferObjectManager())
        {
            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY){
                if(pSceneTouchEvent.isActionMove()){
                    setPosition(pSceneTouchEvent.getX() - getWidth() / 2, pSceneTouchEvent.getY() - getHeight() / 2);
                }
                if(pSceneTouchEvent.isActionUp()){
                    Log.e("Action Up",Integer.toString(fillerNum));
                    if(!filler[fillerNum].body.getUserData().equals("destroyed"))//Line 249
                        createStationaryFiller();
                }
                return true;
            }
        };

        scene.registerTouchArea(filler[fillerNum].sprite);

        filler[fillerNum].body = PhysicsFactory.createCircleBody(mPhysicsWorld, filler[fillerNum].sprite, BodyType.DynamicBody, FIXTURE_DEF);
        filler[fillerNum].body.setUserData("fill");
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(filler[fillerNum].sprite, filler[fillerNum].body, true, true));
        scene.attachChild(filler[fillerNum].sprite);
    }

如果需要任何其他说明或代码,请告诉我,我可以发布。我很难过!

1 个答案:

答案 0 :(得分:0)

行号很好,但我怀疑fillerNum大于填充数组的大小。 无论如何,你的堆栈跟踪说第249行抛出了异常,所以找到那一行你会发现你的问题:)