Search code examples
c#countercountdown

Clicker counter increase/decrease issue


I've recently made this on my clicker counter, currently when the button BR is clicked, it will add 30 to BR and 10 to RB. Likewise when i click RB it will add 30 to RB and 10 to BR. the issue i'm finding at the moment is when i go to decrease the amount it isn't quite doing what i would like it to do. Currently the way it works is say BR is at 30 and RB is at 10, if i right click BR it will deduct 30 from BR and 10 from RB, And if it has 10 in BR and 30 in RB and i right click RB it will deduct 10 from BR and 30 from RB however if the count is 40 in BR and 40 in RB (both buttons clicked once) i can right click either button and it will decrease the first 30/10 but on the one that's then left with 30, it will carry on and keep decreasing it by 10 if its repeatedly right clicked.

Here's a GIF showing it count up fine (button being left clicked) https://gyazo.com/264cc772ac2ac4d1765c92aab34221c1

Here's a GIF to show the issue (button is being right clicked) https://gyazo.com/4a4484d1e78f8fa0e4e2c5c3af0a54a1

This is my int's used:

int BRcount = 0;
int RBcount =0;

This is the Code Used:

private void BR_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            BRcount += 30;
            RBcount += 10;
            BRT.Text = BRcount.ToString();
            RBT.Text = RBcount.ToString();
        }
        else if (e.Button == MouseButtons.Right)
        {
            if (BRcount >=30)
                BRcount -= 30;
            if(RBcount >=10 && BRcount >=30)
                RBcount -= 10;
                BRcount -= 30; //Was missing this, (copy and paste messed up, issue still present)
            BRT.Text = BRcount.ToString();
            RBT.Text = RBcount.ToString();
        }
    }
    private void RB_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            RBcount += 30;
            BRcount += 10;
            RBT.Text = RBcount.ToString();
            BRT.Text = BRcount.ToString();
        }
        else if (e.Button == MouseButtons.Right)
        {
            if (RBcount >=30 && BRcount >=10)
                RBcount -= 30;               
                BRcount -= 10;
            RBT.Text = RBcount.ToString();
            BRT.Text = BRcount.ToString();
        }

The way i'm trying to make it work is either button can be clicked to increase by 30 + 10 respectively and will decrease in the same manner.

(Please Do not hesitate to ask questions to further explain as i know that it looks like it can be a bit confusing)


Solution

  • It looks like you're missing some brackets in your code.

    You wrote,

    if (RBcount >= 10 && BRcount >= 30)
        RBcount -= 10;
        BRcount -= 30;
    

    Without brackets the if statement will execute only the following line, regardless of indenting. So, it will actually be doing,

    if (RBcount >= 10 && BRcount >= 30)
        RBcount -= 10;
    BRcount -= 30;
    

    I think instead you want,

    if (RBcount >= 10 && BRcount >= 30)
    {
        RBcount -= 10;
        BRcount -= 30;
    }
    

    The full code then becomes,

    private void BR_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        BRcount += 30;
        RBcount += 10;
        BRT.Text = BRcount.ToString();
        RBT.Text = RBcount.ToString();
      }
      else if (e.Button == MouseButtons.Right)
      {
        if (BRcount >= 30)
          BRcount -= 30;
        if (RBcount >= 10 && BRcount >= 30)
        {
          RBcount -= 10;
          BRcount -= 30;
        }
        BRT.Text = BRcount.ToString();
        RBT.Text = RBcount.ToString();
      }
    }
    
    private void RB_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        RBcount += 30;
        BRcount += 10;
        RBT.Text = RBcount.ToString();
        BRT.Text = BRcount.ToString();
      }
      else if (e.Button == MouseButtons.Right)
      {
        if (RBcount >= 30 && BRcount >= 10)
        {
          RBcount -= 30;
          BRcount -= 10;
        }
        RBT.Text = RBcount.ToString();
        BRT.Text = BRcount.ToString();
      }
    }