2016-11-25 59 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; 
     } 
}