ecatalano
ecatalano

Reputation: 687

Two Variables Share The Same Value After Changing

I'm trying to build a bouncing ball program, and I am having trouble in changing the angle at which the ball moves.

I have declared my variables as follows:

    int xDelta;
    int yDelta;

    int speed;
    int savedSpeed;

    int iXSize; //size of width of window
    int iYSize; //size of height of window

I initialize my xDelta, yDelta, and speed within my Form1 function:

    public Form1()
    {
        //
        // Required for Windows Form Designer support
        //
        InitializeComponent();
        xDelta = 3;
        yDelta = 3;
        speed = 5;
        ...

In order to move the ball, I have

    private void timer1_Tick(object sender, System.EventArgs e)
    {
        //DrawBall();                       // Draw ball in next frame of animation


        if (stopped != 1)
        {
            if (xDelta != 0)
            {
                ballRect.X += speed * (xDelta / Math.Abs(xDelta));
            }
            if (yDelta != 0)
            {
                ballRect.Y += speed * (yDelta / Math.Abs(yDelta));
            }


            if (ballRect.X + radius >= ClientSize.Width || ballRect.X + radius <= 0)
                xDelta = -xDelta;

            if (ballRect.Y + radius >= ClientSize.Height || ballRect.Y + radius <= 0)
                yDelta = -yDelta;

            this.Invalidate();
        }
    }

To change the angle of the ball, I have two functions:

    private void button3_Click(object sender, EventArgs e)
    {
        //+Angle
        //Move clockwise
        Debug.WriteLine("Before: xDelta: {0} yDelta: {0}", xDelta, yDelta);
        xDelta++;
        yDelta--;
        Debug.WriteLine("After: xDelta: {0} yDelta: {0}", xDelta, yDelta);


    }

    private void button4_Click(object sender, EventArgs e)
    {
        //-Angle
        Debug.WriteLine("Before: xDelta: {0} yDelta: {0}", xDelta, yDelta);
        xDelta--;
        yDelta++;
        Debug.WriteLine("After: xDelta: {0} yDelta: {0}", xDelta, yDelta);
    }

For some reason, the output of my WriteLine statements always output xDelta and yDelta to have the same value. For example:

+Angle Button

Before: xDelta: -3 yDelta: -3
After: xDelta: -2 yDelta: -2
Before: xDelta: -2 yDelta: -2
After: xDelta: -1 yDelta: -1

-Angle Button

Before: xDelta: -1 yDelta: -1
After: xDelta: -2 yDelta: -2
Before: xDelta: 2 yDelta: 2
After: xDelta: 1 yDelta: 1

Why is this happening?

Upvotes: 0

Views: 69

Answers (2)

adjan
adjan

Reputation: 13684

You are using the {0} placeholder twice:

    Debug.WriteLine("Before: xDelta: {0} yDelta: {0}", xDelta, yDelta);

Use {1} instead

    Debug.WriteLine("Before: xDelta: {0} yDelta: {1}", xDelta, yDelta);

Alternatively, starting from C# 6.0, you can use string interpolation:

 Debug.WriteLine($"Before: xDelta: {xDelta} yDelta: {yDelta}");

Upvotes: 3

Kit
Kit

Reputation: 21769

As mentioned in the comments, you are repeating the argument in your WriteLine string. You can avoid these type of accidents by using interpolated strings:

Instead of

Debug.WriteLine("Before: xDelta: {0} yDelta: {0}", xDelta, yDelta);

do

Debug.WriteLine($"Before: xDelta: {xDelta} yDelta: {yDelta}");

Upvotes: 1

Related Questions