There is an application, a flappy bird clone, at death, a file is played. It is required to make so that not one file is played, but random from the list.

Code:

// столкновение с землей if (Intersector.overlaps(fly.getCircle(), ground)) { if (fly.isAlive()) { ResourseLoader.dead.play(); // 1 трек ResourseLoader.dead2.play(); // 2 трек ResourseLoader.dead3.play(); // 3 трек fly.die(); renderer.prepareTransition(255, 255, 255, 0.3f); } movHandler.stop(); currentState = GameState.GAMEOVER; //лучший результат highScore(); } 

How to implement it?

    2 answers 2

    Solution to the forehead:

      if (Intersector.overlaps(fly.getCircle(), ground)) { if (fly.isAlive()) { switch (new Random().nextInt(3)){ case 0: ResourseLoader.dead.play(); // 1 трек break; case 1: ResourseLoader.dead2.play(); // 2 трек break; case 2: ResourseLoader.dead3.play(); // 3 трек } fly.die(); renderer.prepareTransition(255, 255, 255, 0.3f); } 

      If there are a lot of random options, then swich will become inconvenient to use. Then you can create an array of resources.

      Somewhere it is initiated:

       Array<Sound> sounds = new Array<Sound>(); sounds.add(dead); sounds.add(dead2); // и так далее, можно в цикле 

      A random number will be an index to the array:

       if (Intersector.overlaps(fly.getCircle(), ground)) { if (fly.isAlive()) { sounds.get(MathUtils.random(0, sounds.size-1)).play(); fly.die(); renderer.prepareTransition(255, 255, 255, 0.3f); } movHandler.stop(); currentState = GameState.GAMEOVER; //лучший результат highScore(); }