C# How to make Stack function like List? -


as part of requirements, i'm trying modify code can behave same way original code can't seem figure out.

i've done modification , compiles fine can't stack class function list:

list (original):

private list<gem> gems = new list<gem>(); private list<enemy> enemies = new list<enemy>();  private void updategems(gametime gametime) {     (int = 0; < gems.count; ++i)     {         gem gem = gems[i];         gem.update(gametime);          if (gem.boundingcircle.intersects(player.boundingrectangle))         {             gems.removeat(i--);             ongemcollected(gem, player);         }     } } 

stack (my modified version):

private stack<gem> gems = new stack<gem>(); private stack<enemy> enemies = new stack<enemy>();  /// <summary> /// animates each gem , checks allows player collect them. /// </summary> private void updategems(gametime gametime) {     gem[] array = gems.toarray();      (int = 0; < gems.count; ++i)     {         gem gem = array[i];         gem.update(gametime);         if (gem.boundingcircle.intersects(player.boundingrectangle))         {             gems.pop();             ongemcollected(gem, player);         }     } } 

the best can think of:

private stack<gem> gems = new stack<gem>(); private stack<enemy> enemies = new stack<enemy>();  /// <summary> /// animates each gem , checks allows player collect them. /// </summary> private void updategems(gametime gametime) {     var newgems = new stack<gem>(this.gems.count);      while (this.gems.count > 0)         {         var gem = this.gems.pop();         gem.update(gametime);          if (gem.boundingcircle.intersects(player.boundingrectangle))         {             ongemcollected(gem, player);         }         else         {             newgems.push(gem);         }     }      this.gems = newgems; } 

doesn't make sense though.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -