0

I coding a very simple program.

A pictureBox drops from above and another pictureBox (located at button) have to grab the pictureBox (collision between those two picturesBoxes) and then you get one point. My issue is when the collision happens then Point are counting up as long the collision between the two pictureBoxes happens.

How to stop this and only count ++ when a collision happens? - I'm total rookie coding c# so please don't answer a very advanced answer :)

namespace Animation
{
    public partial class Form1 : Form
    {
        Point p1 = new Point();
        Point p2 = new Point();
        int bredde;
        int højde;
        int score;
        int count = 0;

        public Form1()
        {
            InitializeComponent();
            p1.X = pictureBox1.Location.X;
            p1.Y = pictureBox1.Location.Y;
            p2.X = pictureBox2.Location.X;
            p2.Y = pictureBox2.Location.Y;
            højde = ClientSize.Height;
            bredde = ClientSize.Width;
        }

        Boolean max = true;

        void collision()
        {

            if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds)) 
            {
                score++;
                label1.Text = Convert.ToString(score);
                max = true;
            }


        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.W && p1.Y > 0)
                p1.Y -= 3;

            if (e.KeyData == Keys.D && p1.X + pictureBox1.Width < bredde)
                p1.X +=3;


            if (e.KeyData == Keys.A && p1.X > 0)
                p1.X -=3;


            if (e.KeyData == Keys.S && p1.Y + pictureBox1.Height < højde)
                p1.Y +=3;

            pictureBox1.Location = p1;


            if (e.KeyData == Keys.W && p2.Y > 0)


            if (e.KeyData == Keys.D && p2.X + pictureBox2.Width < bredde)


            if (e.KeyData == Keys.A && p2.X > 0)


            if (e.KeyData == Keys.S && p2.Y + pictureBox2.Height < højde)

            pictureBox2.Location = p2; 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            collision();
            p2.Y +=5;
            pictureBox2.Location = p2;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
    }
}
A R
  • 1,412
  • 2
  • 14
  • 29
Undertaker
  • 11
  • 2
  • Does kollesion = collision? Not sure if kollesion is something I don't know. – Felix Castor Nov 15 '17 at 21:13
  • Yes - I spelled the word wrong. I have corrected it. Sorry. – Undertaker Nov 15 '17 at 21:17
  • Just for clarification: You have several nested if statements in the end of your `Form1_KeyDown` method. Did you really mean `if { if { if } }` or should it be `if{} if{}`. I suggest you to use curly braces to improve readability and avoid unwanted behaviours. At the moment your `pictureBox2.Location = p2;` is unreachable. – Martin Backasch Nov 15 '17 at 21:21
  • You have enabled the timer, but you didnt start it. See: https://stackoverflow.com/questions/1142828/add-timer-to-a-windows-forms-application – Ross F Nov 15 '17 at 21:39
  • I'm not sure what you mean? I have "just" put all my if statements under one {code} instead using curly braces to each if statement. It is that you suggest? – Undertaker Nov 19 '17 at 20:36
  • @Undertaker Sorry for the late response. If you want to refer to a comment you can notify the author by adding `@Name`. From the help: _The post author will always be notified of your comment. To also notify a previous commenter, mention their user name: `@peter` or `@PeterSmith` will both work._ – Martin Backasch Nov 24 '17 at 19:30
  • @Undertaker: Now to your question: What I mean is within your `Form1_KeyDown` method, the last 4 `if` in your code are seen by the compiler as `if(condition1) { if(condition2) { if(condition3) { if(condition4){ /*do something*/ } } } }`. You will enter this cascade only when your `e.KeyData == Keys.W` and if it is true then the next condition is false and will return. So the line `pictureBox2.Location = p2;` is unreachable. – Martin Backasch Nov 24 '17 at 19:38

0 Answers0