Why Doesn't This Code Work As Intended? Visibility?


#1

Hey all,

I’m literally sitting in class write now writing this post after spending 10 minutes trying to figure out why this block of code isn’t working as intended for this student:

The student is basically trying to mimic the Exemplary Interactive Card where they shake the mouse and have one image disappear and another image appear. When we run the code, the rm sprite will appear, but the present sprite won’t disappear. I tried a bunch of things like checking his sprites, condensing the if-statements into just 1 if statement that does both actions, and I tried making a completely invisible sprite and using setAnimation rather than .visible - none of them are making this second sprite disappear. I also added a watch to present.visible and can see that it changes to false at the correct moment, but the present doesn’t disappear from the screen.

Am I missing something? Is this a bug? Any help is appreciated.

Thanks,
Dan Schneider


#2

I figured this out 2 minutes later. We weren’t overwriting the background every time, so the image “not disappearing” was really just the image “still remaining” from the previous frames. If that makes sense.

To fix it, we added a background(“white”) block at the top of the draw loop.


#3

Dan,

Glad to see you figured this out and how great a teaching moment this is for everyone that comes across this issue (and everyone does at some point). From your first post, it seemed like you did a great job problem solving and breaking everything down and really looking through the existing code, even using the .visible (and probably checking documentation in the meantime) - in the future, this might be a great “problem solving” problem for the class to engage in as it will probably happen to them.

Brad


#4

Similar issue here. I don’t know why this code is not working as intended. The section of code where the space bar is pressed should execute and stuff within the curly braces should execute. I tracked the variables and they are changing, but the image does not move:


#5

@efarrow You need to also set their visible properties to true inside that if-statement. Your draw loop sets them to false at the beginning, but you never set them to true later on.

Here’s my edit - I added three lines around line 67: https://studio.code.org/projects/gamelab/b1JzdpOqijtaVCrPoTn20TRcwko9-ZETA6HQ2d7dpDs/view

Hope that helps - Dan