所以这是我的代码,
public class GameView extends SurfaceView {
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private List<Sprite> sprites = new ArrayList<Sprite>();
private long lastClick;
public int d = 0;
public int color;
TextView tv;
public int score;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
createSprites();
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int arg2, int height) {
}
});
}
private void createSprites() {
int c = 10;
{
Random rnd = new Random();
color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),
rnd.nextInt(256));
for (int b = 1; b <= c; b++) {
int random = (int) Math.ceil(Math.random() * 24);
if (random == 1) {
sprites.add(createSprite(R.drawable.bad1));
} else if (random == 2) {
sprites.add(createSprite(R.drawable.bad2));
} else if (random == 3) {
sprites.add(createSprite(R.drawable.bad3));
} else if (random == 4) {
sprites.add(createSprite(R.drawable.bad4));
} else if (random == 5) {
sprites.add(createSprite(R.drawable.bad5));
} else if (random == 6) {
sprites.add(createSprite(R.drawable.bad6));
} else if (random == 7) {
sprites.add(createSprite(R.drawable.bad7));
} else if (random == 8) {
sprites.add(createSprite(R.drawable.bad8));
} else if (random == 9) {
sprites.add(createSprite(R.drawable.bad9));
} else if (random == 10) {
sprites.add(createSprite(R.drawable.bad10));
} else if (random == 11) {
sprites.add(createSprite(R.drawable.bad11));
} else if (random == 12) {
sprites.add(createSprite(R.drawable.bad12));
} else if (random == 13) {
sprites.add(createSprite(R.drawable.bad13));
} else if (random == 14) {
sprites.add(createSprite(R.drawable.bad14));
} else if (random == 15) {
sprites.add(createSprite(R.drawable.bad15));
} else if (random == 16) {
sprites.add(createSprite(R.drawable.bad16));
} else if (random == 17) {
sprites.add(createSprite(R.drawable.bad17));
} else if (random == 18) {
sprites.add(createSprite(R.drawable.good1));
} else if (random == 19) {
sprites.add(createSprite(R.drawable.good2));
} else if (random == 20) {
sprites.add(createSprite(R.drawable.good3));
} else if (random == 21) {
sprites.add(createSprite(R.drawable.good4));
} else if (random == 22) {
sprites.add(createSprite(R.drawable.good5));
} else if (random == 23) {
sprites.add(createSprite(R.drawable.good6));
} else if (random == 24) {
sprites.add(createSprite(R.drawable.good7));
}
}
}
}
private Sprite createSprite(int resource) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
return new Sprite(this, bmp);
}
@SuppressLint({ "WrongCall", "DrawAllocation" })
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(color);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
canvas.drawText("SCORE " + score, 10, 10, paint);
for (Sprite sprite : sprites) {
sprite.onDraw(canvas);
}
}
// this is the ontouch event to destroy the sprites and make the blood splat
// effect
@Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 200) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
float x = event.getX();
float y = event.getY();
for (int i = sprites.size() - 1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
if (sprite.isCollition(x, y)) {
{
if ((sprites).equals (R.drawable.bad1))
score = score + 5;
else if ((sprites).equals(R.drawable.bad2))
score = score + 5;
else if ((sprites).equals(R.drawable.bad3))
score = score + 5;
else if ((sprites).equals(R.drawable.bad4))
score = score + 5;
else if ((sprites).equals(R.drawable.bad5))
score = score + 5;
else if ((sprites).equals(R.drawable.bad6))
score = score + 5;
else if ((sprites).equals(R.drawable.bad7))
score = score + 5;
else if ((sprites).equals(R.drawable.bad8))
score = score + 5;
else if ((sprites).equals(R.drawable.bad9))
score = score + 5;
else if ((sprites).equals(R.drawable.bad10))
score = score + 5;
else if ((sprites).equals(R.drawable.bad11))
score = score + 5;
else if ((sprites).equals(R.drawable.bad12))
score = score + 5;
else if ((sprites).equals(R.drawable.bad13))
score = score + 5;
else
score = score - 5;
}
d++;
if (d >= 10) {
d = 0;
createSprites();
}
break;
}
}
}
}
return true;
}
}
我想做的是得到,
if ((sprites).equals(R.drawable.bad1))
score = score + 5;
要检查此代码中是否有某处,
@Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 200) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
float x = event.getX();
float y = event.getY();
for (int i = sprites.size() - 1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
保存正在删除的其中一张图片的值,但我不确定如何正确编码。我不确定每次随机发生器运行时是否需要将图片放入数组中,或者代码取自 youtube 的“edu4java”教程。
我有一个循环的程序,你可以告诉我我可以删除触摸的照片并且分数是正确的我只是不知道如何获得,
if ((sprites).equals(R.drawable.bad1))
score = score + 5;
检查以“查看”正确的图片字符串。我是否需要检查代码“自动创建”的数组?有没有办法检查字符串的值是什么?例如“看到”“精灵”或“精灵”实际上“持有”的是什么?