-1

Background info

Since a few days im working on a multiplayer 2d rpg Based on Java/libgdx. Today i Tried to add some Animations to my Player, that worked for the Idle Animation. But and heres my questions how do i do this for more than once? I am using png spritesheets for the Different states like Idle, Walking etc. I Used states in my Player class like Idle/Walking right/left. In my Animation class There states too, the Same But i cant compare them because they Are enums.

**Main Question **

Is There an easy way to compare and set the enums states out of

class Player {

     public enum Animationstates{

     IDLE,RIGHT,LEFT

     }

   Animationstates Currentstate;

   public AnimationState GetStatefromplayer(){

   return Currentstate;

   }


 }

With

class Animatedsprite{

    public enum AnimationStates{

    IDLE,RIGHT,LEFT

    }

   AnimationState Currentstate = AnimationState.IDLE;

   public void setState  (AnimationState state){

  Currentstate = state;

   }



  }

I Tried a few Different things like :

Animatedsprite.setState(Player.getState());

But that didnt worked :/ any solutions?

genaray
  • 1,080
  • 1
  • 9
  • 30

2 Answers2

1

Actually, as far as I understand your question, you can simply create only one AnimationStates enum outside of your classes and then use it everywhere you need.

So, you'll end up with three classes like this (all in separate files):

class Player {

       AnimationState currentstate;

       public AnimationState GetStatefromplayer(){
              return currentstate;
       }

}


class Animatedsprite{

   AnimationState currentstate = AnimationState.IDLE;

   public void setState  (AnimationState state){
          currentstate = state;
   }
}

   public enum AnimationState{

        IDLE,RIGHT,LEFT;

    }
Enigo
  • 3,685
  • 5
  • 29
  • 54
1

If they have same name, in your case they do;

This is how you compare them:

player.CurrentState.toString().equals(animatedSprite.CurrentState.toString())

This is how you set one to another:

player.setState(Player.AnimationStates.valueOf(animatedSprite.CurrentState.toString()))

Here is a link that might help you understand better:

https://stackoverflow.com/a/604426/2205307

Community
  • 1
  • 1
ossobuko
  • 851
  • 8
  • 25