Touching¶
How a drawing finds out that two shapes touch: whether they touch right now, or each time they start touching. Clicks and keys are on their own pages: Clicks and Keys.
if ... is touching¶
if ball is touching wall {
...
}
Checks whether two shapes touch right now, where they are at the moment this line runs. Put it inside a forever to keep checking while things move:
Canvas room having width 400, length 300
draw circle ball at 50, 150 having radius 20, fill "tomato"
draw rectangle wall at 300, 50 having width 20, length 200, fill "gray"
forever {
if ball is not touching wall {
move ball by 2, 0
}
wait 0.02 seconds
}
The ball rolls until it touches the wall, and then it stops. is not touching checks that two shapes don't touch.
ball is touching wall is true or false, like speed > 3, so it can go anywhere a check can: in an if, with and and or, or in a variable.
when ... touches¶
when ball touches coin {
...
}
Runs its lines each time the two shapes start touching. While they stay together it doesn't run again. Once they have come apart, it runs again the next time they meet.
Canvas field having width 400, length 300
draw circle ball at 30, 150 having radius 20, fill "tomato"
draw circle coin at 200, 150 having radius 12, fill "gold"
when ball touches coin {
hide coin
}
move ball to 370, 150 in 3 seconds
Rules:
- Shapes that already touch when the drawing starts count as starting to touch, so the lines run straight away.
- The lines run in time for the next picture, so you see what they change at the moment the shapes meet.
- The order of the names doesn't matter:
when coin touches ballis the same aswhen ball touches coin. - If the lines are still running (they have a
wait, say) when the shapes meet again, they run again alongside. - A
whencan't go inside anotherwhen, and it has nootherwise.
Which one do I need?¶
| I want to... | Write |
|---|---|
| do something once, each time two shapes meet (collect a coin, score a goal) | when ball touches coin { } |
| keep doing something for as long as they touch (stop at a wall, slow down in mud) | if ball is touching mud { } inside a forever |
| check once, right now | if ball is touching coin { } |
when never runs again while the shapes stay together. To do something the whole time they touch, use forever with if.
What counts as touching¶
Two shapes touch when they share at least one point: they overlap, or their edges meet.
| Borders | count: a thick border makes a shape reach further |
| Filled or not | doesn't matter: every shape is solid, so a dot inside an empty square touches it |
| Lines and arcs | the line as it is drawn, as thick as its thickness |
| Text | the box around its letters |
| Shapes with parts | all of their parts: face is touching ball when any part of the face touches the ball |
| One part | name it with a dot: face.nose is touching ball |
| Hidden shapes | touch nothing |
| Shapes on top of each other | touch, whichever is drawn first |
A shape always touches itself and its own parts, so when face touches face.nose runs once, at the start.
Canvas lake having width 400, length 300
shape duck {
circle body at 0, 0 having radius 30, fill "yellow"
circle beak at 35, -10 having radius 8, fill "orange"
}
draw duck d at 100, 150
draw circle bread at 260, 140 having radius 6, fill "tan"
when d.beak touches bread {
hide bread
}
move d by 130, 0 in 2 seconds
The bread goes when the duck's beak reaches it, before its body gets there.
Walls¶
There is no edge of the drawing to bump into. Draw walls where you want them:
Canvas box having width 400, length 300
draw rectangle left at 0, 0 having width 10, length 300, fill "gray"
draw rectangle right at 390, 0 having width 10, length 300, fill "gray"
draw circle ball at 200, 150 having radius 15, fill "tomato"
speed = 3
forever {
move ball by speed, 0
if ball is touching right {
speed = -3
}
if ball is touching left {
speed = 3
}
wait 0.02 seconds
}
Fast shapes and thin walls¶
Touching is checked where the shapes are each time the drawing changes. A shape that jumps a long way in one step can land on the other side of a thin wall without ever touching it. This ball jumps 40 at a time, and the wall is only 4 wide:
Canvas room having width 400, length 300
draw circle ball at 20, 150 having radius 10, fill "tomato"
draw rectangle wall at 200, 50 having width 4, length 200, fill "gray"
forever {
if ball is not touching wall {
move ball by 40, 0
}
wait 0.2 seconds
}
It never touches the wall, so it never stops. If that happens, either:
- move in smaller steps, more often:
move ball by 2, 0andwait 0.01 secondsinstead ofmove ball by 20, 0andwait 0.1 seconds, or - make the wall thicker than the biggest step.
In a repeat and in a recipe¶
A when inside a repeat watches the shape made that time round, so each coin has its own:
Canvas field having width 400, length 300
draw circle ball at 20, 150 having radius 15, fill "tomato"
repeat 5 times with n {
draw circle coin at 60 * n, 150 having radius 8, fill "gold"
when ball touches coin {
hide coin
}
}
move ball to 380, 150 in 4 seconds
Inside a recipe, is touching and when ... touches use the recipe's own parts, like everything else in it. Each shape drawn from the recipe watches its own parts.