如何在player-LibGdx的状态枚举中添加子状态

时间:2017-10-06 05:44:21

标签: java libgdx

在我的游戏中,玩家有两个主要状态:STATIC和MOVING。

我想再添加两个状态作为STATIC状态的子状态,如UP和DOWN。

public enum PlayerState {

    STATIC,MOVING;

    public boolean isMoving() {
        return this == MOVING;
    }
    public boolean isStatic() {
        return this == STATIC;

    }

}

如何在枚举中添加此子状态并访问它?

现在我在播放器类中以

的形式访问它
private NestState state = NestState.STATIC;

 public void moving() {
    state = NestState.MOVING;

}

public void staticState() {
    state = NestState.STATIC;

}

public boolean isStatic() {

    return state.isStatic();
}

public boolean isMoving() {

    return state.isMoving();
}

1 个答案:

答案 0 :(得分:2)

我可以看到两个简单的解决方案,可以让你拥有状态和子状态。第一个是扩展你目前的状态。

public enum PlayerState {

    STATIC_STILL, STATIC_UP, STATIC_DOWN, MOVING_LEFT, MOVING_RIGHT;

    public boolean isMoving() {
         if(this == MOVING_LEFT || 
               this == MOVING_LEFT){
           return true;
         }
         return false;
    }
    public boolean isStatic() {
        if(this == STATIC_STILL || 
               this == STATIC_UP || 
               this = STATIC_DOWN){
           return true;
         }
         return false;
    }

}

第二种选择是创建与此

分开的第二组状态
public enum PlayerSubState {
    UP,DOWN,LEFT,RIGHT;
}

你的玩家将拥有PlayerState和PlayerSubState。