Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Sprite touching anything
- Ask_Pinkamena
-
51 posts
Sprite touching anything
I have searched for a solution on the forums, and correct me if I am wrong, there is no topic on a touching anything script. What I want to do is have one sprite perform a certain action upon contacting any sprite. I know I could use a broadcast on every sprite, but I am hoping there is a faster alternative as a single script. Any help is appreciated.
Edit: By anything, I mean anything (other sprites, the edges, anything BUT the background).
Edit: By anything, I mean anything (other sprites, the edges, anything BUT the background).
Last edited by Ask_Pinkamena (July 19, 2015 01:49:22)

Since clans are apparently a thing, I propose the Gemini Faction. Just to be special.
- gor-dee
-
1000+ posts
Sprite touching anything
you could use
this works faster because the distance to block works faster than the touching block. You need to put a value where I've put 50 that is approximately the smallest distance between the centres of the two sprites when they are nearly touching. This way your script will only use the laggy touching block if the two sprites are near enough to touch. Hope this makes sense!
<<<touching [sprite1] ?> or < <touching [sprite2] ?>>> or <<touching [edge] ?> >>but this might cause lag if there are a lot of sprites. An improvement on this would be
if <(distance to [sprite1]) < [50 ]> then
if <touching [sprite1] ?> then
broadcast [do something]
end
else
if <(distance to [sprite2]) < [50 ]> then
if <touching [sprite2] ?> then
broadcast [do something]
end
else
if <touching [edge] ?> then
broadcast [do something]
end
end
end
this works faster because the distance to block works faster than the touching block. You need to put a value where I've put 50 that is approximately the smallest distance between the centres of the two sprites when they are nearly touching. This way your script will only use the laggy touching block if the two sprites are near enough to touch. Hope this makes sense!
- Ask_Pinkamena
-
51 posts
Sprite touching anything
This could work. Thanks! you could use<<<touching [sprite1] ?> or < <touching [sprite2] ?>>> or <<touching [edge] ?> >>but this might cause lag if there are a lot of sprites. An improvement on this would beif <(distance to [sprite1]) < [50 ]> then
if <touching [sprite1] ?> then
broadcast [do something]
end
else
if <(distance to [sprite2]) < [50 ]> then
if <touching [sprite2] ?> then
broadcast [do something]
end
else
if <touching [edge] ?> then
broadcast [do something]
end
end
end
this works faster because the distance to block works faster than the touching block. You need to put a value where I've put 50 that is approximately the smallest distance between the centres of the two sprites when they are nearly touching. This way your script will only use the laggy touching block if the two sprites are near enough to touch. Hope this makes sense!

Since clans are apparently a thing, I propose the Gemini Faction. Just to be special.
- NeoProHex
-
12 posts
Sprite touching anything
I guess you could just have multiple scripts for each of the sprites. Or maybe a creative use of the More Blocks tab.
You probably haven't heard of me, but I have a few projects in the works you may be interested in!
If your a Five Nights at Freddy's fan that wants to play the game but doesn't have the money and all the scratch ones are too laggy and incomplete on your computer? Well, then I have the project for you. I have began to remake the first game and make it so it doesn't have all of the effects and unnecessary graphics that made the game laggy. It will eventually become a full project will all the gameplay of the original game, but I need your help! Check out the project here, it is in early stages of development. To be an alpha/beta tester, check it out here
Stay tuned for more projects by following me at NeoProHex
- Ask_Pinkamena
-
51 posts
Sprite touching anything
While I appreciate the response, I am looking for a single script answer. Your second answer could work for what I need. I guess you could just have multiple scripts for each of the sprites. Or maybe a creative use of the More Blocks tab.

Since clans are apparently a thing, I propose the Gemini Faction. Just to be special.
- DrEthan
-
6 posts
Sprite touching anything
i would love an answer to this because i am creating a dynamic inventory (which means that when you pick up an item it can go to any slot not just a set one). But my problem is that there is a chance that you might pick up two items at the same time and if you do that the dynamic inventory gets messed up with the items being on top of each other. The only way i could fix this is by having scripts that say if touching this or this or this which if i am trying to make a lot of items wouldn't be efficient so what i want is a script that can sense if i am touching anything else except this. (this is my game btw https://scratch.mit.edu/projects/95030060/#player)
- DrEthan
-
6 posts
Sprite touching anything
Ok i found a way to do this, I found out that if you type the exact name of all your sprites into a list and then tell the sprite to sense if it is touching anything in that list it will do something. An example i made would be this project https://scratch.mit.edu/projects/101852148/#editor
- DrEthan
-
6 posts
Sprite touching anything
i actually dont think anyone will view this forum because the user who started this is now gone ._. welp.
- LilPikachu08
-
69 posts
Sprite touching anything
when green flag clickedthis is when the ball touches red line fail hard
forever
move (10) steps
if on edge, bounce
if <touching color [#ff0209] ?> then
stop [all v]
end
if <touching [Paddle v] ?> then
turn ccw (100) degrees
end
end
- LilPikachu08
-
69 posts
Sprite touching anything
i actually dont think anyone will view this forum because the user who started this is now gone ._. welp.
you mean this
- DrEthan
-
6 posts
Sprite touching anything
what?i actually dont think anyone will view this forum because the user who started this is now gone ._. welp.you mean this
- Jemmerl
-
100+ posts
Sprite touching anything
Sorry if this is necroposting, but DrEthan- thank you. I was looking for something like that!
Always use message1. The gods of Scratch gave it to you for a reason.
- DrEthan
-
6 posts
Sprite touching anything
your welcome Sorry if this is necroposting, but DrEthan- thank you. I was looking for something like that!

- GamerFnfBoii
-
6 posts
Sprite touching anything
this should be added because it would be useful and save a LOT of time. the “if touching ()” block was updated to a select list to select multiple items.
and
<touching [ anything] ?>
and
<touching [ edge, mouse-pointer, sprite1] ?>
Last edited by GamerFnfBoii (March 14, 2023 21:21:26)
- coIIide
-
79 posts
Sprite touching anything
Please don't necropost. The last time this was posted on was 7 years ago. this should be added because it would be useful and save a LOT of time. the “if touching ()” block was updated to a select list to select multiple items.<touching [ anything] ?>
and<touching [ edge, mouse-pointer, sprite1] ?>
Create a new topic if you need help.
hello

i am not new to scratch this is a new account
- Discussion Forums
- » Help with Scripts
-
» Sprite touching anything