开发者

Remove item from ArrayList, ArrayIndexOutOfBounds exception

开发者 https://www.devze.com 2023-02-28 16:05 出处:网络
I currently have an arraylist in which I add \"blossom\" objects ( which fall from the top of the screen ). When there is a collision between the blossom and a box at the bottom of the screen, I want

I currently have an arraylist in which I add "blossom" objects ( which fall from the top of the screen ). When there is a collision between the blossom and a box at the bottom of the screen, I want to remove the blossom from the ArrayList. however, the way I'm doing it now (in the blossom class), I get an ArrayIndexOutOfBounds exception. Can anybody see where i'm going wrong? I know it has to do with my threads, I just can't figure out WHERE to put this method. (This is being performed in the "hit" method of my Blossom class).

BoardView

    public BoardView(Context context){
    super(context);

    scorePaint.setColor(Color.BLACK);
    scorePaint.setTextSize(12);
    scorePaint.setTypeface(Typeface.MONOSPACE);


    //surfaceHolder provides canvas that we draw on
    getHolder().addCallback(this);

    // controls drawings
    thread = new BoardThread(getHolder(),this, blossomArrayList, box_x, box_y, 
            boxWidth, boxHeight);

    timer = new Thread(){
        public void run(){
            //makes sure the player still has 3 lives left
            while(game == false){
                uiCallback.sendEmptyMessage(0);
                try {
                    Thread.sleep(2000); // wait two seconds before drawing the next flower
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //sleep for 2 seconds
            }
        }
    };
    timer.start();

    //intercepts touch events
    setFocusable(true);

}

@Override

public void onDraw(Canvas canvas){
    canvas.drawColor(Color.WHITE);
    score = "SCORE: " + currentScore;

    //note: pay attention to order you draw things
    //don't change order or else blossoms will fall
    //on top of box, not "into" it.

    //display the scoreboard
    canvas.drawText(score,240,420,scorePaint);
    // uses a synchronized method to prevent concurrent modification
    DrawBlossoms(canvas);
    canvas.drawBitmap(box, box_x, box_y, null);

}

@Override
public boolean onTouchEvent(MotionEvent event){
    //handles movement of box
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        if(event.getX() > box_x & event.getY() > box_y & 
                event.getX() < box_x + boxWidth & event.getY() < box_y + boxHeight)
        {
            mode = true;
        }
    }

    if(event.getAction() == MotionEvent.ACTION_MOVE) {
        if(event.getX() > box_x & event.getY() > box_y & 
                event.getX() < box_x + boxWidth & event.getY() < box_y + boxHeight)
        {
            mode = true;
        }
        if(mode == true){
            box_x = (int)event.getX();
        }   

    }

    if(event.getAction() == MotionEvent.ACTION_UP){
        mode = false;
    }

    invalidate();
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, 
        int format, int width, int height ){
    Log.v(TAG, "Surface Changed");
    //somehow these don't seem to be working
}

@Override
public void surfaceCreated(SurfaceHolder holder){
    thread.startRunning(true);
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    Log.v(TAG, "Surface Destroyed");
    //somehow these don't seem to be working
    thread.startRunning(false);
    thread.stop();
    timer.interrupt();
    timer.stop();
}

private Handler uiCallback = new Handler(){
    public synchronized void handleMessage(Message msg){
        //add a new blossom to the blossom ArrayList!!
        blossomArrayList.add(new Blossom( 
            (BitmapFactory.decodeResource
                    (getResources(), R.drawable.blossom))));
        blossomNum++;

        //remove neccesary blossoms from list
        Log.v(TAG, "Number of Blossoms =" + blossomNum);
    }
};

private synchronized void DrawBlossoms(Canvas c)
{
    Canvas canvas = c;
    for(Blossom blossom: blossomArrayList)   // getting errors here
    {
            blossom.Draw(canvas);
            blossom.hit(box_x,box_y, box_x + boxWidth, box_y + boxHeight, blossomArrayList);

    }
}

}

Blossom class

public Blossom(Bitmap bitmap)
{
    blossom = bitmap;
    blossomWidth = blossom.getWidth();
    blossomHei开发者_开发知识库ght = blossom.getHeight();
    blossom_x = generator.nextInt(320-blossom.getWidth());
    blossomRect = new RectF(blossom_x,blossom_y, blossom_x + blossomWidth, blossom_y + blossomHeight);
}

public Bitmap getBitmap()
{
    return blossom;
}

public Boolean hit(int boxLeft, int boxTop, int boxRight,int boxBottom, ArrayList<Blossom> blossomArrayList)
{
    if(remove == true)
    {
        Log.v(TAG, "Remove = true");
        blossomArrayList.remove(removeInt);
        remove = false;
    }
    if(blossom_x > boxLeft & blossom_y > boxTop
            & blossom_x + blossom.getWidth() < boxRight
            & blossom_y + blossom.getHeight() < boxBottom)
    {
        Log.v(TAG, "Collision Detected");
        remove = true;
        removeInt = blossomArrayList.indexOf(blossom);

        return true;
    }
    else
    {
        return false;
    }
}

public float getBlossomX()
{
    return blossom_x;
}

public float getBlossomY()
{
    return blossom_y;
}

public float getBlossomWidth()
{
    return blossomWidth;
}

public float getBlossomHeight()
{
    return blossomHeight;
}

public void Draw(Canvas canvas)
{
    //draws the flower falling
    if (hit == false)
    {
        canvas.drawBitmap(blossom, blossom_x,
            blossom_y = blossom_y+5 , null);
    }

}

public void UpdatePosition()
{
    blossomRect.set(blossom_x, blossom_y, blossom_x + 25, blossom_y + 25);
}

}

Board Thread

    public BoardThread(SurfaceHolder holder, BoardView boardView2, 
        ArrayList<Blossom> blossomArrayList1,
        int box_x, int box_y, int boxW, int boxH) {

    surfaceHolder = holder;
    boardView=boardView2;

    blossomArrayList = blossomArrayList1;
    boxX = box_x;
    boxY = box_y;
    boxW = boxWidth;
    boxH = boxHeight;
}

public void startRunning(boolean run) {

    mrun=run;
}

@Override
public void run() {

    super.run();
     Canvas canvas;
     while (mrun) {
        canvas=null;
         try {
             canvas = surfaceHolder.lockCanvas(null);
              synchronized (surfaceHolder) {
                 //update position
                 Position(blossomArrayList, boxX, boxY, boxWidth, boxHeight);
                 // draw flowers
                 boardView.onDraw(canvas);   // and getting errors here - concurrent 
             }
         } finally {
                 if (canvas != null) {
                 surfaceHolder.unlockCanvasAndPost(canvas);
             }
         }
     }
  }

private synchronized void Position(ArrayList<Blossom> blossomArrayList, int box_x, int box_y, 
        int boxWidth, int boxHeight)
{
    for(Blossom blossom: blossomArrayList)
    {
        blossom.UpdatePosition();
    }
}

}


The problem is that, you are providing an integer as a parameter to remove method while it is considering it as index of arrayList.

ArrayList provides two methods that are:

public E remove(int index)
public boolean remove(Object o)

What you can do is:
1) create an arrayList of type string.
2) cast your int to string and use it as-

blossomArrayList.remove(String.valueOf(removeInt));


Hope it helps..


Assuming that you get the error on blossomArrayList.remove(removeInt);, you could try to remove the object itself as .remove(..) can take an Object parameter.


Adding a cast to mark the int as an object worked for me. blossomArrayList.remove((Object)myint);

this way it doesn't get confused with index.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号