我正在制作一款RPG,我有一个怪物遭遇功能,我在其中添加敌人攻击的重量和模式,我可以在调用函数时编辑它:
是可能的数组和变量都是参数在同一函数?如果没有,是否有其他方法可以达到同样的目标?
static void MonsterEncounter(float hp = 10, float speed = 3, float attack = 2, string monName = "Monster", float exp = 200F, float gold = 30, bool boss = false, bool doubleBattle = false, bool twoEnemy = false, float hpTwo = 10, float speedTwo = 10, float attackTwo = 1, string monNameTwo = "Monster 2", int teamMate = 0, string status = "Fine", string powerAttack = "Super Hit", float powerAttackHit = 10, float powerAttackHeal = 2, bool isHealing = false, bool isDefending = false, int waitTime = 0, string statusAfflict = "null", bool hasWeight = true, bool hasPattern = false, string statusTwo = "Fine",string powerAttackTwo = "Super Hit", float powerAttackHitTwo = 10, float powerAttackHealTwo = 2, bool isHealingTwo = false, bool isDefendingTwo = false, int waitTimeTwo = 0,string statusAfflictTwo = "null", bool hasWeightTwo = true, bool hasPatternTwo = false, int[] patternTwo = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weight = {0, 0, 0, 0}, int[] pattern = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weightTwo = {0, 0, 0, 0})
但这是相关的:
int[] patternTwo = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weight = {0, 0, 0, 0}, int[] pattern = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weightTwo = {0, 0, 0, 0}
但是我一直在这里得到相同的错误:
main.cs(4265,254): error CS1525: Unexpected symbol '{'
main.cs(4265,255+): error CS1737: Optional parameter cannot precede required parameters
main.cs(4265,254): error CS1525: Unexpected symbol '1'
我试过用不同的方式声明变量:
int[] patternTwo = new int[10] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, int[] weight = new int[4] {0, 0, 0, 0}, int[] pattern = new int[10] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, int[] weightTwo = new int[4] {0, 0, 0, 0}
但是它仍然产生错误:
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'patternTwo' must be a constant or default value
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'weight' must be a constant or default value
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'pattern' must be a constant or default value
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'weightTwo' must be a constant or default value
我用谷歌搜索,但我找到的页面要么是用不同的编程语言,要么没有涵盖我的问题。
我可以传递一个数组作为参数的方法与变量参数在Java?
c#方法与无限参数或方法与数组或列表?传递数组作为参数(c#编程指南)
在c#中传递数组作为参数
是可能的数组和变量都是参数在同一个函数?如果没有,是否有其他方法可以达到同样的目标?
这不是一个"解决方案,但更多的变通。您可以将默认数组值设置为null
,然后使用if语句设置所需的数组值
static void MonsterEncounter(float hp = 10, /*[...]*/ int[] patternTwo = null, int[] weight = null, int[] pattern = null, int[] weightTwo = null){
if (patternTwo == null) { pattern = new int [] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; }
//[...]
if (weightTwo == null) { weightTwo = new int[] { 0, 0, 0, 0 }; }
}
否则,你也可以使用重载。