I have a student who is working on the Lesson 14 Interactive Card. He created an awesome card that has a sprite plane that moves using the arrow keys and rain appears when the plane moves down only. He wants the rain to appear whenever the arrow keys are pressed.
I have attached a link to his code. Can anyone help me figure out what is going on.
You are right, he has an awesome project going … Just a few things for you to have him look at.
Short answer: It is working when the plane moves down because that is the last command in the draw loop.
Longer Answer: What is happening is that each of the blocks of code that he has programmed for directions are telling the rain to show up when pressed, but to hide when not pressed. So, the program is conflicting itself by quickly sending four different messages. Because this happens so fast, only the final direction is followed … For example, if left is pressed, the block of code for left tells the rain to show up, but the code blocks for right, up and down immediately tell it to hide again because they aren’t pressed, so the net result is that it is hidden. If down is pressed, since that’s the last command received, the rain shows up.
Thoughts on fixing it …
What if, instead of having 4 consecutive If / Else blocks, you used something called nested if blocks instead… It would look more like this (not actual code … just a representation)
IF left key pressed (move left, show rain)
ELSE
(IF right key pressed (move right, show rain)
ELSE
(IF up key pressed (move up, show rain)
ELSE
(IF down key pressed (move down, show rain)
ELSE (don't show rain).
Something like this would quickly check to see if any of the 4 direction keys were pressed and if not, the rain would hide or stay hidden.
To accomplish this, the 2nd IF block would be nested inside the ELSE of the 1st If block. The 3rd If block would be nested inside the Else of the 2nd IF block, etc.
Let us know if he still has questions or needs further information!