Game Programming‎ > ‎

Tiles

Tiles, Jigsaw Puzzles and other tile-type games with many, many objects
These are games where you move objects using drag-and-drop motions. They are a variation of the matching type games.

You will learn a few tricks to cut the amount of coding that you have to do. A good programmer tries to avoid duplicating code when possible. When you have multiple copies of code, when you have to make changes it is tedious (and error prone) to make the same changes in multiple places.

"Inheriting" code - write it once - for all objects, not many times on every object

In Java, you have multiple classes that inherit code from higher classes where possible. It is better to put code in the parent class instead of duplicating it in every child class. 

In LiveCode, we have the message path which is the order of places that LiveCode looks for a message handler.  First it looks in the object script. If it does not find the message handler there, then it looks at the card. If it does not find it there, then it looks in the Stack scripts. GFinally it looks in the LiveCode engine itself.

As a rule, you should put code as high in the message path as possible just as you put code in an upper class in Java. If you have 20 buttons, it does not make sense to put the same code in 20 buttons when you could put 1 copy in the card script.

In our case, we are talking about a simple mouseDown message
    e.g.
          on mouseDown

              grab me 

          end mouseDown


Instead of putting that on every button (20 copies), we can put just one copy on the card. One snag, how do we know which button to grab? 

Even though the handler is on the card, LiveCode knows which object was originally clicked and that is the "Target". But we just can not do the following code:

         on mouseDown

              grab the target

         end mouseDown


    because you get an error if you click on the card and try to "grab it". 

We have to make sure that we are grabbing only the buttons. So we have to check what the "Target" was. If you look at what the "Target" contains - it is the same as the "Short Name" or the object itself.  If you click on various objects and show the "target" you might see:

        button "box" or card "game"

Try it right now. Open up an old game or get a new card and put some objects on it. Then add the following to the card script:

    on the CARD SCRIPT:

         on mouseDown

              answer the target

         end mouseDown


  Now click on different objects and see what you get for the target

hint: button "box"
        field "label field"
    etc...


So our final change to the code for our tiles game is:

           on mouseDown

                if word 1 of the target is "button" then

                       grab the target

                end if

           end mouseDown


now...
Comments