1

我是 android 游戏开发的新手,我正在使用 AndEngine GLES 2 Java。我收到一个错误,这与我的数组(face1)具有非法索引有关。我无法解决问题,所以我需要一些帮助。代码:

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
    final float centerX1 = 400;
    final float centerY1 = 50;
    final Sprite[] face1 = new Sprite[i];
    face1[i] =  new Sprite(centerX1, centerY1, this.m2FaceTextureRegion, this.getVertexBufferObjectManager());
    final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
    final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

            if(!face1[i].collidesWith(this))
            {
                x+=1; 
            this.setPosition(x, 50 );
            }
            else
            {
                x=60;
                this.setPosition(x, 50 );
            }
            return true;
        }
    };

    scene.attachChild(face);
    scene.registerTouchArea(face);
    scene.setTouchAreaBindingOnActionDownEnabled(true);
    face.setScale(4);
    scene.attachChild(face1[i]);
    face1[i].setScale(2);


    return scene;
}

错误:

11-11 12:12:42.690: E/AndEngine(13291): java.lang.ArrayIndexOutOfBoundsException

谢谢。

4

1 回答 1

0

从日志看来,异常在com.example.sheeprun1.BaseActivity.onCreateScene(BaseActivity.java:89). 你能检查一下这一行的代码以及那里正在执行什么数组操作......

EDIT1:final Sprite[] face1 = new Sprite[i];此时创建数组时, 的值i不应为零。

当您访问数组中的值时,请始终检查索引是否小于数组的大小/长度

if(face!=null && i<face.length){
face1[i] = new Sprite(centerX1, centerY1, this.m2FaceTextureRegion, this.getVertexBufferObjectManager());
}
于 2012-11-11T13:46:52.930 回答