Time for action – responding to events
To enable our buttons to do something, we need to add code to the UIButton_Clicked()
method by performing the following steps:
1. Modify the
UIButton_Clicked()
method in theTankBattlesGame
class to respond to button clicks as follows:void UIButton_Clicked(object sender, UIButtonArgs e) { int playerNumber = int.Parse(e.ID.Substring(1, 1)) - 1; string buttonName = e.ID.Substring(2); switch (buttonName) { case "Left": tanks[playerNumber].TurretRotation += 0.01f; break; case "Right": tanks[playerNumber].TurretRotation -= 0.01f; break; case "Up": tanks[playerNumber].GunElevation -= 0.01f; break; case "Down": tanks[playerNumber].GunElevation += 0.01f; break; case "Fire": break; } }
2. In the
User Interface
region of theTankBattlesGame
class, add a helper method for updating the contents...