One of my students is trying to make one of their sprites go up down repeatedly on its own. He was able to do it with his second sprite going side to side. Is it possible for the sprite to go up and down? Here is his what he has so far…
Looks like he was on the right path, but the line of code where he checks to see if it is == to 120 could be the problem. Due to how code runs, it is always safer to check if it’s >= to 120 rather than specifically equal to 120, because it is possible that it checks the condition at 119 and then doesn’t check again until tiger.y is at 123 and then, it misses the code to turn around.
I changed that in his code and then it was working, but I also had to change a line of code in his tigerRun function (line 64, I believe) to check when it reaches the top. Those are obviously adjustable, but here’s my remix version if you want to have a look at it.
Best of luck,
This is one of the key problems with the code. It checks whether tiger.y is exactly 120 but doesn’t account for when tiger.y passes 120.
You can see here that tiger.y never actually hits 120 and skips from 119 to 123.
tiger.y >= 30 is also wrong and it should be
tiger.y <= 30
tiger.y == 120 should be changed to the opposite (greater than or equal to):
tiger.y >= 120
After these edits, you may find that the tiger is not actually moving, that’s because tiger.y is 31. Simply go to line 8, where it says:
var tiger = createSprite(46, 31) and change 31 to 30.
Here are the changes