Confusion with False/True Statements
I’m trying to figure out a way to make a sprite jump that’s simpler than the one in the tutorial. I thought of using true/false statements, but I can’t tell if they’re not working or if I’m misusing them. Am I meant to use binary instead of true/false?
The code I’m trying to implement:
if keyboard.press.Y then
player_jump == true
if player_jump == true then
player_y += 30
end
end
As it is, there’s no way to stop the sprite from going through the ceiling but I want to take care of this true/false business first.
The problem is that you are setting player_jump using a double equals (==
), which is used for equalities/inequalities instead of a single equals (=
), which sets a variable. You also have your 'ends' in the wrong places, as they should end each if statement.
The corrected code should look like this:
if keyboard.press.Y then
player_jump = true
end
if player_jump == true then // note you can also remove the '== true' here
player_y += 30
end
Just so you know, microStudio does accept binary as true and false anyway.
Hopefully this helps :)
Well, that changed something, but now I only have a floating block that isn’t returning to earth. If you want to see for yourself: https://microstudio.io/Trondei/practicerocsfeather/ U2NFMNAD/
This is where the Time Machine and Watch window can help you a lot ; open the Watch window, start recording in the time machine then play your sequence of keyboard inputs ; then go back in time and see what happens with your variables at the point in time where the program doesn't do what you expect.
The reason for the 'floating block' is because there isn't any gravity! I usually add gravity by introducing a speed variable that is constantly subtracted by a gravity value. Like this for example:
update = function()
player_speedy -= 0.1 // gravity
player_y += player_speedy // use speed value
if keyboard.SPACE then
player_speedy = 6 // jump
end
end
If you look at the changes I've made on your project you should see some basic gravity and 'jumping' physics implemented. :)