I have some powerups for my game that I have stored in a list.
Each powerup lasts for a certain duration.
When a powerup is received, it is added to the list as below:
`bonusQueue.Add(BonusType.SLOWMO);`
`bonusQueue.Add(BonusType.BOUNCY);`
etc…
Likewise when the timer for each powerup is finished, I remove the powerup from the list like so:
`bonusQueue.Remove (BonusType.SLOWMO);`
At most there can be 5 powerups in the list and it changes dynamically accordingly.
The powerups all work as I want but I am having trouble with the icons I want to display for the powerups.
I want to display all powerups stacked according to the order they were received like the picture below:
[28093-icons.jpg*_|28093]
So as a powerup is added to the list, it should go to the start of the list.
As such the icons will work by icon 1 will display whatever is at list element 1, icon 2 will display list element 2, etc…
If there are no elements present at a certain index, it will display no icon.
I have some code and comments in psuedocode
public GameObject powerupIcon1;
public GameObject powerupIcon2;
public GameObject powerupIcon3;
public void ChangeIcons()
{
for(int i = 0; i < internalManager.bonusQueue.Count+1; i++)
{
if(internalManager.GetBonus(i) == InternalBonusManager.BonusType.NULL)
break;
Debug.Log (System.Enum.GetNames(typeof(InternalBonusManager.BonusType))*);*
//get the name of the powerup at position
//if the name at [1] == “SLOWMO”, powerupIcon1.Sprite.name == “SlowMo”;
//if the name at [1] == “BOUNCY”, powerupIcon1.Sprite.name == “Bouncy”;
//if the name at [2] == “SLOWMO”, powerupIcon2.Sprite.name == “SlowMo”;
//and so on until number 5
* }*
}
public BonusType GetBonus(int index) {
* if(bonusQueue.Count == 0) return BonusType.NULL;*
* if(index >= bonusQueue.Count) return BonusType.NULL;*
* return bonusQueue[index];*
My main problem I think is not fully understanding how to properly work a list and how to iterate through it to find a certain value.
I may be wrong in assuming that when a new element is added to the list, it goes to the front and that when an element is removed, the other all shift down in position.
Any help or ideas for how to achieve this greatly appreciated.
_*