我正在参加一场比赛,让每个战斗机对抗另外5名战士。为此,我有两个数组,playerArray和opponentArray,每个数组包含相同的六种字符类型。
在锦标赛中进行匹配时,我想阻止他们对抗自己的类型,例如,我不想让Rogue与Rogue对战。
这是用splice以某种方式完成的吗?
var playerArray:Array = ["Fighter", "Ranger", "Wizard", "Rogue", "Cleric", "Sorcerer"];
var opponentArray:Array = ["Fighter", "Ranger", "Wizard", "Rogue", "Cleric", "Sorcerer"];
charSel.addEventListener(MouseEvent.CLICK, matchups);
function matchups(event:MouseEvent){
for (a = 0; a < 6; a++){ // for each character
trace ("\n=== " + playerArray[a] + " fights ===\n");
for (b = 0; b < 5; b++){ // fight each opponent
trace (playerArray[a] + " vs " + opponentArray[b]);
}
}
}
全新的。谢谢你的帮助!
答案 0 :(得分:0)
使用字符串比较作为快速解决方案,
我已更新您的代码,
function matchups(event:MouseEvent) {
for (a = 0; a < 6; a++){ // for each character
trace ("\n=== " + playerArray[a] + " fights ===\n");
for (b = 0; b < 5; b++) { // fight each opponent
trace (playerArray[a] + " vs " + opponentArray[b]);
//This is added part (Comparing a String)
if(String(playerArray[a]) == String(opponentArray[b]))
{
trace("Opponents are of same type.");
}
}
}
}
答案 1 :(得分:0)
for (a = 0; a < playerArray.length; a++){ // for each character
trace ("\n=== " + playerArray[a] + " fights ===\n");
for (b = 0; b < opponentArray.length; b++){ // fight each opponent
if(playerArray[a] != opponentArray[b])
trace (playerArray[a] + " vs " + opponentArray[b]);
}
}
您可以避免硬编码部分。而是使用array.length
属性。
答案 2 :(得分:0)
如果playerArray和opponentArray值在迭代中相同,则跳过。
for (var a = 0; a < 6; a++)
{
trace ("\n=== " + playerArray[a] + " fights ===\n");
for (var b = 0; b < 6; b++) //Note here 5 to 6 better you length property of Array like opponentArray.length
{
if(playerArray[a] == opponentArray[b])
continue;
trace (playerArray[a] + " vs " + opponentArray[b]);
}
}