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?

Hi!

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

1 Like

@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

@hensleyn

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.

Elizabeth

1 Like

I am having basically the same issue where a students screen is not changing when he “clicks” the start button.

Bobby,

Welcome to the forum! Interestingly when I clicked on the Start button twice it went to the screen. I was able to remix the code and the issue on the first click is down at line 16 and 20 with the if (player1.y == 220) and if (player2.y == 220) - the error message suggest that those be in quotes:

ERROR: Line: 16: Oops, we can’t figure out what player1 is - perhaps you meant the string “player1” with quotes? If this is meant to be a variable, make sure you declared a variable: var player1

so if you change it to if ("player1".y == 220) and the same with player2 it will work right away.

Hope that helps!

Brad

It still does not appear to work.

I think the issue is the student trying to change the position of a UI element with code from the sprites in game lab.
You cannot get and set UI element properties with the dot operator like you did in game lab.
You now use functions, getters and setters.

So

player1.y = player1.y -2 
//now becomes
setProperty("player1", "x", ( getProperty("player1", "x")-2 ) );

Try that out.