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......