Home‎ > ‎

Coloring Books

Making a Coloring Book  (the Code for this is in the file - "ColoringBook.LiveCode" below)
You can easily make a coloring book with buttons and graphics objects. We are going to have buttons with different colors on them. When you click on one of them, you will "pick up" their color and what ever you click on after that, will be colored the same color.

1. Create a new "Mainstack" with a first card.
2. Add buttons for colors to pick. Make each one a separate color and the following code on them:

    For each button, use this as it's code:

                    global x

                    on mouseUp

                           put the backColor of me into x

                    end mouseUp


When you click on a button, the above code saves the color in a variable that we called "x". Notice that we declaw "x" as a global so other objects on the card can also use it. Also notice that we don't name any color because it doesn't really matter. Whatever the background colors is of that object, it will be put into x. So you can change the colors and you do not have to change the code.


3. Add a button that has the name "More Colors" and put  the following code on it:


                   global x

                   on mouseUp

                           answer color "Pick a color to use"

                           put it into x

                   end mouseUp


The line that says "answer color Pick a color to use"   allows the player to choose their own color to use. Like the other buttons, it saves the color that they pick into the global variable called "x"



It should look like this right now:

4. Now add your objects to color. Use the graphics shapes on the "Tools "palette" as shown:


           On the "Tools Palette"                           


                        

    Use these graphics - square, circle, oval and polygons

                    

        It should now look like this:


                      


        Make Sure that each object has the "Opague" box checked

                       

    And add the following code to every object that you added:


                    global x

                    on mouseUp

                               set the backColor of me to x

                    end mouseUp


------------------------------------------------------- Advanced Code --------------------------------------------------------

     As you add more objects, you will have to add this code to every object and it is all the same code.

   But there is a shortcut!!!

    We could put the following code on the card instead (and not put any code on each object)

                    global x

                    on mouseUp

                           set the backColor of the target to x

                    end mouseUp


   When LiveCode has a message, like mouseUp, it looks on the object for the code for it. 

   If it does not find any code for the mouseUp message, it then looks on the card.

   If it does not find any code there, it then looks on the stack.

   In other words, it looks up the "message chain" for the code for the mouseUp message.

   

   Notice that we changed the code a little. We made it use the 'target' instead of 'me'

   When the code was on the object, it was 'me'. Now that the code is on the card, it

   does not want to use the color of 'me' which is the card. It wants to use that of the 'target'

   of the object that you clicked.


   Look up the "message path" in the lessons to find out more


------------------------------------------------------- Advanced Code --------------------------------------------------------


It should now look something like this:

                      




Now it should run. Click on a color, then click on an object and it will change to that color

       


ċ
ColoringBook.livecode
(9k)
cyril.pruszko@pgcps.org,
Mar 15, 2014, 6:05 PM
Comments