Thursday, April 12, 2012

Android Game Java removing a Bitmap from a linked list

So, I have a various Bitmaps on my canvas that are animated as individual classes.


When the Bitmap goes off screen i want the Bitmap to be removed from the linkedlist. they are in a linkedlist as there can be more than one of the same bitmap on the screen.


The bitmap is removing from the screen but it is still in the linkedlist as the linkedlist size is 1.


If i add more than one bitmap to the linkedlist they both get drawn but when on goes of the screen the game crashes.


Panel Class.


public void doDraw(long elapsed, long score, Canvas canvas) {
         canvas.drawBitmap(background, 0, 0, null); // draw a black background
         synchronized (mChimneys) {
 
             if ((mPresents.size() < 3)) {
                 for (Present presents : mPresents) {
                     presents.doDraw(canvas);
                 }
             }
         }
 
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         synchronized (mChimneys) {
 
             float x = event.getX();
             float y = event.getY();
 
             float mX = 0;
             float mY = 0;
 
             int mWidth = 0;
             int mHeight = 0;
 
             for (Santa santas : mSantas) {
                 mX = santas.getmX();
                 mY = santas.getmY();
                 mWidth = santas.getmBitmap().getWidth();
                 mHeight = santas.getmBitmap().getHeight();
             }
 
             switch (event.getAction()) {
 
             case MotionEvent.ACTION_DOWN:
                 if (x >= mX && x < (mX + mWidth) && y >= mY
                         && y < (mY + mHeight)) {
                     if ((mPresents.size() < 4)) {
                         mPresents.add(new Present(getResources(), mX, mY));
                         mPresentNumber = mPresents.size();
                     }
                 }
             }
         }
         return true;
     }
 
 public void animate(long elapsedTime) {
         synchronized (mChimneys) {
 
             for (Present presents : mPresents) {
                 boolean remove = presents.animate(elapsedTime);
                 if (remove) {
                     mPresents.remove(presents);
                 }
             }
         }
 


Present Class.


    package com.droidnova.android;
 
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Random;
 
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 
 public class Present {
     private float mX;
     private float mY;
 
     private Bitmap mBitmap;
 
     private int mSpeedY;
 
     Panel panel;
 
     public Present(Resources res, float x, float y) {
         Random rand = new Random();
 
         List<Integer> my_presents = new LinkedList<Integer>();
 
         my_presents.add(R.drawable.presentblue);
         my_presents.add(R.drawable.presentpurple);
         my_presents.add(R.drawable.presentred);
         my_presents.add(R.drawable.presentyellow);
 
         int choice = rand.nextInt(my_presents.size());
 
         mBitmap = BitmapFactory.decodeResource(res, my_presents.get(choice));
         mX = x + 50;
         mY = y + 100;
         mSpeedY = 5;
     }
 
     public void doDraw(Canvas canvas) {
         canvas.drawBitmap(mBitmap, mX, mY, null);
     }
 
     public boolean animate(long elapsedTime) {
         mY += mSpeedY * (elapsedTime / 20f);
         boolean remove = checkBorders();
         if (remove) {
             return true;
         }
         return false;
     }
 
     private boolean checkBorders() {
         if (mY + mBitmap.getHeight() >= Panel.mHeight) {
             return true;
             // mSpeedY = -mSpeedY;
             // mY = Panel.mHeight - mBitmap.getHeight();
         }
         return false;
     }
 }
 


Any help is appreciated.



Answer:

I think these lines may cause a problem:
        for (Present presents : mPresents) {
            boolean remove = presents.animate(elapsedTime);
            if (remove) {
                mPresents.remove(presents);
            }
        }
Since you are trying to remove "presents" from the "mPresents", while you are on that element of the list. I could be wrong, but I think this is a bit like trying to eat your own head. It might help to create another list (or just another Present object), and then remove that from the list after the loop is finished. For example, something like this:
ArrayList <Present> presentsToRemove = new ArrayList<Present>();
for (Present presents : mPresents) {
     if (presents.animate(elapsedTime)) {
           presentsToRemove.add(presents);
     }
}
mPresents.removeAll(presentsToRemove); 
Please let me know if this helps, or if I've misunderstood.

No comments:

Post a Comment