c# - State Design pattern and infinite loops with MonoGame -


i trying use state design pattern game(monogame game).i want detect collision , give cell type , upon gives me specific action.but unfortunately implementation gives infinite loop "exception.stackoverflow" in player instances.

     abstract class playerstate      {          abstract public void doaction(player player );      } 

classes implements playerstate

    class deadstate:playerstate     {        public override void doaction(player player)             {                player.dead();                player.setstate(this);             }      }       class increasegold: playerstate      {     public override void doaction(player player)            {                player.increasegold();                player.setstate(this);            }      } 

player.cs

      public void setstate(playerstate state) { this.state = state; }       public playerstate getstate() { return state; } 

actioncontroller.cs

     player player = new player();      public void passcell(int celltype)     {         if (celltype == 1)         {             new deadstate().doaction(player);         }         if (celltype == 2)         {             new increasegold().doaction(player);         }     } 

finally in update method on game1.cs

    protected override void update(gametime gametime)     {        player.passcell(celltype);         base.update(gametime);     } 


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -