Page 135 - Learn To Program With Scratch
P. 135

The sprite switches its costume and then calls the Recalculate proce-
                          dure to update the volume and surface area calculations. Note that these
                          scripts use the value of the current costume to determine whether the
                          sphere has reached its highest or lowest size, thus ensuring valid responses
                          to the Up and Down buttons. I’ll say more about the if block in the next
                          chapter, but for now, let’s discuss the sphere’s Recalculate procedure,
                          shown in Figure 5-29.

















                          Figure 5-29: Recalculate procedure

                             First, the value of the diameter variable is set according to this formula:

                                         diameter = 1 + 0.25 × (costume number – 1)
                             Since the costume number ranges from 1 to 9, the corresponding val-
                          ues of the diameter variable will be 1, 1.25, 1.50, ... , 2.75, 3, which is what we
                          intended.
                             The script finds the radius, r, by dividing the diameter by 2. It then cal-
                          culates the volume and the surface area of the sphere using the formulas
                          shown in Figure 5-26. The computed values will show up automatically on
                          the corresponding monitors on the Stage.



                                                    try it out 5-5
                            Open the application and run it . Study the scripts to understand how the applica-
                            tion works . Add a script to the Sphere sprite so it rotates and changes color as the
                            application runs . As another exercise, modify the original program to use a single
                            costume for the Sphere sprite and use the change size by block to change the
                            sphere’s size . The scaled image won’t look as nice, but otherwise, the application
                            should perform identically .












                                                                                   Variables   113

                                           www.it-ebooks.info
   130   131   132   133   134   135   136   137   138   139   140