我一直在关注以下教程: http ://www.edu4java.com/en/androidgame/androidgame.html
我现在的立场是,我能够在屏幕上填充各种精灵,每个角色将从一个随机位置开始,他们将朝一个随机方向行走。
我的目标是每 X 秒添加一个新精灵,但我不完全确定如何执行此操作。
目前我的 GameView 是这样设置的:
public class GameView extends SurfaceView {
private GameLoopThread gameLoopThread;
private List<Sprite> sprites = new ArrayList<Sprite>();
private List<tempSprite> temps = new ArrayList<tempSprite>();
private long lastClick;
private Bitmap bmpBlood;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
createSprites();
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.image);
}
private void createSprites() {
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.image));
}
private Sprite createSprite(int resouce) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
return new Sprite(this, bmp);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
for (int i = temps.size() - 1; i >= 0; i--) {
temps.get(i).onDraw(canvas);
}
for (Sprite sprite : sprites) {
sprite.onDraw(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 300) {
lastClick = System.currentTimeMillis();
float x = event.getX();
float y = event.getY();
synchronized (getHolder()) {
for (int i = sprites.size() - 1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
if (sprite.isCollision(x, y)) {
sprites.remove(sprite);
temps.add(new tempSprite(temps, this, x, y, bmpBlood));
break;
}
}
}
}
return true;
}
} `
如您所见,我的精灵是在 createSprites() 中创建的 - 但是,我不确定如何创建一个时间来增加精灵的数量。
有人可以帮忙,因为我不确定如何将其添加到游戏循环中。
谢谢。