I am having a problem with my code below. When the user selects a RadioButton (such as reload) and clicks the submit button, I want an ammo counter in the screen to increase by 1. On a RadioButton selection of "fire", I want the ammo counter to decrease by one.
The problem I'm having is the ammo counter seems to be one behind the user selection. So:
- ammo count = 1: user selects reload
- ammo count = 1: user selects reload
- ammo count = 2: user selects fire
- ammo count = 3: user selects reload
- ammo count = 2:
^^ see, the ammo count is consistently one behind the user selection. How can I fix this?
namespace ShotgunApp
{
public partial class SingleGame : PhoneApplicationPage
{
public static class AmmoCount
{
public static int userAmmo = startVars.startAmmo;
public static int geniusAmmo = startVars.startAmmo;
}
public SingleGame()
{
InitializeComponent();
GeniusAmmo.Text = "ammo: " + AmmoCount.geniusAmmo;
UserAmmo.Text = "ammo: " + AmmoCount.userAmmo;
}
private void submit_Click(object sender, RoutedEventArgs e)
{
Move();
}
public void Move()
{
if (uReload.IsChecked.HasValue && uReload.IsChecked.Value == true)
{
UserAmmo.Text = "ammo: " + AmmoCount.userAmmo++;
}
else if (uShield.IsChecked.HasValue && uShield.IsChecked.Value == true)
{
}
else if (uFire.I开发者_如何学运维sChecked.HasValue && uFire.IsChecked.Value == true)
{
UserAmmo.Text = "ammo: " + AmmoCount.userAmmo--;
}
else
{
}
}
}
}
AmmoCount.userAmmo++
and AmmoCount.userAmmo--
(postfix operators) are decrementing and incrementing the variable correctly but they return the previous unchanged values.
You should use the prefix operators: (++AmmoCount.userAmmo)
and (--AmmoCount.userAmmo)
.
精彩评论