This site requires JavaScript, please enable it in your browser!
Greenfoot back
pixie_laluna
pixie_laluna wrote ...

2015/5/1

Trouble with life counter

pixie_laluna pixie_laluna

2015/5/1

#
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
  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);
        }
    }
This is what I wrote on Scenario Class, for the LifeBar
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;
    }
}
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 :)
Super_Hippo Super_Hippo

2015/5/1

#
return LifeBar;
Where is the variable 'LifeBar' declared?
pixie_laluna pixie_laluna

2015/5/2

#
this, on Scenario class -->
private static ArrayList LifeBar = new ArrayList();
Under the LIVE_MAX declaration, I have typo on the code above. any suggestion ?
Super_Hippo Super_Hippo

2015/5/2

#
private static ArrayList<Life> LifeBar = new ArrayList<Life>();
Does this work? If not, this?
private static ArrayList<Life> LifeBar = new ArrayList();
Your array list stores objects, so you can't simply save it in a variable of type "Life", that's why there is this error.
danpost danpost

2015/5/2

#
The elements of your arraylist are stored as Object objects (not as Life objects). Therefore, before you can hold the element in a variable of type Life, you need to cast the element as type Life. Change line 5 in your 'died' method to:
Life LastLife =  (Life) SpaceWorld.getLifeBar().get(lastIndex);
BTW, the un-typoed declaration of the arraylist was okay to begin with.
pixie_laluna pixie_laluna

2015/5/8

#
Super_Hippo wrote...
private static ArrayList<Life> LifeBar = new ArrayList<Life>();
Does this work? If not, this?
private static ArrayList<Life> LifeBar = new ArrayList();
Your array list stores objects, so you can't simply save it in a variable of type "Life", that's why there is this error.
My scenario doesn't recognize 'Life' of course... But now I get it that the it's because I can't cast my array objects directly
pixie_laluna pixie_laluna

2015/5/8

#
danpost wrote...
The elements of your arraylist are stored as Object objects (not as Life objects). Therefore, before you can hold the element in a variable of type Life, you need to cast the element as type Life. Change line 5 in your 'died' method to:
Life LastLife =  (Life) SpaceWorld.getLifeBar().get(lastIndex);
BTW, the un-typoed declaration of the arraylist was okay to begin with.
Nah...!! It works... Thanks a lot Looks like I'm still confused about my array elements storing and how should I cast it thanks again ! :)
You need to login to post a reply.