嘿大家我是C#的新手,我遇到了一些问题。我现在正在为学校做一份工作,我真的被这部分困住了。我盯着它,谷歌搜索几个小时,我有运气。我已经在下面列出了我到目前为止的内容。
方向不是那么好。
“为4种图像状态创建公共枚举CharacterState:攻击,防御,空闲和死亡。 现在创建一个成员变量状态来保存字符状态和一个带有get的公共属性State 并设定。现在,填写get的默认行为并设置为return / set state of state。“
任何帮助都会受到很大的限制!谢谢
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种图像状态创建公共枚举CharacterState:攻击,防御,空闲和死亡......
从您的代码中,您为每个州创建了一个枚举。枚举对于定义选项很有用,在你的情况下,你应该有一个定义你的角色状态的枚举。
viewForFooterInSection
...现在创建一个成员变量状态来保存字符状态,并使用get和set创建一个公共属性State。
if self.didTapOnButton == true {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.3, animations: {
bubbleView.backgroundColor = UIColor(red: 255/255, green: 225/255, blue: 68/255, alpha: 1)
containerViewTrailingConstraint.constant = 5
iconImageViewTrailingConstraint.constant = 5
bubbleViewEqualWidthConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: { (done:Bool) in
UIView.animate(withDuration: 0.2, animations: {
textView.alpha = 1
doneButton.alpha = 1
}, completion: { (done:Bool) in
textView.becomeFirstResponder()
})
})
self.didTapOnButton = false
}
}
然后,您可以通过State属性访问角色的当前状态。
public enum CharacterState {
Attacking,
Defending,
Idle,
Dead
}
希望这能澄清事情。
答案 1 :(得分: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;
答案 2 :(得分: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();
}));
}
}
}
}
答案 3 :(得分: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;
}
}