Home‎ > ‎

Detecting Collisions (Intersect command)

Detecting Collisions - Intersect command

We need to check when the player runs into another object. For that we use the "intersect" command. Read about The_Intersect_Command and Objects_In_LiveCode 

Adding Another Button to the Card

Let's go back to our program "box" where we learned to move out red box. Now let's add another button and call it "Rock" See LC_MovingCodeWithIntersect.

                                                    
Now, if you move the box around, you see that it goes behind the "Rock" that we just created. In games, these objects would collide and something would happen. We are now going to learn how to check if these objects run into each other and then make something happen...


Add the intersect code to detect collisions of the player with another object ("intersect" command) and decide what to do. 
(On the 'Card Script', you should have the following code: LC_MovingCodeWithIntersect. You may want to change the intersect code to do something different - see below)

The Intersect Command

The intersect command is of the form:

        intersect (<object 1> , <object 2> )
   or 
        intersect (<object 1> , <object 2> , <threshold> )


where:
        object 1 and object 2 are of the form:  <type> <name>

            e.g.    button "my box"
                      button "enemy1"
                      graphic "rectangle"
                      image "clown"

        threshold is what level of transparency you detect
              it can be:
                        a number from 0 to 255 (0 is transparent and 255 is opaque)
                        "pixels"
                        "opaque pixels"
                        ...and a few other less commonly used terms

For example:

        intersect ( button "box" , image "asteroid" ) 
 
A more involved version of the command is with the "threshold" but it is not necessary. 

        intersect ( button "box" , image "asteroid", "pixels" ) 

            perhaps the following will help you:


                      Term        Alpha value               Description                                    
         "opague pixels"            255  opaque    =  no light passes through, solid objects
                                                                          e.g.  aluminum foil, solid objects
                                   ---------------------------------------------------------------------------------
                                           254
                                               translucent = lets some light through, various degrees of transparency,                             
                                            to                               blending
                                                                        e.g. colored glass, shower curtains, waxed paper
                                            2
                                 -----------------------------------------------------------------------------------
                  "pixels"               1    transparent = you can see details through it
                                                                         e.g. clear glass, plastic/Saran wrap

                               ===========================================
                  "bounds"                                       use the boundaries of the object
 
            if the intersect does not produce the result that you want, experiment and see what threshold works

note: sometimes when you import an image, the background is actually white pixels, not transparent ones. So the "intersect" command will detect the boundaries (square outline) before you actually touch the real object. In those cases, use the image tools (eraser) on the LiveCode Tools Palette to eraser those "white" pixels.

What do you do when the "Player" collides with another object?
  • You can send it back to the beginning to start over:
                if intersect( <object1> , <object2> ) then
                       set the loc of <object1>  to 50, 50
                end if

 
                (where you substitute the type and name of the objects that are involved)

            for example:

             if intersect( button "myBox" , image "spider" ) then
                 set the loc of button "myBox" to 50,50                      // this moves it instantly
             end if

            or the same code in another way...

             if intersect( button "myBox" , image "spider" ) then
                 move button "myBox" to 50,50                                // this moves it slower
             end if

                note: I just put 50,50 as an example.

                    To find the mocation for the start of your object:
                        click on the "Browse" tool and move your player (object1) to where you want it to start.
                        open up its PI (Property Inspector), click on "Size and Position', and see what its location is
                        use those numbers in the commands above. In the example below, the location is 197,160

                                 

  • You can send it back to the beginning to start over slowly with effects:
            This will make the background of the card (screen) yellow while it slowly (in 4 secs) moves the box back to the start. It will also show a message that says "Back to the Beginning You Go" while it is moving.
Then when it is done moving, it will set the background back to white and take away the message.

 if intersect (button "box" , image "maze" ) then
      set the backgroundcolor of the card "mycard" to "yellow"
      set visible of field "back_you_go" to "true"
      move the button "box" to 50,50 in 4 secs
      set visible of field "back_you_go" to "false"
   end if
   set the backgroundcolor of the card "mycard" to "white"
  • You can add objects (diamonds, stars, etc) to pick up for bonus points or objects (bombs, enemies, etc) to avoid. When your player hits one, make the object disappear, add stars for effect and either add points to the score, or anything else that your imagination can come up with
  •              if intersect( button "myBox" , image "maze" ) then
                     set the visible of graphic "diamond" to false
                     set the visible of graphic "stars" to true
                     put score + 10 into score
                     put the score into the field "ScoreCard"
                  end if  

  • You can add an object that is not visible. When your player touches that invisible object, you can pop up a dialog box that says the "You won" or automatically go to another card (the next level). 

                if intersect( button "myBox" , button "end" , "opaque pixels") then
                     Answer "You Won"
                end if    

                    or

                if intersect( button "myBox" , image "maze" , "opaque pixels") then
                     go to card "card2"
                end if    

  • You can do many more things to make your game interesting, fun, challenging or more difficult.  There is no limit to your imagination Have objects pop-up at random places that the player has to avoid. Have objects walking around or chasing the player. Be creative. Get ideas as you look at other pages on this website
NEXT - Skinning Buttons

Comments