Adding a new enemy every x amount of seconds - Android Game Dev

泪湿孤枕 提交于 2019-12-11 17:53:11

问题


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.

My objective is to add a new sprite every X amount of seconds but I am not entirely sure how to do this.

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

} `

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.

Thanks.


回答1:


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

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.




回答2:


Create another thread, timer task, or handler and have that run periodically. Make sure you synchronize your collection properly and only call re draw on the GUI thread.



来源:https://stackoverflow.com/questions/19965802/adding-a-new-enemy-every-x-amount-of-seconds-android-game-dev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!