Time for action – creating an accelerometer-controlled game

A game file is not required for this session!

  1. Create a new blank game called AccelloSoccer.
  2. Create a blank scene called Soccer Pitch, leaving all other scene settings at their default configurations.
  3. Download the actor called Stencyl Book Soccer Ball from StencylForge.
  4. Add the Stencyl Book Soccer Ball actor into the Soccer Pitch scene (so we don't forget to do it later).
  5. Attach the built-in Bounce off screen bounds behavior to the Stencyl Book Soccer Ball actor.
  6. Create a new actor behavior called Ball Movement, and attach it to the Stencyl Book Soccer Ball actor.
  7. Return to the newly created Ball Movement behavior, select + Add Event | Basics | When Updating, and rename the new Updating event to Move Ball.
  8. Add the instructions, shown in the following screenshot, to the new Move Ball event, noting that the accelerator's x multiplier is -100 (negative 100):
    Time for action – creating an accelerometer-controlled game
  9. Ensure the target device drop-down list is set to the correct mobile device, and the device is connected to the development computer.
  10. Click on the Test Game button, wait for the game to compile, and then play the game on the mobile device. Note how the ball acceleration increases when the device is tilted at a greater angle!

What just happened?

We have created the rudiments of an accelerometer-controlled game in only 10 steps!

Note

The completed AccelloSoccer sample game file, named 5961_10_03.stencyl, can be located in the book's download files.

We should by now be familiar with creating blank games, scenes, and adding behaviors. So, we only need to look in detail at the Ball Movement behavior that we have attached to the soccer ball actor.

The Move Ball event always checks the values of the x and y feedback from the device's accelerometer and it sets the scene's x and y gravity based on those values. As we learned in our earlier experiments, the accelerometer returns values between -1 and +1; however, these values are far too small to have any effect on the scene's gravity. To increase the feedback returned by the accelerometer to values that will be useful to our game, we have simply multiplied them by a factor of -100 for the x axis, and +100 for the y axis.

While these multipliers are a good starting point, experimentation is often required to achieve accelerometer controls to which the player can easily assimilate; so, do experiment with the multiplier numbers to attain a better understanding of how they will affect gameplay.

Autorotate

It's important to be aware that, typically, the accelerometer should not be used with the autorotate feature on iOS devices (at the time of writing, Stencyl does not support autorotate on Android). This is configured in the game settings, and can be found in the main Stencyl menu under Settings | Mobile | User Input.

By default, autorotate is disabled in Stencyl, because most games do not use this feature. However, it can sometimes be useful — imagine a game in which the normal gameplay display mode was portrait, but when turning the device to landscape, an in-game information screen was displayed. This could offer some interesting design possibilities if implemented appropriately, but if a game, such as our sample soccer game had autorotate enabled, this would cause havoc for the player, as the accelerometer feedback would appear to be reversed each time the device was rotated. iOS developers can try enabling autorotate in the soccer game's settings (select Settings | Mobile | User Input), and test the game on their mobile device to experience the problem firsthand.

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

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