0

我正在开发一个由andengine 驱动的box2d 游戏。当我尝试从场景中删除碰撞的物体时,有时会发生以下错误。似乎这是一个随机错误。

Uncaught handler: thread GLThread exiting due to uncaught exception
java.lang.IndexOutOfBoundsException: Invalid location 48, size is 48
at java.util.ArrayList.get(ArrayList.java:341)
at org.anddev.andengine.entity.Entity.onManagedDraw(Entity.java:835)
at org.anddev.andengine.entity.shape.Shape.onManagedDraw(Shape.java:105)
at org.anddev.andengine.entity.Entity.onDraw(Entity.java:719)
at org.anddev.andengine.entity.Entity.onManagedDraw(Entity.java:835)
at org.anddev.andengine.entity.scene.Scene.onManagedDraw(Scene.java:271)
at org.anddev.andengine.entity.Entity.onDraw(Entity.java:719)
at org.anddev.andengine.engine.Engine.onDrawScene(Engine.java:507)
at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:499)
at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:148)
at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:617)
at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.run(GLSurfaceView.java:549)

以下是我的更新代码块:

scene.registerUpdateHandler(new IUpdateHandler(){

            public void onUpdate(float pSecondsElapsed) {
                // TODO Auto-generated method stub
                try{
                scoreA = "SCORE  : "+p1score;
                hudTxtScoreA.setText(scoreA);
                scoreB = "SCORE  : "+p2score;
                hudTxtScoreB.setText(scoreB);
                if(p1score==900)
                {
                    hudTxtScoreA.setText("won");
                    runOnUiThread(new Runnable() {
                        public void run() {



                            AlertDialog.Builder builder = new AlertDialog.Builder(GmailGame.this);
                            builder.setMessage(playerA+" won the game !!!")
                                   .setCancelable(false)
                                   .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                       public void onClick(DialogInterface dialog, int id) {
                                           startActivity(new Intent(GmailGame.this, JoinGame.class));
                                           GlobalClass.setPartner("");

                                       }
                                   });
                            AlertDialog alert = builder.create();

                            alert.show();
                        }
                    });

                    //mEngine.stop();
                }
                else if(p2score==900)
                {
                    hudTxtScoreB.setText("won");

                    runOnUiThread(new Runnable() {
                        public void run() {



                            AlertDialog.Builder builder = new AlertDialog.Builder(GmailGame.this);
                            builder.setMessage(playerB+" won the game !!!")
                                   .setCancelable(false)
                                   .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                       public void onClick(DialogInterface dialog, int id) {
                                           startActivity(new Intent(GmailGame.this, JoinGame.class));
                                           GlobalClass.setPartner("");

                                       }
                                   });
                            AlertDialog alert = builder.create();

                            alert.show();
                        }
                    });

                    //mEngine.stop();
                }

                if(x1!=null)
                {   
                    if(status.equals("1")){
                        isMyTurn=true;
                        status="0";
                    }



                if((x1.getBody().getUserData().toString().equals("cat1"))||(x2.getBody().getUserData().toString().equals("cat1"))){
                    if(icat1==0){
                        icat1=1;
                Log.e("suchith", x1.getBody().getUserData().toString());

                String x1String = x1.getBody().getUserData().toString();
                String x2String = x2.getBody().getUserData().toString();

                mPhysicsWorld.destroyBody(bcat1);
                scene.getLastChild().detachChild(scat1);


                p1score=p1score+100;
                //scene.detachChild(scat1);

                if(isMyTurn)
                sendMessage1(x1String,x2String,"body");

                    }

                }
                else if((x1.getBody().getUserData().toString().equals("cat2"))||x2.getBody().getUserData().toString().equals("cat2")){
                    if(icat2==0){
                        icat2=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(bcat2);   
                    scene.getLastChild().detachChild(scat2);
                    p1score=p1score+100;
                    p1win=p1win+1;
                    //scene.detachChild(scat2);

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");

                    }
                    }
                else if((x1.getBody().getUserData().toString().equals("dog1"))||(x2.getBody().getUserData().toString().equals("dog1"))){
                    if(idog1==0)
                    {
                        idog1=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(bdog1);
                    scene.getLastChild().detachChild(sdog1);
                    //scene.detachChild(sdog1);
                    p1score=p1score+200;
                    p1win=p1win+1;

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");

                    }
                    }
                else if((x1.getBody().getUserData().toString().equals("dog2"))||x2.getBody().getUserData().toString().equals("dog2")){
                    if(idog2==0){
                        idog2=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(bdog2);   
                    scene.getLastChild().detachChild(sdog2);
                    //scene.detachChild(sdog2);
                    p1score=p1score+200;
                    p1win=p1win+1;

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");
                }
                    }
                else if((x1.getBody().getUserData().toString().equals("dog3"))||x2.getBody().getUserData().toString().equals("dog3")){
                    if(idog3==0){
                        idog3=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(bdog3);
                    scene.getLastChild().detachChild(sdog3);
                    //scene.detachChild(sdog2);
                    p1score=p1score+300;
                    p1win=p1win+1;

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");
                }
                    }
                if((x1.getBody().getUserData().toString().equals("ecat1"))||(x2.getBody().getUserData().toString().equals("ecat1"))){
                    if(iecat1==0){
                        iecat1=1;
                Log.e("suchith", x1.getBody().getUserData().toString());

                String x1String = x1.getBody().getUserData().toString();
                String x2String = x2.getBody().getUserData().toString();

                mPhysicsWorld.destroyBody(becat1);
                scene.getLastChild().detachChild(secat1);
                p2score=p2score+100;
                p2win=p2win+1;
                //scene.detachChild(scat1);

                if(isMyTurn)
                sendMessage1(x1String,x2String,"body");

                    }

                }
                else if((x1.getBody().getUserData().toString().equals("ecat2"))||x2.getBody().getUserData().toString().equals("ecat2")){
                    if(iecat2==0){
                        iecat2=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(becat2);  
                    scene.getLastChild().detachChild(secat2);
                    p2score=p2score+100;
                    p2win=p2win+1;
                    //scene.detachChild(scat2);

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");

                    }
                    }
                else if((x1.getBody().getUserData().toString().equals("edog1"))||(x2.getBody().getUserData().toString().equals("edog1"))){
                    if(iedog1==0)
                    {
                        iedog1=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(bedog1);
                    scene.getLastChild().detachChild(sedog1);
                    //scene.detachChild(sdog1);
                    p2score=p2score+200;
                    p2win=p2win+1;

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");

                    }
                    }
                else if((x1.getBody().getUserData().toString().equals("edog2"))||x2.getBody().getUserData().toString().equals("edog2")){
                    if(iedog2==0){
                        iedog2=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(bedog2);  
                    scene.getLastChild().detachChild(sedog2);
                    //scene.detachChild(sdog2);
                    p2score=p2score+200;
                    p2win=p2win+1;

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");
                }
                    }
                else if((x1.getBody().getUserData().toString().equals("edog3"))||x2.getBody().getUserData().toString().equals("edog3")){
                    if(iedog3==0){
                        iedog3=1;
                    Log.e("suchith", x1.getBody().getUserData().toString());

                    String x1String = x1.getBody().getUserData().toString();
                    String x2String = x2.getBody().getUserData().toString();

                    mPhysicsWorld.destroyBody(bedog3);
                    scene.getLastChild().detachChild(sedog3);
                    //scene.detachChild(sdog2);
                    p2score=p2score+300;
                    p2win=p2win+1;

                    if(isMyTurn)
                    sendMessage1(x1String,x2String,"body");
                }
                    }                   

                }           
                }
                catch(NullPointerException e)
                {
                    Log.e("Update","Null");
                }
                catch(Exception e){
                                            Log.e("Udate","Error");
                }
            }
            public void reset() {
                // TODO Auto-generated method stub

            }
        });
        return scene;
    }

请帮忙。

4

1 回答 1

2

您正在删除 UI 线程上的正文。这意味着在绘制场景时,它会尝试绘制不再附加到场景的东西,并且您会得到 ArrayIndexOutOfBoundsException。如果您使用 runOnUpdateThread 而不是 runOnUiThread,则应该没有问题。

于 2012-06-10T18:24:09.920 回答