0

In my app (which is an Android game), I have a method which checks if the player is still alive, and if not, runs an animation and the game is over.

There are currently, 3 different animations available depending on how the player loses.

So, for example, they can fall off the screen, hit a bird, or get squash by something falling from the sky. Each one has a different animation.

Basically, what I'm doing is this:

When the player loses, I set the method by which it happened, so for example, if they were hit by a bird:

hero.setKilledBy(hero.HITBIRD);

Then I act of this by switching within my checkGameOver() method. However, I'm confused. I have to check it like so: (Note, in my cases, I have to put 0, 1, 2):

switch(hero.killedBy()){

    case 0: {                            
        fallOffScreenAnimation();break;
    }

    case 1: {
        hitBirdAnimation();break;
    }
    case 2: {
        squashedAnimation();
    }
}

within my hero object's class, I have the following methods

int killedBy;
final int FELLOFFSCREEN = 0;
final int HITBIRD = 1;
final int SQUASHED = 2;

int killedBy(){

    return killedBy;

}

int setKilledBy(int value){

    killedBy = value;

}

So, my question is, why can't I do something like this:

switch(hero.killedBy()){

    case hero.HITBIRD {
        fallOffScreenAnimation();break;
    }

 //............... and so on.......

Within the switch statement, my hero object, isn't recognised? Why is this? If I just type it anyway, (case hero.HITBIRD), I get this error:

case expressions must be constant expressions

Obviously, for readability, I would much prefer to use the variable names I've set rather than the raw numerical values......

Zippy
  • 3,826
  • 5
  • 43
  • 96
  • Check this one http://stackoverflow.com/questions/3827393/java-switch-statement-constant-expression-required-but-it-is-constant – Boris Ivanov Nov 02 '14 at 19:58
  • Thanks @BorisIvanov, I've had a quick read. I'm not sure I understand what is meant by 'use an initialiser'? Would this mean - declare it as (for example) int FELLOFFSCREEN and then set it somewhere - so, FELLOFFSCREEN = 0? If this is the case, I'm guessing I can't use the final keyword? - thanks – Zippy Nov 02 '14 at 20:09
  • 1
    Can you try first public final static int XXX = 1; or ENUM ? – Boris Ivanov Nov 02 '14 at 20:13
  • Thanks @BorisIvanov, I think I'm going to go with enum :-) – Zippy Nov 02 '14 at 20:18

2 Answers2

1

Make your int constants static:

final static int FELLOFFSCREEN = 0;
final static int HITBIRD = 1;
final static int SQUASHED = 2;

As class constants, the values available and known before an instance of Hero is created.


However, for these reasons I would make these an enum and either switch on that, or have methods on the enum of what action to take when the hero dies because of it thus avoiding the switch altogether.

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

You can only use constants in cases. Your variables are instance variables, not constants. Each hero has a copy of those variables. They should be declared as:

static final int FELLOFFSCREEN = 0;
static final int HITBIRD = 1;
static final int SQUASHED = 2;

And your should access them using the class name, not the object:

case Hero.HITBIRD {

(assuming the class name is Hero).

Note that you should probably replace these constants by an enum:

public enum HeroDeathCause {
    FELL_OFF_SCREEN, HIT_BIRD, SQUASHED
}

This is more type-safe, and more self-descriptive.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hi JBNizet - thanks for this, yes I use enums in other parts of my code, so I think this is the way to go.... – Zippy Nov 02 '14 at 20:17
  • Eeek, sorry, could you show how I would access my enum? I've set it up, but again, I just can access it via the switch statement? as soon as I type 'hero.' nothing happens, so would I have to declare something static here also or should I just change to an if-else? – Zippy Nov 02 '14 at 20:43
  • `switch (hero.killedBy()) { case FELL_OFF_SCREEN: {` Of course, `hero.killedBy()` must return an instance of `HeroDeathCause`. For example: `return HeroDeathCause.HIT_BIRD;` – JB Nizet Nov 02 '14 at 20:44