I'm creating a simple game for my coursework. I have an array list containing different types of enemies made from different classes I have created. All the objects have a method called drawEnemy which draws the object to the screen (so the function drawEnemy() is in every class). I am trying to draw all the objects in the array in the draw loop.
ArrayList<Object> enemies;
enemies.add(new enemy1())
enemies.add(new enemy2())
enemies.add(new enemy3())
void draw(){
for(Object i : enemies)
i.drawEnemy();
}
This tells me that the function drawEnemy does not exist.
Object. You want anArrayList<Enemy>, and then iterate usingfor(Enemy e: enemeies). Now you can call any method you defined in your Enemy class, because the compiled knows those methods exist. But don't give Enemy a methoddrawEnemy, just call itdraw: we already know it draws an enemy, adding an echo just adds an echo.