0
\$\begingroup\$

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();
}
\$\endgroup\$
5
  • \$\begingroup\$ That sounds like a reasonable goal. How have you tried to achieve this goal based on your research and experiments so far? How does the results of your efforts differ from what you want or expect? \$\endgroup\$
    – DMGregory
    May 26, 2022 at 10:30
  • \$\begingroup\$ I tried to use the timer but it conflicts with the other timerspawn. So I can't think of a way to perform this change of position.y and after a return to the original position.y. \$\endgroup\$
    – witsel
    May 26, 2022 at 10:49
  • 1
    \$\begingroup\$ You don't need a timer, you need a game loop to increment your position and progress variables each frame. \$\endgroup\$
    – DMGregory
    May 26, 2022 at 10:58
  • \$\begingroup\$ Alternatively, you can track more than one time elapsed variable. If each enemy needs to track its own elapsed time, then that info should probably be in the enemy class / struct rather than a global. As touched on in the previous comment, it's much easier to manage your updates if they all flow through your game loop. But it's hard to give specific advice without seeing a self contained code, complete code example. \$\endgroup\$
    – Pikalek
    May 26, 2022 at 14:22
  • \$\begingroup\$ Can I see an example with gameloop? Even with pseudo-code, to understand the idea. \$\endgroup\$
    – witsel
    May 26, 2022 at 17:49

0

You must log in to answer this question.

Browse other questions tagged .