Unit 6, Lesson 9


One of my students is trying to get the character to roll across the screen and when it hits a point get the screen to change to his winner castle. We have tried to set it up like the emoji game and it does not work. Any ideas on how to get the screen to switch?



From the code that I see, this is what I would expect to happen:

When the “Start Game” button is pressed, the screen changes to the game screen.

Moving the toggle switch will change the direction that the ball will move when the button is pressed. The ball should only move when the button is pressed.

If the ball is far enough to the right when the toggle switch is flipped, the screen will change to the end screen. The toggle switch has to be flipped to trigger this. This is because myFunction, which is checking the position and changing the screen, is only called within the toggleSwitch events.

Probably, you want to check the position of the ball every time the ball moves, which involves calling the function in the button press event instead.

I might be off, because I’m not sure the exact behavior you want form the game, so if that’s not what you’re looking for, please let me know.



@elizabeth_admin I have yet another one that is having trouble getting it to switch screens. Can you help as to why when the star hits the bottom that it does not go to the win screen? https://studio.code.org/projects/applab/UQbh4JwWwXgE3zscbqlUJ7gAFq44zPgq22f0IZdPCes



There is a function called end that checks to see whether the star is at the bottom of the screen and sets to the new screen if it is. That function is good, but it’s only called once, in line 8, when the program first starts to run. The star is not at the bottom of the screen then, so the screen does not change, and then the function is never called again.

Really, you want to check the star’s position every time that it moves down the screen, so you should call the end function inside the event that moves the star down.