问题描述
我一直关注以下教程: http://www.edu4java.com/en/androidgame/androidgame.html
I have been following along with the tutorials from:http://www.edu4java.com/en/androidgame/androidgame.html
我现在的立场是,我可以在屏幕上填充各种精灵,每个角色都将从一个随机位置开始,并且它们将沿随机方向行走.
Where I stand now is that I am able to populate the screen with a variety of sprites and each character will begin at a random position and they will walk in a random direction.
我的目标是每X秒钟添加一个新的精灵,但是我不确定如何做到这一点.
My objective is to add a new sprite every X amount of seconds but I am not entirely sure how to do this.
目前,我的GameView设置如下:
Currently my GameView is set up like this:
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()中创建的-但是,我不确定如何创建时间来增加精灵的数量.
As you can see, my sprites are being created in createSprites() - however, I am unsure how to create a time to increase the number of sprites.
有人可以帮忙,因为我不确定如何将其添加到游戏循环中.
Could someone please help as I am not sure how to add this into the game loop.
谢谢.
推荐答案
您可以使用Thread创建新的精灵,并在循环中,在调用方法声明和将新的精灵添加到Sprite之间添加Thread.sleep(1000)行.数组列表.就像:
You can use a Thread for creation of new sprites and in a loop, add Thread.sleep(1000) line between invoke method declaration and adding new sprite to sprites ArrayList. Just like:
private void createSprites(int x) {
new Thread(new Runnable() {
public void run() {
for(int i = 0; i < x; i++) {
sprites.add(createSprite(R.drawable.image));
try
{
Thread.sleep(1000); //Waits for 1 second
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
}
其中x是秒的变量.因此,当您的游戏仍在后台运行时,这将在第二次通过时创建X个精灵.
where x is the variable of seconds. So this will create X sprites as a second passes, while your game is still working on background.
这篇关于每x秒添加一个新敌人-Android游戏开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!