1
\$\begingroup\$

I need to make a life counter so that when my Actor get hit by an Enemy, his life will be decreased by 1.

I put this method in my Actor class:

private void decreaseLife() {
    Actor enemy = getOneIntersectingObject(Enemy.class);
    if (enemy != null) {
        int lastIndex = Scenario.getLifeBar().size()-1;
        Life LastLife = Scenario.getLifeBar().get(lastIndex);
        /** catch error on lastIndex --> "incompatible types" **/

        getWorld().removeObject(enemy);

        getWorld().removeObject(LastLife);
        Scenario.getLifeBar().remove(lastIndex);
    }
}

I used Scenario.getLifeBar().size() - 1; to get the last index from my ArrayList collection then decrease it by 1. My LifeMax was set to 3, so when decreased by 1 it should be returning value = 2, and be saved in lastIndex.

This is my code including LifeBar code in Scenario Class:

import greenfoot.*;
import java.util.ArrayList;

public class Scenario extends World {   
    private final int LIFE_MAX = 3; //Max value of my Actor's life
    private static ArrayList LifeBar = new ArrayList();

    public Scenario() {   
        super(600, 400, 1); 
        prepare();}

    private void prepare() {     
        prepareLifeBar(40, 30);
        // Add obj. main actor
        // Add obj.enemy
    }

    private void prepareLifeBar(int offsetX, int offsetY) {
        int spaceBetweenLife = 10;  // just to put space between image
        for (int i = 0; i < LIFE_MAX; i++) {
            Life life = new Life();
            life.setLocationX(offsetX+((life.getImage().getWidth()+spaceBetweenLife)*i) );
            nyawa.setLocatinY(offsetY);
            addObject(life, life.getLocationX(), life.getLocationY());
            LifeBar.add(life);
        }
    }

    public static ArrayList getLifeBar() {
        return LifeBar;
    }
}

In my Life class, I put some code to add attributes of LocationX and LocationY to save my Life actor in x and y axis.

I believe there's no other exception caught in any other class related to my life counter, yet my lastIndex caught an incompatible types exception.

What's causing this exception to be thrown?

\$\endgroup\$
  • \$\begingroup\$ Hey, welcome to GameDev.SE! I edited your question, but I decided to not add a Greenfoot tag as I don't think that framework is relevant to your issue (I'm happy to be proved wrong though!). \$\endgroup\$ – Alex May 1 '15 at 18:46
  • \$\begingroup\$ @Alex Yes, thanks for editing..! It looks more to the point right now. Actually I was looking for greenfoot-related questions in GameDev before, but found out that it's not yet become tag.. that's why I was suggesting :D \$\endgroup\$ – raisa_ May 1 '15 at 18:53
  • \$\begingroup\$ I see, it's possible that there simply are no questions specifically relating to Greenfoot. If you do have any of your own though, please go ahead and ask them! :) \$\endgroup\$ – Alex May 1 '15 at 18:57
0
\$\begingroup\$

Apparently my arraylist elements stored as Object objects (not as Life objects). So I need to cast the element as type Life first,

I need to change this :

Life LastLife = Scenario.getLifeBar().get(lastIndex);

into this :

Life LastLife =  (Life) SpaceWorld.getLifeBar().get(lastIndex);

and it works just fine.

Check the Reference here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.