0
\$\begingroup\$

I was recently trying to make a boucing ball off the environment walls. I apparently had everything correct, except for one thing, that I corrected stupidly, which made my code work, but I don't understand why.

So I had set the value of a 2d vector's X and Y inside protected override void Update(GameTime gameTime); this vector was what I would add to the object's X and Y so it could move according to whatever logic.

But it didn't work, the fix to this problem was to apparently declare the vector's values inside protected override void LoadContent().

So can anyone explain to me, what is the difference between declaring your variables/constants inside these two methods? And why?

EDIT : Wrong Sourcecode :

  protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // Loaded content
}
 protected override void Update(GameTime gameTime)
    {

        // Allows the game to exit
        if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
            this.Exit();

         //Code logic

            movement.X = 5;
            movement.Y = 5;
            birdbox3.X += (int)movement.X; //this variable was declared outside of any local field as a rectangle

            if (birdbox3.X <= 0)
            {

                movement.X = -movement.X;
            }
            else if (birdbox3.Y <=0)
            { movement.Y = - movement.X;
            }
            base.Update(gameTime);
        }

So when I would run this code, the birdbox3 would just stop when the if statement is fulfilled. The correct version would be to cut the

            movement.X = 5;
            movement.Y = 5;

and paste it into LoadContent() instead. The problem is I do not know why.

\$\endgroup\$
4
  • \$\begingroup\$ It is hard to understand what exactly your problem might be. It might be that you don't know yet about the difference between local, member and global variables. But it could also be something else. Please post the wrong and correct sourcecode. \$\endgroup\$
    – Philipp
    Commented Mar 2, 2015 at 8:21
  • \$\begingroup\$ I have edited my original post, hopefully it will help you help me, sorry for discarding the code since I thought it was something common for newbies to miss out. \$\endgroup\$
    – Zee
    Commented Mar 2, 2015 at 8:34
  • \$\begingroup\$ Assuming "LoadContent" gets called once at startup and "Update" gets called every frame, your issue is probably because the ball's X and Y coordinate got set to 5 every frame in the update method therefore it wouldn't be moving around freely... If it gets called once in the LoadContent method then it does not continually update the X and Y position to be at position X:5 and Y:5. \$\endgroup\$
    – Savlon
    Commented Mar 2, 2015 at 9:21
  • 1
    \$\begingroup\$ @Savlon Riight! That is indeed very logical. So the thing here is that it would be reset to X = Y = 5 in every frame, making it go back no matter what the if statement would do, thanks a lot! Why don't you submit your comment as an answer so I can give you credit for it? :) \$\endgroup\$
    – Zee
    Commented Mar 2, 2015 at 10:25

1 Answer 1

0
\$\begingroup\$

Assuming "LoadContent" gets called once at startup and "Update" gets called every frame, your issue is probably because the ball's X and Y coordinate got set to 5 every frame in the update method, therefore, it wouldn't be moving around freely...

If it gets called once in the LoadContent method then it does not continually update the X and Y position to be at position X:5 and Y:5.

Setting the position of x and y in the once called "LoadContent" method allows for the movement logic to operate. Setting the position in "Update" would override your movement logic.

:)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .