Quantcast
Channel: Answers for "need explanation for a line of code."
Viewing all articles
Browse latest Browse all 4

Answer by CHPedersen

$
0
0
First of all, correct the code. It's wrong, and won't compile. It should be: public Vector2 speed = new Vector2(50, 50); Vector2 movement = new Vector2(speed.x * inputX,speed.y * inputY); Notice the capital "V", there. Second of all, while studying the code, try to simply say out loud (to yourself!) what the code does, line by line, dot by dot, comma by comma. This is known as rubber duck debugging, and often helps understanding because saying things out loud forces your mind to process what the code does. Here's an example, based on your code: "Okay, line 1: Declare a Vector2 called 'speed'. It's two-dimensional, and it's a field variable, since it has an access modifier, which is set to public. Initialize this variable, 'speed', to hold a Vector2 whose values are set to x = 50, and y = 50. Okay, so speed is a two-dimensional vector holding 50, 50. Cool." "Okay, line 2: Declare a new Vector2, called 'movement'. This one is a local variable, since it does not have an access modifier, and because it references 'speed' later on. Initialize 'movement' to a new Vector2 holding values for x and y which are *multiplications* of the x and y of 'speed', and of the inputX and inputY variables. Okay, so, inputX and inputY are probably read by input from the user, and their values will change based on what the user does. But the values of speed are constants - I know that, because I just set both of them to 50. Ah! Okay, so movement is just a vector that defines the direction of movement as per whatever the user does, and then the magnitude of the vector is scaled up using the speed numbers!". End of story. :)

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images