Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Combining "if" block with "when this sprite is clicked"
- Ridhwan1820
-
23 posts
Combining "if" block with "when this sprite is clicked"
Hello Scratchers. This is my second game I created.
I was left wondering why is there not a block that allows combining “if” with “sprite is clicked”. Is there a way to achieve this? - there is only “when sprite is clicked”
My plan is to have a script randomly appear. Once it appears, it will wait for either 0.5 seconds or if sprite is clicked, then it will disappear and appear randomly in time again.
Since I could not find “sprite is clicked”, i created this humongous chunk of code in my project,
https://scratch.mit.edu/projects/420120272
I was left wondering why is there not a block that allows combining “if” with “sprite is clicked”. Is there a way to achieve this? - there is only “when sprite is clicked”
My plan is to have a script randomly appear. Once it appears, it will wait for either 0.5 seconds or if sprite is clicked, then it will disappear and appear randomly in time again.
Since I could not find “sprite is clicked”, i created this humongous chunk of code in my project,
https://scratch.mit.edu/projects/420120272
- MopperFat1
-
100+ posts
Combining "if" block with "when this sprite is clicked"
This is what I usually do, but may not be the most efficient.
forever
if <<touching [mouse pointer v] ?> and <mouse down?>> then
code here
end
end
- Ridhwan1820
-
23 posts
Combining "if" block with "when this sprite is clicked"
This is what I usually do, but may not be the most efficient.forever
if <<touching [mouse pointer v] ?> and <mouse down?>> then
code here
end
end
Cool trick. What doesn “mouse down” mean? Click and hold?
And why it may not be efficient?
Last edited by Ridhwan1820 (Aug. 25, 2020 14:35:40)
- MopperFat1
-
100+ posts
Combining "if" block with "when this sprite is clicked"
mouse down is if the mouse click is held down. It isn't efficient because sometimes other scripts may run after this block, but it will slow them down.
- Scratch137
-
1000+ posts
Combining "if" block with "when this sprite is clicked"
The code that MopperFat1 suggested should work, but if you want the script to only run when the sprite is clicked and not held, you'll need to add a bit more code:
forever
if <<touching [mouse pointer v]> and <mouse down?>> then
wait until <not <mouse down?>>
...
end
end
Last edited by Scratch137 (Aug. 25, 2020 20:56:31)
- Vibrato
-
1000+ posts
Combining "if" block with "when this sprite is clicked"
<mouse down?>is true if the left click button is currently pressed/held down.
is false otherwise
- Ridhwan1820
-
23 posts
Combining "if" block with "when this sprite is clicked"
not held, you'll need to add a bit more code:The code that MopperFat1 suggested should work, but if you want the script to only run when the sprite is clicked andforever
if <<touching [mouse pointer v]> and <mouse down?>> then
wait until <not <mouse down?>>
...
end
end
Thanks a million for the advise Scratchers. I really like that Scratch has a platform where people can ask questions. Really help a beginner like me to learn.
- Ridhwan1820
-
23 posts
Combining "if" block with "when this sprite is clicked"
not held, you'll need to add a bit more code:The code that MopperFat1 suggested should work, but if you want the script to only run when the sprite is clicked andforever
if <<touching [mouse pointer v]> and <mouse down?>> then
wait until <not <mouse down?>>
...
end
end
i tried your suggestion. Although it greatly reduces complexity of my codes, i feel like it only works if one presses the mouse button and hold, releasing after 2 secs. Hmm. May I ask if this is how it should work?
New program file based on your recommendation:
https://scratch.mit.edu/projects/420369935
Last edited by Ridhwan1820 (Aug. 26, 2020 12:12:28)
- deck26
-
1000+ posts
Combining "if" block with "when this sprite is clicked"
There is only one built-in timer and you have multiple scripts resetting it. The sprites are then waiting 2 seconds during which you can't click them. You probably want one master control script managing the timer. The sprites should wait until two seconds have passed OR they have been clicked.
- StarWalker600
-
100+ posts
Combining "if" block with "when this sprite is clicked"
not held, you'll need to add a bit more code:another variant of this (if u only want it to check if the player clicked the mouse once) is: The code that MopperFat1 suggested should work, but if you want the script to only run when the sprite is clicked andforever
if <<touching [mouse pointer v]> and <mouse down?>> then
wait until <not <mouse down?>>
...
end
end
Any event :: events hatU can also use the when sprite clicked hat block. Start by making a variable “clicked?”
wait until <<touching [mouse pointer v] ?> and <mouse down?>>
if <<touching [mouse pointer v] ?> and <mouse down?>> then
...
end
when this sprite clicked
set [clicked? v] to [1]
wait (0.1) secs
set [clicked? v] to [0]
... // Then from somewhere else from the code when u want to check
if <(clicked?) = [1]> then
... //code
end
Also u might wanna add some delay & add the if statement to possibly some loop or something so that you wait until the user has clicked the sprite in question
Hope it helps!!!!
Last edited by StarWalker600 (Dec. 13, 2024 16:11:18)
- Discussion Forums
- » Help with Scripts
-
» Combining "if" block with "when this sprite is clicked"