Hello everyone, I looked at the article on Habré ( http://habrahabr.ru/post/136802 ) about creating a simple game and decided to borrow some of the code associated with Enemy, which moves towards the player. I rewrote everything correctly, but the enemies in my case do not appear every n-seconds, but simply continuously create and fill the playing field. How is it possible to make the timer work normally and the enemies appear not without fail but with a certain interval. what is up on the playing field (Play - the enemy) enter image description here

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback, Runnable { public static final int WIDTH = 856; public static final int HEIGHT = 480; private float coordY = 480; private MainThread thread; private Thread thred = new Thread(this); private Background bg; public static Bitmap rocket; private Bitmap rocket2; private Paint paint; private List<Asteroids> asteroids = new ArrayList<Asteroids>(); Bitmap ast; Timer timer = new Timer(); TimerTask timerTask; Timer timer2 = new Timer(); TimerTask timerTask2; public void draw(Canvas canvas) { final float scaleFactorX = getWidth() / WIDTH; final float scaleFactorY = getHeight() / HEIGHT; if (canvas != null) { final int savedState = canvas.save(); canvas.scale(scaleFactorX, scaleFactorY); bg.draw(canvas); canvas.drawBitmap(rocket2, 0, coordY / 2, paint); Iterator<Asteroids> i = asteroids.iterator(); while(i.hasNext()) { Asteroids e = i.next(); if( ex <= 1000) { e.onDraw(canvas); } else { i.remove(); } } canvas.restoreToCount(savedState); } } @Override public void run() { while(true) { Random rnd = new Random(); try { Thread.sleep(rnd.nextInt(2000)); asteroids.add(new Asteroids(this, ast)); } catch (InterruptedException e) { e.printStackTrace(); } } } public void update() { ast = BitmapFactory.decodeResource(getResources(), R.drawable.play); asteroids.add(new Asteroids(this, ast)); bg.update(); } 
  • And where is your timer here? - Android Android
  • @ Android Android, I create it at the very beginning (added to the code) - En1q0d
  • @AndroidAndroid, here, private Thread thred = new Thread (this); - En1q0d
  • You do not use the timer, you have created a separate thread that you sleep for two seconds before adding an object - Android Android
  • @AndroidAndroid, but how should the code be changed to fit the idea? - En1q0d

1 answer 1

Replace your Thred and all that stuff in the run () method with this one, for example. There are many reasons why you should not use TimerTask, but for now let's not talk about it. Periodic actions can be performed using the code below. Initialize the timer, it will start doing its job. After you have decided that it is enough to call the stopTimer method. Please remember for a lifetime - Thread.sleep () is a heresy that should not spread to the masses, and live in someone else’s code =))

 public class GamePanel extends SurfaceView implements SurfaceHolder.Callback, Runnable { . . . ScheduledExecutorService timer; . . . private void launchTimer(){ timer = Executors.newSingleThreadScheduledExecutor(); final Random rnd = new Random(); timer.scheduleWithFixedDelay(new Runnable() { @Override public void run() { asteroids.add(new Asteroids(this, ast)); } }, 0, rnd.nextInt(2000), TimeUnit.MILLISECONDS); } private void stopTimer(){ if(timer != null) timer.shutdown(); } // other code } 
  • Thank you so much, I did the same, but where did we get the shutdown () method? It allocates it in red to me ... - En1q0d
  • edited a post above, I think it will be so clear. All that was needed was to bring the executor to the field - Vladymyr R.tmnko
  • @Vladynyr R.timko, thank you, there is no more error) And here's another question. My designer class Asteroids accepts GamePanel gamepanel and Bitmap bitmap, but with asteroids.add (new Asteroids (this, ast)); My first parameter is Runnable and therefore an error, how can I fix this? - En1q0d
  • @ En1q0d Fix this first parameter on GamePanel.this. I also recommend that you study Java syntax, a very useful thing - Vladymyr R.tmnko
  • @Vladynyr R.timko, and where should I place the launchTimer ()? In update or in draw? - En1q0d