-2
\$\begingroup\$

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?)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ May 18, 2013 at 4:12
  • \$\begingroup\$ This question would be better suited on SO, there's nothing game-specific here. \$\endgroup\$
    – Tetrad
    May 18, 2013 at 4:23

1 Answer 1

2
\$\begingroup\$

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).

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ May 18, 2013 at 4:22
  • \$\begingroup\$ There is no no sane reason to use a stack unless that is somehow a requirement of what ever it is you are doing. \$\endgroup\$ May 18, 2013 at 4:24

Not the answer you're looking for? Browse other questions tagged .