有关枚举方法的C#问题 [英] C# question regarding enum methods

查看:67
本文介绍了有关枚举方法的C#问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家,我现在正在为学校做作业,我真的被困在这一部分。我盯着它,谷歌搜索几个小时,我有运气。我已经在下面列出了我到目前为止的内容。



方向不是那么好。



为4种图像状态创建一个公共枚举CharacterState:攻击,防御,空闲和死亡。

现在创建一个成员变量使用get

和set来保持字符状态和公共属性State。现在,填写get的默认行为并设置为return / set state of state。





任何帮助都会受到极大的限制!谢谢







使用System;

使用System.Collections。 Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.Windows.Media;



命名空间WPFBattle

{

类CharacterImage:System.Windows。 Controls.Image

{





公共枚举攻击{







}



public enum捍卫{







}



公共枚举空闲{







}



public enum Dead {< br $>


}





public ImageSource IdleImageSource {get;组; } $ / $
public ImageSource AttackingImageSource {get;组; }

public ImageSource TakeDamageImageSource {get;组; }

public ImageSource DeadImageSource {get;组; }



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:

默认值:

this.Source = IdleImageSource;

休息;

}

}



protected override void OnRender(DrawingContext dc)

{

UpdateImageSource();

base.OnRender(dc);

}





public CharacterState State

{

get {return state; }

set

{

state = value;

this.Dispatcher.Invoke((Action)(( )=>

{

UpdateImageSource();

}));

}

}





}

}



我尝试了什么:



谷歌搜索各种各样的东西,我试图按照指示,但似乎没有任何工作

Hey everybody I'm currently doing an assignment for school and I'm really stuck on this part. I've staring at it and googling things for hours and I'm having any luck. I have included below what I have so far.

The directions aren't that good.

"Create a public enum CharacterState for the 4 image states: Attacking, Defending, Idle, and Dead.
Now create a member variable state to hold the character state and a public property State with a get
and set. For now, fill in the default behavior of get and set to return/set the value of state."


Any help would be greatly appeciated! Thank you



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

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();
}));
}
}


}
}

What I have tried:

Googling various things, and I tried to follow the directions but nothing seems to be working

推荐答案

你定义这样的枚举:



you define enums like this:

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





类变量应该是这样的:

编辑:改进了第二部分答案



the class variable should be like this:
improved second part of answer

class CharacterImage{
//member variable
private CharacterState characterState;

//property
public CharacterState State
{
get{return characterState;}
set(characterState=value;}
}
}


这篇关于有关枚举方法的C#问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆