8

In Scratch, there is a condition in the sensing category called touching, that can have edge as a parameter.

Given that the condition returns true, how can I tell which edge was touched (i.e. top, bottom, left or right edge of scene)?

Alternatively: How to tell the dimensions of a scene? I have worked out that position (x=0, y=0) is in the middle of the scene. Where are the four corner points located?

Charles Stewart
  • 11,661
  • 4
  • 46
  • 85
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • The Scratch wiki explains the coordinate system extensively [here](https://en.scratch-wiki.info/wiki/Coordinate_System) – Xbox One Aug 13 '20 at 19:53

3 Answers3

14

Make 4 sprites, all thin lines of any color you want. Two vertical, and two horizontal. Place them at the edges of your project. Add a start block followed by a set effect ghost to 100 block in each of these four lines. This hides the line when the project is played. If you name the lines top, bottom, left, right, it's easy to test which edge a different sprite touches, because you can have blocks run after one sprite touches another (the edges).

The whole project is 360 tall and 480 wide, with 0,0 being in the exact center. Always.

Community
  • 1
  • 1
Nick McCurdy
  • 17,658
  • 5
  • 50
  • 82
  • I think that @user2144827 's answer is probably the best way to find this, because making 4 new sprites just for the sake of detecting which edge of one sprite was touched is very inefficient. It makes things unorganized, and what if you had multiple sprites (or clones) that needed this? – stonefish Jun 11 '21 at 20:21
3

You could also do it like this:

If x > 239 then do something

(Change the value 239 (or less) to your purpose, and you could track the right edge) and x<-239 if you track left side.

Xbox One
  • 307
  • 2
  • 13
2

In category Sensing there is a touching x? block. x can be selected as edge.

tscpp
  • 1,298
  • 2
  • 12
  • 35