I want to make the selected enemy increase position.y by 1.0 and after 1.5 seconds return to the original position. I already use a timer of 2 seconds because I need to select a random enemy every 2 seconds, and then increase the position. My code:
public static int getRandomElement(int[] arr){
return arr[ThreadLocalRandom.current().nextInt(arr.length)];
}
public static int[] deleteRandomElement(int[] arr, int index){
int[] arr_new = new int[arr.length-1];
for(int i=0,k=0; i<arr.length; i++){
if(i!=index){
arr_new[k]=arr[i];
k++;
}
}
return arr_new;
}
public void makeEnemies() {
enemies = new Array<Enemy>(9);
enemies.add(new Enemy(new Sprite(new Texture("mysterio.png")), position1));
enemies.add(new Enemy(new Sprite(new Texture("ironman.png")), position2));
enemies.add(new Enemy(new Sprite(new Texture("rhyno.png")), position3));
enemies.add(new Enemy(new Sprite(new Texture("venom.png")), position4));
enemies.add(new Enemy(new Sprite(new Texture("sandman.png")), position5));
enemies.add(new Enemy(new Sprite(new Texture("scorpion.png")), position6));
enemies.add(new Enemy(new Sprite(new Texture("octopus.png")), position7));
enemies.add(new Enemy(new Sprite(new Texture("mj.png")), position8));
enemies.add(new Enemy(new Sprite(new Texture("ned.png")), position9));
}
private void makeAnimation(){
enemies.get(RandNumPick).position.y += 1.2; //up
//enemies.get(RandNumPick).position.y -= 1.2; //??down
}
@Override
public void show() {
BackgroundLevel1 = new Texture("BackgroundLevel1.png");
//spawn
position1 = new Vector2(115,180);
position2 = new Vector2(365,165);
position3 = new Vector2(535,165);
position4 = new Vector2(793,295);
position5 = new Vector2(793,450);
position6 = new Vector2(735,450);
position7 = new Vector2(485,540);
position8 = new Vector2(900,440);
position9 = new Vector2(365,285);
position10 = new Vector2(365,300);
timerspawn = 0;
timerbonus = 0;
RandNumPick = getRandomElement(numpersonaggi);
makeEnemies();
}
@Override
public void render(float f) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(TimeUtils.nanosToMillis(TimeUtils.nanoTime()) - timerspawn >= 2000){
timerspawn = TimeUtils.nanosToMillis(TimeUtils.nanoTime());
//Copy of array without the random number (so no two random ones will be alike)
int[] numpersonaggi_new = deleteRandomElement(numpersonaggi,RandNumPick);
RandNumPick = getRandomElement(numpersonaggi_new);
}
makeAnimation();
game.batch.begin();
//Background
game.batch.draw(BackgroundLevel1, 0, 0, SpiderEnemies.WIDTH, SpiderEnemies.HEIGHT);
//Creo un ciclo for per stampare gli enemy (sprite)
for (i=0;i < enemies.size; i++){
Enemy enemy = enemies.get(i);
game.batch.draw(enemy.sprite,enemy.position.x,enemy.position.y,100,100);
}
game.batch.end();
}