所以,我遇到了一个小问题。每当在战斗中,转弯每秒钟循环一次,无论如何,该生物总是在玩家甚至击中之前死亡。所以问题是,这个“if”语句应该如何构建以获取玩家正在战斗的生物的动态健康总数?
public void StartBattle()
{
messages.Add(String.Format("You engage in combat with {0}", monster.Name));
battleTimer.Start();
}
private void OnBattleUpdate(object sender, ElapsedEventArgs e)
{
if (monster.Health <= 0)
{
ActionResult result = monster.GetAttackResult();
messages.Add(result.LookMessage);
state.Character.CauseDamage(result.HealthChange);
if (state.Character.Health <= 0)
{
messages.Add("You have died.");
battleTimer.Stop();
}
int playerDamage = state.Character.GetDamage().HealthChange;
messages.Add(String.Format("You swing your weapon at {0} and cause {1} damage", monster.Name, playerDamage));
monster.CauseDamage(playerDamage);
messages.Add(String.Format("Health : {0}/{1}", state.Character.Health, state.Character.MaxHealth));
}
else
{
int playerDamage = state.Character.GetDamage().HealthChange;
messages.Add(String.Format("You swing your weapon at {0} and cause {1} damage", monster.Name, playerDamage));
messages.Add(String.Format("The {0} died.", monster.Name));
messages.Add(String.Format("You loot {0} gold from the {1}", monster.Gold, monster.Name));
messages.Add(String.Format("Health : {0}/{1}", state.Character.Health, state.Character.MaxHealth));
state.Character.Gold += monster.Gold;
mapManager.RemoveMonster(monster, mapManager.MapName);
battleTimer.Stop();
}
}
如果需要更多信息,请与我们联系!写我的第一个真正的节目!!
编辑: 其他一切都在战斗中起作用,但在我添加制造一个生物实际死亡的方法之前,战斗将一直持续到玩家死亡。
怪物在怪物制造者类中被定义为:
示例鼠:
public class MonsterMaker
{
public static Monster CreateRat()
{
Monster monster = new Monster();
monster.Name = "rat";
monster.Gold = 10;
monster.Health = 10;
monster.lowDamage = 2;
monster.highDamage = 4;
monster.AttackMessage1 = "{0} bites you for {1} damage.";
monster.AttackMessage2 = "{0} slashes at you with it's claws for {1} damage.";
monster.AttackMessage3 = "{0} rats you out for {1} damage.";
monster.MissMessage1 = "{0} bites at you and misses.";
monster.MissMessage2 = "{0} slashes at you and misses.";
return monster;
}
答案 0 :(得分:3)
这些行
if (monster.Health <= 0)
{
ActionResult result = monster.GetAttackResult();
似乎在说&#34;如果怪物的生命值为零或更少,那就让它进行攻击&#34;。你可能意味着&gt; 0
<强>更新强>
根据您在评论中链接的完整代码
我怀疑核心问题是计时器会在您调试时反复触发,从而导致混乱行为。我修改了处理程序的开头,以便在当前正在处理事件时返回, 使步骤更容易。在正常执行期间不需要这样做,因为定时器间隔比处理代码的时间长得多。
bool runningOnBattleUpdate = false;
public void OnBattleUpdate(object sender, ElapsedEventArgs e)
{
if (runningOnBattleUpdate) return;
try
{
runningOnBattleUpdate = true;
if (monster.IsAlive == false)
{
// Stuff
}
else
{
// Other stuff
}
}
finally
{
runningOnBattleUpdate = false;
}
}
此属性实现不正确。它抛出StackOverflowException,因为setter以递归方式调用自身。
public bool IsAlive
{
get { return _isAlive; }
set
{
if (Health <= 0)
{
_isAlive = false;
IsAlive = false;
}
else
{
_isAlive = true;
IsAlive = true;
}
}
}
您可以使用
public bool IsAlive
{
get { return Health > 0; }
}
您当前直接设置了IsAlive的状态。但是,这没有任何意义。相反,设置当前的健康状况,让IsAlive吸气剂根据健康状况计算出来。
并删除显式更改IsAlive和_isAlive的行,如
monster.IsAlive = true;
monster._isAlive = true;
你可能还希望防止角色在死亡后受到打击
if (state.Character.Health <= 0)
{
messages.Add("You have died.");
battleTimer.Stop();
return; // You don't want to let a dead character damage a monster
}
请注意,用户界面项目当前未设置为编译(在解决方案资源管理器中右键单击解决方案,选择Configuration Manager,单击构建)。
随着这些变化,我能够向南移动几次并击败一条蛇。