Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to change List<> to Stack<> however I'm having a hard time figuring out.

A snippet of my code on where my issue occurs is:

    private Stack<Gem> gems = new Stack<Gem>();
    private Stack<Enemy> enemies = new Stack<Enemy>();

    private void UpdateGems(GameTime gameTime)
    {
        for (int i = 0; i < gems.Count; ++i)
        {
            Gem gem = gems[i];

            gem.Update(gameTime);

            if (gem.BoundingCircle.Intersects(Player.BoundingRectangle))
            {
                gems.RemoveAt(i--);
                OnGemCollected(gem, Player);
            }
        }
    }

The error messages that I'm recieving are:

  3 Cannot apply indexing with [] to an expression of type
   'System.Collections.Generic.Stack<Platformer.Gem>'

  4 'System.Collections.Generic.Stack<Platformer.Gem>' does not contain a definition for 
     'RemoveAt'and no extension method 'RemoveAt' accepting a first argument of type 
     'System.Collections.Generic.Stack<Platformer.Gem>' could be found (are you missing 
     a using directive or an assembly reference?)
share|improve this question
2  
The error messages are self-explanatory, and they wouldn't be errors if applied to List objects. Why do you want to use a Stack at all? –  Seth Battin May 18 '13 at 4:12
    
This question would be better suited on SO, there's nothing game-specific here. –  Tetrad May 18 '13 at 4:23
add comment

closed as off topic by msell, Tetrad May 18 '13 at 4:23

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

You are trying to use a stack like a list. A stack has no random access. By that, I mean you can only take 'pop' off the stack what you just 'pushed'. Think of a stack of plates. You can only access the plate that you just put on the top of the stack.

Also, stack data structure is almost certainly something you do not wish to use for your purpose (updating game objects).

share|improve this answer
    
Definitely. If I had to, I would prefer to just use a list. However, I would like to know I can implement it using a stack instead of a list. Any ideas how I can go about doing this? –  Mark Wilkonson May 18 '13 at 4:22
    
There is no no sane reason to use a stack unless that is somehow a requirement of what ever it is you are doing. –  Stephan van den Heuvel May 18 '13 at 4:24
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.