Time for action connect the dots

We need to add a new sprite and a new broadcast message to coordinate the dots with the line:

  1. Duplicate the double sprite and name it grapher-double. To duplicate the double sprite, right-click on the sprite and choose duplicate.
  2. When we use the duplicate option, we get a sprite with the same appearance, scripts, and variables. This time however, we don't want to carry over the scripts or variables, so delete them all from the grapher-double sprite.
  3. After we draw a point on the graph, we need to connect the line. Create a new broadcast message to make this happen. With the double sprite selected, add a broadcast block to the end of the script that begins with the when I receive graph block.
  4. Create a new broadcast message named connect. The following screenshot shows the script for the double sprite:
    Time for action connect the dotsdouble it calculationdots, connecting
  5. Now, we need to program the grapher-double sprite to act on the broadcast message. Add the when I receive block to the double sprite's script area. Select connect from the drop-down menu.
  6. The grapher-double sprite will chase the double sprite across the screen. From the Motion palette, add the go to block to the when I receive block. From the drop-down list, select the double sprite.
  7. As the grapher-double sprite chases the double sprite, we want to draw a line. From the Pen palette, add the pen down block to the go to block. From the Pen palette, add the set pen size to block.
  8. From the Pen palette, add the set pen color to block to the set pen size to block.
  9. From the Pen palette, add the pen down block to the set pen color to block.
  10. Run the script. Click the flag to reset the values, and then press the Space bar. The points on the graph should be connected by a line.
Time for action connect the dotsdouble it calculationdots, connecting

If you didn't adjust your pen size and color, feel free to customize those values. A larger pen size creates a thicker line.

When you click on the color square in the set color block, a color palette displays. Set the color by picking a spot with the eye dropper.

What just happened?

Calculate—Graph—Draw. These are the three general tasks in our project to this point.

Each time the double sprite calculated a value for the newAmount variable, it broadcast the graph message, which signaled the double sprite to move to a set of coordinates where it created a dot with the stamp block. After the double sprite created a dot, the grapher-double sprite chased after the double sprite, leaving a line in its wake.

When we clicked the flag, our graph was cleared from the stage. That's because we added the clear block to the double block in one of our earlier exercises.

Our graph almost worked correctly. If you studied the line we just added with the pen tool, you noticed that the line probably didn't start until the third dot or so. If you tested different days and startAmount values, then you might have seen an errant line, as shown in the following screenshot:

What just happened?

Before we fix this problem, see if you can identify why our line doesn't draw like we expect, and see if you can come up with a solution. I'll wait.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.116.50