I have a array of sprite in a class "Enemy", I want to draw enemies in some positions, but when I draw one, the previous one disappears.

public class Level1 implements Screen{

   Vector2 position;
   private Enemy enemies = new Enemy();

   public Level1 (NameGame game){
      this.game = game;
      position = new Vector2(300,100);

And this is my for loop:

    for (i=0;i<enemies.enemy.size;i++){
    //Maybe i need for (Sprite s : enemy)

It's only a test to draw enemy, after i will try to draw in more position

If i change position.x and position.y every iteration, for example with position = new Vector2(..,..) after the "game.batch.draw", the previous enemy disappear, but why? How can i draw all?

And this is my enemy class:

package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.utils.Array;

public class Enemy {
    public Array<Sprite> enemy = new Array<Sprite>(14);   

public void nemici() {
    //Inizializzo e aggiungo i nemici all'array
    Sprite mysterio, boss, doctorstrange, electro, goblin, ironman, lizard, mj, ned, octopus, rhyno, sandman, scorpion, scorpion2, venom;
    enemy.add(mysterio = new Sprite(new Texture("mysterio.png")));
    enemy.add(boss = new Sprite(new Texture("boss.png")));
    enemy.add(doctorstrange = new Sprite(new Texture("doctorstrange.png")));
    enemy.add(electro = new Sprite(new Texture("electro.png")));
    enemy.add(goblin = new Sprite(new Texture("goblin.png")));
    enemy.add(ironman = new Sprite(new Texture("ironman.png")));
    enemy.add(lizard = new Sprite(new Texture("lizard.png")));
    enemy.add(mj = new Sprite(new Texture("mj.png")));
    enemy.add(ned = new Sprite(new Texture("ned.png")));
    enemy.add(octopus = new Sprite(new Texture("octopus.png")));
    enemy.add(rhyno = new Sprite(new Texture("rhyno.png")));
    enemy.add(sandman = new Sprite(new Texture("sandman.png")));
    enemy.add(scorpion = new Sprite(new Texture("scorpion.png")));
    enemy.add(scorpion2 = new Sprite(new Texture("scorpion2.png")));
    enemy.add(venom = new Sprite(new Texture("venom.png")));       


  • \$\begingroup\$ Looks like you should show us your Enemy class. To get help with debugging, you need to share a Minimal Complete Verifiable Example — that is, every line of code needed to reproduce the problem. \$\endgroup\$
    – DMGregory
    May 19, 2022 at 12:23
  • \$\begingroup\$ Your code is not what I suggested yesterday. \$\endgroup\$
    – Vaillancourt
    May 19, 2022 at 13:31
  • \$\begingroup\$ Because i am testing to use what you recommended, but i made several mistakes \$\endgroup\$
    – witsel
    May 19, 2022 at 15:06

1 Answer 1


You have many enemy sprites, but only one position variable.

In your loop, you take turns to draw every sprite at the same position, so they stack up and you see only one.

You do not want an instance of the enemy class to have several sprites but no position, while the single enemy position is owned by the level. This makes no logical sense whatsoever.

What you want is that a level contains many enemies, and each enemy has one sprite and one position of its own.

public class Enemy {
    public Sprite sprite;  
    public Vector2 position;

    public Enemy(Sprite sprite, Vector2 position) {
        this.sprite = sprite;
        this.position = position;

public class Level1 implements Screen {

    private Array<Enemy> enemies;  

    public void makeEnemies() {

        enemies = new Array<Enemy>(14);            
        enemies.add(new Enemy(
               new Sprite(new Texture("mysterio.png")), 
               new Vector2(100, 100)

        enemies.add(new Enemy(
               new Sprite(new Texture("boss.png")), 
               new Vector2(200, 100)
        // ...

Then your loop draws each enemy at its own position:

for (i=0;i < enemies.size; i++){
    Enemy enemy = enemies.get(i);
        enemy.position.x, enemy.position.y,
        100,              100
  • \$\begingroup\$ I don't know why the array is empty. I'm trying from today to understand what is the problem. \$\endgroup\$
    – witsel
    May 19, 2022 at 21:18
  • \$\begingroup\$ Did you call makeEnemies()? \$\endgroup\$
    – DMGregory
    May 19, 2022 at 21:33
  • \$\begingroup\$ I tried with enemies.size but the error is: "Exception in thread "main" java.lang.NullPointerException: Cannot read field "size" because "this.enemies" is null", but i think that this.enemies means this.sprite \$\endgroup\$
    – witsel
    May 19, 2022 at 21:38
  • \$\begingroup\$ You did not answer the question I asked you. \$\endgroup\$
    – DMGregory
    May 19, 2022 at 21:49
  • \$\begingroup\$ no, as I told you I used enemies.size \$\endgroup\$
    – witsel
    May 19, 2022 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .