Time for action – adjusting the drop-location of the statue

The game file that needs to be imported and loaded for this session is 5961_04_06.stencyl.

Ensure that the Drop Actors Randomly behavior is displayed onscreen. Execute the following steps:

  1. Click on the drop-down arrow to right of the x textbox (it currently contains the number 320).
  2. Move the mouse cursor over Math in the drop-down menu.
  3. In the Arithmetic section of the pop-up menu, click on the 0 + 0 block.

    Note

    Note that the zeroes in the new green instruction block do not appear in our event blocks, they have been replaced by empty textboxes.

  4. In the left-hand blank textbox of the new of camera + … block, click on the drop-down arrow.
  5. Select Scene from the drop-down menu.
  6. In the Basics section of the pop-up menu, click on the red x of camera block.
  7. In the right-hand blank textbox of the of camera + … block, enter the number 320, as shown in the following screenshot:
    Time for action – adjusting the drop-location of the statue
  8. Test the game. Make the monkey run to the right of the scene.

What just happened?

Instead of the statues dropping at a location that is always 320 pixels from the left-hand edge of the scene, they will now always drop at a location 320 pixels from the left-hand edge of the screen.

To achieve this result, we used the x of camera block and a little bit of basic math (addition). The position x of camera is always the left-hand edge of the screen, which is zero pixels from the left of the screen. The screen width is 640 pixels, so if we add half the screen width (half of 640 = 320) to the camera's x position (0 pixels), we will always be specifying an x position of 320 pixels from the left of the screen. No matter where our monkey is, the statue will always be dropped into the center of the viewport.

The statues will now drop from the center of the screen as required, but there are still some refinements to be made to this behavior, and we'll work on them as we progress through this chapter.

Examining the scene instruction blocks

We have been learning how to use the create Actor Type at … block, which we found under the Actors category in the Scene section of the block palette.

Take this opportunity to examine the other instruction blocks that can be found in the Scene section of the block palette—particularly those in the View and Game Flow subcategories. Many of the more advanced blocks won't necessarily make sense yet, but there are some very interesting instruction blocks for which their purpose is immediately apparent. For example, in the View category, there is a shake screen for 0 sec with intensity 0 % block which, perhaps unsurprisingly, does exactly what it says, it shakes the screen for the specified time with the specified intensity.

Make a note of any interesting blocks, and consider how they might be useful in our game. Perhaps the scene could shake each time a statue hits the ground.

Preparing for future changes

We've created a problem for the future! What if we decide to change the screen size for our game? Perhaps we decide to target a different device for our game—the screen size of 640 pixels wide and 480 pixels high might not be appropriate. We've already learned that we can easily change these parameters in the Game Settings dialog box, but if we change the screen width, our game isn't going to work as planned. We've specified that the statue should be dropped at exactly 320 pixels from the left-hand edge of the screen, which is half the current screen width of 640. If, for example, we change the screen width to 800 pixels, then the statue won't drop in the center of the screen because half that width is 400 pixels. It will be dropped somewhere towards left of the center, which is not the desired location! Let's fix that problem.

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

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