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
Post a Comment