COLLISION DETECTION

Now we will do something more funny.

Whe will move the truck (like we did) and, when the truck collides against the ball, we will explode the ball (we will hide it and make a sound). To do this you need to learn some new tricks.

A designed object (obj1) has a method that returns true when it collides against other (obj2):

obj1 overlaps: obj2.

We will create a script, glued to the Truck, running in loop making this test. The script is:

(Flag1 getNumericValue = 0) & (self overlaps: Balloon) ifTrue:[
  self beep: 'splash'.
  Balloon hide.
  Flag1 setNumericValue: 1 ]

You are learning another thing here : how to play a sound using the method of any designed object: beep: 'some sound' . To the sound to play only one time (and the Balloon be hided also only once) we need to create a "flag" that changes when the collision occurs. This "flag" is an object Text we put at the world and change its value to : ZERO. It can be hidded.

To do this script to run in loop we click in the area after the clock and choose: "ticking".

The script of the button is the same we have seen in the previous lesson.

This demo has sound!!!! Play with it (stay clicking the button) and after: try to do something like it, using "the collision detection thing". Use your imagination.




PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE