The student says the program keeps freezing. One problem is the Velocity X. Can a student have two velocity X’s? Next, in the if/while statement he thinks this is where the program freezes, goes into debug and never comes out. I am not sure if something is wrong with his code or there is limitation in what he is trying to do or there is a bug in the program? Could someone take a look at his game and help?
thank you very much
@laurenlu.davis
It is a code issue. The if statement should be in the draw function and call the correct movement function. The functions for move_left or move_right would only have the code to do the movement. I would also have a function for stand.
if keyDown(left)
move_left
else if keyDown(right)
move_right
else
stand
Happy computing,
Andrea
@laurenlu.davis
Hi Lauren,
The program isn’t freezing, as far as I can tell. The reason that the left arrow isn’t working is that the program is calling move_right() after move_left(). When the left arrow is held down, the following things happen:
- move_left is called and checks for whether the left arrow is down. It is, so it sets the velocity to -10.
- move_right is called and checks for whether the right arrow is down. It isn’t, so it sets the velocity to zero.
Because the velocity is now zero, the sprite does not move.
The way to avoid this is to use “else if” rather than two if statements. You can access that my clicking the “+” at the bottom of an if/else block. It runs two conditions. You can look at the rexmix here. Notice that if the first condition is true, it runs the code for that condition and then doesn’t check for any of the other ones. It only runs the “else” if none of the conditions are true. Here’s a remix: https://studio.code.org/projects/gamelab/hlybShp5Bo7uQ1ngLfhMP14w2efBzrFtGcNYh0conKw
Elizabeth