Discuss Scratch

Tallented-Code-bot
Scratcher
100+ posts

How does the touching block work?

I have been wondering how the
<touching[sprite 1 v]?>
block works.

Could someone explain, or show me where the code for it is?

Thanks!
Sikecon
Scratcher
1000+ posts

How does the touching block work?

Basically it's a boolean. You can put it like this:
when green flag clicked
forever // put some kind of loop so it can detect whenever a sprite touches another sprite forever.
if <touching [sprite1 v] ?> then
broadcast [do something v]
...


else
...
end
end

Last edited by Sikecon (May 13, 2021 21:44:49)

Tallented-Code-bot
Scratcher
100+ posts

How does the touching block work?

Sikecon wrote:

Basically it's a boolean. You can put it like this:
when green flag clicked
forever // put some kind of loop so it can detect whenever a sprite touches another sprite forever.
if <touching [sprite1 v] ?> then
broadcast [do something v]
...


else
...
end
end
I know how to use it in scratch. What I am wondering is how I could implement it in another language, like js.
Smitop
Scratcher
100+ posts

How does the touching block work?

It attempts to look up the sprite specified, returning false is no such sprite (clones are ignored). If the target sprite is invisible, it returns false. Then for every XY point on the screen it checks if the sprite is touching that point and the target sprite are touching that point.

But how does a sprite know if it is touching an particular XY point? If the sprite is unscaled, it is touching a point when it's transparency at a point is non-zero. If the sprite has been scaled, then it is touching a point if any of the 4 pixels used in the linear interpolation scaling have non-zero transparency.

The logic for checking if the mouse pointer or edge is touching the sprite is completely seperate.
Smitop
Scratcher
100+ posts

How does the touching block work?

BTW, the was Scratch does it is super-inefficient. There are way faster ways to do this if you know that the sprites being checked are rectangles or circles or ellipses or really any shape that isn't a bunch of pixels. (and you can even cheat a little: if something if *almost* a rectangle, you can treat it is a rectangle for the purposes of overlap checking and almost nobody can tell the difference).
Tallented-Code-bot
Scratcher
100+ posts

How does the touching block work?

Thanks, I think I understand now. I am reporting this thread to be closed.

Last edited by Tallented-Code-bot (May 13, 2021 22:01:47)

gosoccerboy5
Scratcher
1000+ posts

How does the touching block work?

explanation-ish here

Powered by DjangoBB