Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » ChatGPT speaks Scratch and helps with everything
- masoluka
-
Scratcher
26 posts
ChatGPT speaks Scratch and helps with everything
I highly recommend consulting with it if you need help, here is my latest example:
QUESTION:
My son is developing a game in Scratch and the problem is that when player plays in browser and accidentally clicks outside of playing stage, sprite Player gets wrong (blur?) keypresses and slams sprite into the wall, can't move, coz' keep getting imaginary keypresses. Here's his current movement blocks https://imagizer.imageshack.com/img922/7260/rnlyox.jpg How could I help him with that?
–
Based on the image you've shared, it looks like you're using the “if” block to check if the corresponding arrow key is pressed, and then setting the “Player v” x and y position accordingly. To reset the key presses when the game is unpoused, you could add an additional “if” block that checks if the game is unpoused and then runs a “set” block for each arrow key press variable, setting the value back to 0.
For example, you could add a new “if” block that checks if the “paused?” variable is equal to “no” and then, inside of that block, add "set to 0“, ”set to 0“, ”set to 0“ and ”set to 0“ blocks.
Alternatively, you can also add a ”when game starts“ block and set the key press variables to 0.
Please note that this is just one possible solution, and it may require some experimentation to find the best approach for your specific game. It might be also helpful to include a ”when key released“ block that sets the corresponding key press variable to 0 as well.
You may also want to consider using the ”broadcast“ block to broadcast a message when the game is paused or unpoused and add the script to handle key press variables inside the corresponding ”when I receive“ blocks.
Please let me know if you have any questions or if you need any further help.
QUESTION:
ok, but first the actual key presses, he's using block ”key _____ key pressed?“ say for a,w,s,d and arrows. How can he use another variable to actually monitor key presses? you said: ”you could add an additional “if” block that checks if the game is unpoused and then runs a “set” block for each arrow key press variable, setting the value back to 0.“. How do I do that?
–
You can use a variable to monitor key presses by setting the variable to a value when the corresponding key is pressed and then checking the value of the variable in your script.
Here's an example of how you could set up the key press variables:
Create four new variables (e.g. ”left arrow pressed“, ”right arrow pressed“, ”up arrow pressed“, ”down arrow pressed“)
When the game starts, set the values of these variables to 0.
Use the ”when key pressed“ block to check if the left arrow key is pressed and set the value of the ”left arrow pressed“ variable to 1.
Use the ”when key pressed“ block to check if the right arrow key is pressed and set the value of the ”right arrow pressed“ variable to 1.
Use the ”when key pressed“ block to check if the up arrow key is pressed and set the value of the ”up arrow pressed“ variable to 1.
Use the ”when key pressed“ block to check if the down arrow key is pressed and set the value of the ”down arrow pressed“ variable to 1.
Use the ”when key released“ block to check if the left arrow key is released and set the value of the ”left arrow pressed“ variable to 0.
Use the ”when key released“ block to check if the right arrow key is released and set the value of the ”right arrow pressed“ variable to 0.
Use the ”when key released“ block to check if the up arrow key is released and set the value of the ”up arrow pressed“ variable to 0.
Use the ”when key released“ block to check if the down arrow key is released
QUESTION:
Scratch doesn't offer ”when key released“ block, he can only use ”wait until not key pressed“, but the problem is that although key is not pressed, it acts like that it is due to lost focus problem.
–
You're correct that Scratch does not have a ”when key released“ block, but you can use the ”wait until“ block in combination with the ”key _____ pressed?“ block to achieve a similar effect.
Instead of waiting until a key is released, you can use the ”wait until not pressed“ block to wait until the key is no longer being pressed. For example, you could use ”wait until not key pressed“ and then set the value of the ”left arrow pressed“ variable to 0.
It seems like the problem you're facing is that when a user loses focus on the game, the keys remain pressed and the player continues to move even though the user is no longer pressing the keys.
One way to solve this is to add an additional variable to track whether the game has focus or not. Then you can use ”when I receive “ block and ”when I receive “ block to handle the situation.
When focus is lost, you can use ”set to 0“ block to reset the key press variable. When focus is regained, you can use ”set to 1“ block to set the focus variable to 1. And then in the movement script, you can add ”if = 1“ block to check if the game has focus before allowing the movement.
Please let me know if you have any further questions.
QUESTION:
OK, can you summarize all of the above, so that we could act upon it, based on the image I've shared. Thank you.
–
Based on the screenshot you shared, to solve the problem of key presses persisting after the game loses focus, you can take the following steps:
Create a new variable, for example ”Focus“
Set the value of the ”Focus“ variable to 1 when the game starts
Use ”when I receive “ block and ”when I receive “ block to handle the situation when the game loses and gains focus.
When focus is lost, use ”set to 0“, ”set to 0“, ”set to 0“, ”set to 0“ blocks to reset the key press variables.
When focus is regained, use ”set to 1“ block to set the focus variable to 1.
In the movement script, add ”if = 1“ block to check if the game has focus before allowing the movement, by connecting it to the ”if-else“ block that control the movements.
This way, when the game loses focus, the key press variables will be reset, and the player will stop moving. When the game regains focus, the player will be able to move again.
Please let me know if you have any further questions.
QUESTION:
OK, so the actual movement is based on variables for each key with states 0 or 1, and not the actual block ”key _____ key pressed?“, right?
–
Yes, that's correct. The movement script in the screenshot you shared is based on variables for each key with states 0 or 1, rather than the actual block ”key _____ key pressed?“. The idea is to set the variable to 1 when the corresponding key is pressed, and set it back to 0 when the key is released or the game loses focus. Then, in the movement script, you check the value of the variables to determine which keys are currently pressed, and update the position of the player sprite accordingly.
For example, when the ”left arrow pressed“ variable is 1, you decrease the x-coordinate of the player sprite to move it left, and when the ”right arrow pressed" variable is 1, you increase the x-coordinate of the player sprite to move it right.
This way, you can keep track of the key presses independently of whether the game has focus or not, and reset the key press variables as needed.
Please let me know if you have any further questions.
QUESTION:
Sounds like a good plan, we'll code it up and share results… IT WORKED!
–
I'm glad you found my explanation helpful. I'd be happy to help you with any further questions or issues that come up as you implement the changes. If you are facing any issue, feel free to reach out to me. Good luck with your game development!
QUESTION:
My son is developing a game in Scratch and the problem is that when player plays in browser and accidentally clicks outside of playing stage, sprite Player gets wrong (blur?) keypresses and slams sprite into the wall, can't move, coz' keep getting imaginary keypresses. Here's his current movement blocks https://imagizer.imageshack.com/img922/7260/rnlyox.jpg How could I help him with that?
–
Based on the image you've shared, it looks like you're using the “if” block to check if the corresponding arrow key is pressed, and then setting the “Player v” x and y position accordingly. To reset the key presses when the game is unpoused, you could add an additional “if” block that checks if the game is unpoused and then runs a “set” block for each arrow key press variable, setting the value back to 0.
For example, you could add a new “if” block that checks if the “paused?” variable is equal to “no” and then, inside of that block, add "set to 0“, ”set to 0“, ”set to 0“ and ”set to 0“ blocks.
Alternatively, you can also add a ”when game starts“ block and set the key press variables to 0.
Please note that this is just one possible solution, and it may require some experimentation to find the best approach for your specific game. It might be also helpful to include a ”when key released“ block that sets the corresponding key press variable to 0 as well.
You may also want to consider using the ”broadcast“ block to broadcast a message when the game is paused or unpoused and add the script to handle key press variables inside the corresponding ”when I receive“ blocks.
Please let me know if you have any questions or if you need any further help.
QUESTION:
ok, but first the actual key presses, he's using block ”key _____ key pressed?“ say for a,w,s,d and arrows. How can he use another variable to actually monitor key presses? you said: ”you could add an additional “if” block that checks if the game is unpoused and then runs a “set” block for each arrow key press variable, setting the value back to 0.“. How do I do that?
–
You can use a variable to monitor key presses by setting the variable to a value when the corresponding key is pressed and then checking the value of the variable in your script.
Here's an example of how you could set up the key press variables:
Create four new variables (e.g. ”left arrow pressed“, ”right arrow pressed“, ”up arrow pressed“, ”down arrow pressed“)
When the game starts, set the values of these variables to 0.
Use the ”when key pressed“ block to check if the left arrow key is pressed and set the value of the ”left arrow pressed“ variable to 1.
Use the ”when key pressed“ block to check if the right arrow key is pressed and set the value of the ”right arrow pressed“ variable to 1.
Use the ”when key pressed“ block to check if the up arrow key is pressed and set the value of the ”up arrow pressed“ variable to 1.
Use the ”when key pressed“ block to check if the down arrow key is pressed and set the value of the ”down arrow pressed“ variable to 1.
Use the ”when key released“ block to check if the left arrow key is released and set the value of the ”left arrow pressed“ variable to 0.
Use the ”when key released“ block to check if the right arrow key is released and set the value of the ”right arrow pressed“ variable to 0.
Use the ”when key released“ block to check if the up arrow key is released and set the value of the ”up arrow pressed“ variable to 0.
Use the ”when key released“ block to check if the down arrow key is released
QUESTION:
Scratch doesn't offer ”when key released“ block, he can only use ”wait until not key pressed“, but the problem is that although key is not pressed, it acts like that it is due to lost focus problem.
–
You're correct that Scratch does not have a ”when key released“ block, but you can use the ”wait until“ block in combination with the ”key _____ pressed?“ block to achieve a similar effect.
Instead of waiting until a key is released, you can use the ”wait until not pressed“ block to wait until the key is no longer being pressed. For example, you could use ”wait until not key pressed“ and then set the value of the ”left arrow pressed“ variable to 0.
It seems like the problem you're facing is that when a user loses focus on the game, the keys remain pressed and the player continues to move even though the user is no longer pressing the keys.
One way to solve this is to add an additional variable to track whether the game has focus or not. Then you can use ”when I receive “ block and ”when I receive “ block to handle the situation.
When focus is lost, you can use ”set to 0“ block to reset the key press variable. When focus is regained, you can use ”set to 1“ block to set the focus variable to 1. And then in the movement script, you can add ”if = 1“ block to check if the game has focus before allowing the movement.
Please let me know if you have any further questions.
QUESTION:
OK, can you summarize all of the above, so that we could act upon it, based on the image I've shared. Thank you.
–
Based on the screenshot you shared, to solve the problem of key presses persisting after the game loses focus, you can take the following steps:
Create a new variable, for example ”Focus“
Set the value of the ”Focus“ variable to 1 when the game starts
Use ”when I receive “ block and ”when I receive “ block to handle the situation when the game loses and gains focus.
When focus is lost, use ”set to 0“, ”set to 0“, ”set to 0“, ”set to 0“ blocks to reset the key press variables.
When focus is regained, use ”set to 1“ block to set the focus variable to 1.
In the movement script, add ”if = 1“ block to check if the game has focus before allowing the movement, by connecting it to the ”if-else“ block that control the movements.
This way, when the game loses focus, the key press variables will be reset, and the player will stop moving. When the game regains focus, the player will be able to move again.
Please let me know if you have any further questions.
QUESTION:
OK, so the actual movement is based on variables for each key with states 0 or 1, and not the actual block ”key _____ key pressed?“, right?
–
Yes, that's correct. The movement script in the screenshot you shared is based on variables for each key with states 0 or 1, rather than the actual block ”key _____ key pressed?“. The idea is to set the variable to 1 when the corresponding key is pressed, and set it back to 0 when the key is released or the game loses focus. Then, in the movement script, you check the value of the variables to determine which keys are currently pressed, and update the position of the player sprite accordingly.
For example, when the ”left arrow pressed“ variable is 1, you decrease the x-coordinate of the player sprite to move it left, and when the ”right arrow pressed" variable is 1, you increase the x-coordinate of the player sprite to move it right.
This way, you can keep track of the key presses independently of whether the game has focus or not, and reset the key press variables as needed.
Please let me know if you have any further questions.
QUESTION:
Sounds like a good plan, we'll code it up and share results… IT WORKED!
–
I'm glad you found my explanation helpful. I'd be happy to help you with any further questions or issues that come up as you implement the changes. If you are facing any issue, feel free to reach out to me. Good luck with your game development!
- deck26
-
Scratcher
1000+ posts
ChatGPT speaks Scratch and helps with everything
It's good that you want to help but the forum works by those needing help posting their questions and those helping respond where they have something useful to add. That has proved to be the best way for this forum to work and any attempts to do anything else just confuse things for those needing help. It also means people get help from different users which increases the chances of them getting a range of potential solutions.
- Discussion Forums
- » Help with Scripts
-
» ChatGPT speaks Scratch and helps with everything

