Android Game Java 从链表中删除 Bitmap



因此,我的画布上有各种位图,这些位图作为单独的类进行动画处理。

当位图

离开屏幕时,我希望从链接列表中删除位图。 它们位于链接列表中,因为屏幕上可以有多个相同的位图。

位图正在从屏幕上删除,但它仍在链接列表中,因为链接列表大小为 1。

如果我在链接列表中添加多个位图,它们都会被绘制,但是当屏幕打开时,游戏崩溃。

面板类。

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);
                }
            }
        }

现在的类。

    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;
    }
}

任何帮助,不胜感激。

我认为这些行可能会导致问题:

        for (Present presents : mPresents) {
            boolean remove = presents.animate(elapsedTime);
            if (remove) {
                mPresents.remove(presents);
            }
        }

由于您正在尝试从"mPresents"中删除"礼物",而您在列表的该元素上。我可能是错的,但我认为这有点像试图吃掉自己的头。创建另一个列表(或只是另一个 Present 对象)可能会有所帮助,然后在循环完成后将其从列表中删除。例如,像这样:

ArrayList <Present> presentsToRemove = new ArrayList<Present>();
for (Present presents : mPresents) {
     if (presents.animate(elapsedTime)) {
           presentsToRemove.add(presents);
     }
}
mPresents.removeAll(presentsToRemove); 

请让我知道这是否有帮助,或者我是否误解了。

相关内容

  • 没有找到相关文章

最新更新