2016-11-25 64 views
1

嘿,大家我是C#的新手,我遇到了一些問題。我目前正在爲學校做一項任務,而我真的被困在這個部分。我一直在盯着它看,幾個小時都在Google上搜索,我很幸運。我已經包括了我迄今爲止的內容。有關枚舉方法的C#

方向不是很好。

「創建4個圖像狀態的公共枚舉CharacterState:攻擊,防禦,空閒,和死 現在創建一個成員變量狀態,以保持角色狀態,並用獲得 並設置公共屬性State爲。現在,填寫get和set的默認行爲來返回/設置狀態的值。「

任何幫助將大大appeciated!謝謝

namespace WPFBattle 
{ 
    class CharacterImage: System.Windows.Controls.Image 
    { 
     public enum Attacking{ 

     } 

     public enum Defending{ 

     } 

     public enum Idle{ 

     } 

     public enum Dead{ 

     } 
     public ImageSource IdleImageSource { get; set; } 
     public ImageSource AttackingImageSource { get; set; } 
     public ImageSource TakeDamageImageSource { get; set; } 
     public ImageSource DeadImageSource { get; set; } 

     protected void UpdateImageSource() 
     { 
      switch (State) 
      { 
       case CharacterState.Attacking: 
        this.Source = AttackingImageSource; 
        break; 
       case CharacterState.TakeDamage: 
        this.Source = TakeDamageImageSource; 
        break; 
       case CharacterState.Dead: 
        this.Source = DeadImageSource; 
        break; 
       case CharacterState.Idle: 
       default: 
        this.Source = IdleImageSource; 
        break; 
      } 
     } 

     protected override void OnRender(DrawingContext dc) 
     { 
      UpdateImageSource(); 
      base.OnRender(dc); 
     } 

     public CharacterState State 
     { 
      get { return state; } 
      set 
      { 
       state = value; 
       this.Dispatcher.Invoke((Action)(() => 
       { 
        UpdateImageSource(); 
       })); 
      } 
     } 
    } 
} 
+0

好了,你有4個空的枚舉,而不是隻是一個有4個值。 – Kinetic

+0

什麼是您的代碼中的CharacterState? – Kinetic

+0

而且我還不確定你的問題到底是什麼。 – Kinetic

回答

4

從您的枚舉開始。

創建4個圖像狀態的公共枚舉CharacterState:攻擊,防禦,空閒,和死了......

從你的代碼,你會爲每個國家一個枚舉。枚舉對於定義選項很有用,在你的情況下,你應該有一個枚舉來定義角色的狀態。

public enum CharacterState { 
    Attacking, 
    Defending, 
    Idle, 
    Dead 
} 

...現在創建一個成員變量狀態,用get和set保持字符狀態和公共財產的國家。

public class Character { 

    public Character() 
    { 
     State = CharacterState.Idle; 
    } 

    private CharacterState state; //-Member variable (private) 

    public CharacterState State 
    { get 
     { 
      return state; // note the casing in State and state 
     } 
     set 
     { 
      State = value; 
     } 
} 

然後你就可以通過其國家屬性來訪問你的角色的當前狀態。

var character = new Character(); 
CharacterState state = character.State; // returns Idle. 
// or set it 
character.State = CharacterState.Attacking; 
Console.WriteLine($"Take cover! character is {character.State}"); 
//- logs "Take cover! character is Attacking" 

希望能夠澄清一些事情。

+0

非常感謝您的回覆!我修復了枚舉部分,但是我仍然對「現在創建一個成員變量狀態來保存字符狀態和一個公共屬性狀態,並且設置了一個獲取 並設置」這個部分感到困惑。我不確定如何解決這個問題。 – firmfiasco

+0

我用一些評論更新了答案。請注意,'state'是成員變量,只能從屬性'State'更新,注意不同的外殼和'private/public'修飾符。 'State'只能在類中被訪問,而'State' *封裝它。這意味着你可以通過訪問公共屬性'State'來讀寫'state'。 –

+0

好的,謝謝! – firmfiasco

1

你的枚舉聲明是錯誤的。這是你想要的。

enum CharacterState 
{ 
    Attacking, 
    Defending, 
    Idle, 
    Dead 
} 

class CharacterImage 
{ 

    private CharacterState state; 
    public CharacterState State 
    { 
     get { return state; } 
     set 
     { 
      if (state == value) 
       return; 

      state = value;    
     } 
    } 
} 

你也可以寫你的財產..

public CharacterState State { get; set; } 

此編譯相同的代碼上面的財產,除非是價值比較部分。

if (state == value) 
    return; 
1

首先,聲明 - 如你所寫 - 說:創建一個公共枚舉4個圖像狀態。這僅僅是具有4個可能值的一個對象。創建,然後使用屬性/字段成員來管理內部值。也許,談到這樣的:

namespace WPFBattle 
{ 
    public enum CharacterState{ 
     Attacking = 1, 
     Defending = 2, 
     Idle = 3, 
     Dead 
    } 

    class CharacterImage: System.Windows.Controls.Image 
    { 

     public CharacterState _state; 

     public ImageSource IdleImageSource { get; set; } 
     public ImageSource AttackingImageSource { get; set; } 
     public ImageSource TakeDamageImageSource { get; set; } 
     public ImageSource DeadImageSource { get; set; } 

     protected void UpdateImageSource() 
     { 
      switch (_state) 
      { 
       case CharacterState.Attacking: 
        this.Source = AttackingImageSource; 
        break; 
       case CharacterState.TakeDamage: 
        this.Source = TakeDamageImageSource; 
        break; 
       case CharacterState.Dead: 
        this.Source = DeadImageSource; 
        break; 
       case CharacterState.Idle: 
       default: 
        this.Source = IdleImageSource; 
        break; 
      } 
     } 

     protected override void OnRender(DrawingContext dc) 
     { 
      UpdateImageSource(); 
      base.OnRender(dc); 
     } 

     public CharacterState State 
     { 
      get { return _state; } 
      set 
      { 
       _state = value; 
       this.Dispatcher.Invoke((Action)(() => 
       { 
        UpdateImageSource(); 
       })); 
      } 
     } 
    } 
} 
0
public enum CharacterState { 
    Attacking, 
    Defending, 
    Idle, 
    Dead 
} 

public class Character { 

    public Character() 
    { 
     State = CharacterState.Idle; 
    } 

    private CharacterState state; //-Member variable (private) 

    public CharacterState State 
    { get 
     { 
      return state; // note the casing in State and state 
     } 
     set 
     { 
      State = value; 
     } 
}