- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 C# 编码相当陌生,正在尝试自学更多。我一直在尝试制作一个简单的角色扮演游戏,其中包含角色扮演游戏中关卡的统计数据,但一直试图根据我的角色统计数据对敌人施加伤害。
当我认为我已经通过将玩家的统计脚本拆分为敌方单位的第二个统计脚本来解决问题时,不幸的是,我遇到了一个问题,即赋值的左侧需要是一个可变属性或索引器,无论我如何寻找解决方案,我都被难住了。有人可以看一下我的脚本并指出我犯的任何明显错误吗?
请并谢谢您!
public void TakePhysicalDamage()
{
defaultStats.GetPhysicalDamage()-= armor; //This is the offending line
physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
health -= (int)Math.Round(physicalDamage);
if(health <= 0)
{
health = 0;
Die();
}
}
void Die()
{
{
playerLevel.AddExperience(experience_reward);
}
Destroy(gameObject);
}
}
这里是playerstats(defaultstats)脚本仅供引用,我试图从中获取物理伤害
[SerializeField] float 强度 = 5f;[SerializeField] float 物理伤害 = 5f;
public float GetPhysicalDamage()
{
return physicalDamage += strength;
}
很抱歉,如果这看起来非常基本,但如果您感到无聊,请看一下!
最佳答案
您正在尝试修改函数:
defaultStats.GetPhysicalDamage()-= armor;
但你不能,因为 GetPhysicalDamage
只返回伤害,它没有设置为允许你修改它的属性(也不要这样做!)
public float GetPhysicalDamage()
{
return physicalDamage += strength;
}
相反,您应该使用一个变量physicalDamage
,例如:
public void TakePhysicalDamage()
{
physicalDamage = defaultStats.GetPhysicalDamage() - armor; //This is the offending line
physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
health -= (int)Math.Round(physicalDamage);
if(health <= 0)
{
health = 0;
Die();
}
}
实际上,经过仔细审查,我认为您可能没有做您认为自己正在做的事情。看起来 physicalDamage
应该是您正在造成的基础伤害,但是当您在 GetPhysicalDamage()
中有如下一行时:
return physicalDamage += strength;
如果 physicalDamage
为 5 并且 strength
为 5,那么第一次调用 GetPhysicalDamage()
时,您会得到 10。但是您会得到什么?正在做的是添加物理伤害的强度,并使用+=
运算符存储该值作为新的物理伤害,这样下次您调用 GetPhysicalDamage()
,physicalDamage
变量现在为 10(来自上一次调用),现在返回 15。然后是 20、25,等等。
我认为你想要的只是物理伤害和力量的总和,例如:
return physicalDamage + strength;
但如果是这种情况,那么我认为变量名称 physicalDamage
具有误导性。我个人更喜欢像 basePhysicalDamage
这样的东西,然后你可以拥有这样的属性:
public int PhysicalDamage => basePhysicalDamage + strength;
我特别建议这样做,因为稍后在您的代码中,您现在遇到了问题,您正在修改 physicalDamage
变量,如下所示:
physicalDamage = Mathf.Clamp(physicalDamage, 0, int.MaxValue);
这也很令人困惑,因为看起来您正在尝试 GetPhysicalDamage
并使用 armor
修改它,但是当您调用 GetPhysicalDamage
时armor
你从同一个(本地)来源获得它们,所以要么是玩家用玩家的盔甲对自己造成的物理伤害,要么是生物用他们的盔甲。
我会将损坏作为参数传递,以便您可以将损坏从一件事发送到另一件事,例如:
public void TakePhysicalDamage(int damage)
{
damage -= armor;
damage = Mathf.Clamp(damage, 0, int.MaxValue);
health -= (int)Math.Round(damage);
if(health <= 0)
{
health = 0;
Die();
}
}
关于c# - c# 初学者遇到 CS0131 : The left-hand side of an assignment must be a variable, 属性或索引器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73251679/
我试图理解 java.util.concurrent.locks 库,并希望实现两个通过列表运行的线程,而第二个线程不应超过(领先)第一个线程。具体来说,我想实现手动锁定。 我写了下面的代码,但不起作
我正在寻找 Adobe Reader 中的张开的手/闭合的手 鼠标光标。他们在哪里?如果它们是非标准的,我用 C# 创建它们的最简单方法是什么? 最佳答案 是的,它们是非标准的。您只需要四处搜索,
我有这些警告,但我不知道如何解决它们。 warning: left-hand operand of comma has no effect warning: right-hand operand of
我在网站上寻求帮助,但仍然找不到此问题的答案。 这是代码: import java.util.Scanner; public class Player { private Card[] hand; p
当我尝试这样做时出现错误 friend std::ostream& operator<<(std::ostream& os, const hand& obj) { return obj.sho
我之前没有学过javascript,完全不知道自己该怎么做,因为我只是跟着别人的项目,希望有人能告诉我如何修改这些错误的地方。谢谢T_T 最佳答案 改变 function aniSprite (col
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
题目地址:https://leetcode.com/problems/hand-of-straights/description/ 题目描述 Alice has a hand of cards,
我有一个包含几个变量的循环;其中一个在每一步递增。然而,有时,这个变量可能会被重置为 0。因此我可以写: (loop with z = 0 ... do (progn
在 this link ,期望的结果是当光标悬停在图例上时,鼠标不会变为手形。我怎样才能实现它? 我不确定,但是这样的东西有用吗? window.setTimeout("document.body.s
我有一个 GameActivity 和一个 GameOverActivity。我将游戏积分保存在 GameActivity 中,但会在 GameOverActivity 中显示这些积分。我必须做什么?
这个问题已经有答案了: Output of System.out.println(object) (4 个回答) 已关闭 9 年前。 我不确定为什么会得到这个输出。当我尝试为我的扑克程序打印一手牌时。
我使用 carousel bootstrap 3 来使用幻灯片文本。当第 1 行文本长到底部(例如:高度 300 像素)而第 2 行文本太短(例如:仅 10 像素)时,问题是当我们单击第 # 行旁边时
如何使用 JavaScript 在 td 元素中添加样式属性 cursor? 我已经试过了: var tabla = document.getElementById("table"); var fil
有没有办法从导入的模块中获取异常处理输出并从调用程序中处理它?例如,我有一个写入 HTTP 异常的导入模块 except urllib2.HTTPError, e: sys.stderr.wri
我在这里遵循 W3C 的标准 http://www.w3.org/TR/CSS2/ui.html在这里 http://www.quirksmode.org/css/cursor.html#note .
我正在使用 Unity 构建汽车模拟器游戏。对于输入,我使用 Logitheck 方向盘 G29。现在我需要使用 Hand Controller 来加速或刹车。这是我的手控器手控器 HC1 Link
我在 chrome 浏览器中运行我的 flutter 应用程序,它会显示以下错误。 Do not call didCreateEngineInitializer by hand. Start with
我想为我的 Lable 设置一个鼠标监听器,这样当用户将鼠标放在标签上时,我可以将光标更改为 HAND_CURSOR。 我试图设置样式 css“cursor: hand;”对于这个标签,但是在运行
我正在使用 idTCPServer 来处理数据。对于新设备,我需要将套接字移交给 dll(停止 tcp 服务器从该套接字读取数据)。 Indy 或 ICS 可以实现这一点吗? [编辑]出于测试目的,我
我是一名优秀的程序员,十分优秀!