【发布时间】:2016-03-15 07:24:36
【问题描述】:
我正在尝试更新“玩家的健康状况”。当“玩家”攻击目标时。玩家的生命值会根据目标的回击伤害而降低。
我的游戏类(标签所在的位置):
public partial class Game : Form
{
public static Wizard wizard;
public static Assasin assasin;
public Game()
{
InitializeComponent();
}
private void Battle_Click(object sender, EventArgs e)
{
Battle battle = new Battle();
battle.Show();
}
public void Game_Load(object sender, EventArgs e)
{
NameLabel.Text = HeroMaker.className;
if (HeroMaker.wizardChoosen)
{
wizard = new Wizard(HeroMaker.className);
HealthLabel.Text = wizard.Health.ToString();
DamageLabel.Text = wizard.AttackDamage.ToString();
HealthBar.Maximum = wizard.Health;
HealthBar.Value = wizard.Health;
}
}
}
我的战斗课(攻击发生时):
public partial class Battle : Form
{
Creature troll = CreaturesFactory.CreateCreature(CreatureType.Troll);
public Battle()
{
InitializeComponent();
}
private void AttackTroll_Click(object sender, EventArgs e)
{
Game.wizard.Health -= troll.ReturnDamage;
//TODO: Update the "HealthLabel value."
}
}
问题是当我攻击巨魔时,玩家的生命值正在下降,但标签上没有更新。提前致谢。
【问题讨论】:
-
您必须编写代码,在每次玩家健康状况发生变化时更新标签。您可以在
Wizard类中创建HealthChanged事件(或在Wizard和Assassin具有共同点的任何基类中)并在Health属性设置器中引发它,然后在@ 中注册一个事件处理程序987654329@ 更新标签的表单代码。