所以,我的画布上有各种位图,它们作为单独的类进行动画处理。
当位图离开屏幕时,我希望从链接列表中删除位图。它们位于链表中,因为屏幕上可能有多个相同的位图。
位图正在从屏幕上移除,但它仍在链表中,因为链表大小为 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;
}
}
任何帮助表示赞赏。