【发布时间】:2015-06-04 00:13:22
【问题描述】:
更新:我应该更清楚。我正在尝试按升序对列表进行排序,然后获得比当前选择的武器具有更大价值的第一个武器。
我的 NextWeapon() 函数此时没有做任何事情(它只是在那里显示我尝试过的东西),但过去我只是迭代到列表中的下一个项目,但是一个项目可能不在。
枚举 F3DFXType 是我的角色可能拾取的武器,但如果它们实际上不在库存中,则循环遍历这些武器是没有意义的。因此,我尝试创建一个 int 类型的列表,然后循环遍历它。当角色拿起新武器时,它将作为 int 添加到列表中,然后当我切换到该 int 时,我会在 F3DFXType 枚举中检查相同的 int。
例如,在皮卡中,我会检查碰撞事件,然后使用: 武器列表.Add(5);认为它会将整数 5 添加到我的库存中。 F3DFXType 中的第 5 项是“Seeker”。当我尝试循环浏览我的库存时,它实际上不会添加第 5 个 F3DFXType,它只会在 F3DFXType 中添加 NEXT 项目。 (例如:我已经有 Vulcan,这将简单地添加 SoloGun,这是枚举中的第二项)
我正在尝试遍历列表中的项目。
我遇到的问题是,如果 Ito 迭代到列表中的下一个项目,该项目可能实际上并不存在。那么如何前进到列表中存在的下一个项目?
我不一定要使用下面的代码寻找答案,我只是想提供一些上下文,以便您可以看到我当前的方法。
// Possible weapon types
public enum F3DFXType
{
Vulcan = 1,
SoloGun,
Sniper,
ShotGun,
Seeker,
RailGun,
PlasmaGun,
PlasmaBeam,
PlasmaBeamHeavy,
LightningGun,
FlameRed,
LaserImpulse
}
// List of weapons currently avaialable
public List<int> weaponList;
public int currentIndex = 1;
void NextWeapon()
{
// Keep within bounds of list
if (currentIndex < weaponList.Count)
{
currentIndex++;
// Check if a higher value exists
var higherVal = weaponList.Any(item => item < currentIndex);
// create a new list to store current weapons in inventory
var newWeapList = new List<int>();
foreach (var weap in weaponList)
{
newWeapList.Add(weap);
}
weaponList = newWeapList;
// If a higher value exists....
if (higherVal)
{
// currentIndex = SOMEHIGHERVALUE
}
}
}
void PrevWeapon()
{
if (currentIndex > 1)
{
currentIndex--;
}
}
// Fire turret weapon
public void Fire()
{
switch (currentIndex)
{
case 1:
// Fire vulcan at specified rate until canceled
timerID = F3DTime.time.AddTimer(0.05f, Vulcan);
Vulcan();
break;
case 2:
timerID = F3DTime.time.AddTimer(0.2f, SoloGun);
SoloGun();
break;
case 3:
timerID = F3DTime.time.AddTimer(0.3f, Sniper);
Sniper();
break;
case 4:
ShotGun();
break;
case 5:
timerID = F3DTime.time.AddTimer(0.2f, Seeker);
Seeker();
break
default:
break;
}
}
【问题讨论】:
-
我对你在这里尝试做的事情感到困惑。在
weaponList.Any(item => item < currentIndex);为什么要与索引比较项目值? -
不太明白你想要完成什么。能不能详细解释一下?