I'm trying to make a game similar to Galaxian, but I have a trouble with my life counter, it reached the exception "Incompatible Types"
I need to make a life counter with max life is 3, life will countinously decrease by 1 everytime my actor hit an enemy.
And when no life left, game will over.
I put this method on my Actor's class. I'm planning to decrease LifeBar by 1 when my Actor hit an Enemy.
This is my method
This is what I wrote on Scenario Class, for the LifeBar
For the class Life, I just put some code to add attribute for LocationX and LocationY to save my Life position in X and Y coordinate
I'm still new to Greenfoot, any help would be very much appreciated :)
private void died() { Actor enemy = getOneIntersectingObject(Enemy.class); if (enemy != null) { int lastIndex = SpaceWorld.getLifeBar().size()-1; Life LastLife = SpaceWorld.getLifeBar().get(lastIndex); --> [b](catch error on "lastIndex", Incompatible Types)[/b] getWorld().removeObject(enemy); getWorld().removeObject(LastLife); SpaceWorld.getLifeBar().remove(lastIndex); } }
import greenfoot.*; import java.util.ArrayList; public class Scenario extends World { private final int LIFE_MAX = 3; private static ArrayList nyawaBar = new ArrayList(); public Scenario() { super(600, 400, 1); prepare(); } private void prepare() { prepareLifeBar(40, 30); // Add obj. main actor // Add obj. enemy // ... etc } private void prepareLifeBar(int offsetX, int offsetY) { int spaceBetweenLife = 10 ; //just for space between img for (int i = 0; i < LIFE_MAX; i++) { Life life = new Life(); life.setLokasiX( offsetX+((life.getImage().getWidth()+spaceBetweenLife)*i) ); life.setLokasiY(offsetY); addObject(life, life.getLokasiX(),life.getLokasiY()); LifeBar.add(life); } } public static ArrayList getLifeBar() { return LifeBar; } }