Time for action – creating an event to relocate the monkey

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

Ensure that the Health behavior is being displayed. It currently contains a single event called Countdown expired. Now perform the following steps:

  1. Click on + Add Event | Advanced | Custom Event.
  2. Rename the Custom Event to Relocate monkey.
  3. In the new orange when … happens event block, enter the name RestartLevel (note the capitalization and that there no spaces).
  4. In the block palette, under Actor, click on Position, locate the set x to 0 for Self block, and drag it into the when RestartLevel happens block.
  5. Hold down the Alt key on the keyboard (Option on Mac OS X), and drag the new set x to … for Self block so that a copy of it appears below the first one.
  6. In the top set x to … for Self block, enter the number 200 into the textbox.
  7. In the lower set x to 0 for self block, click on the x option and change it to y, then enter 175 in the textbox, as shown in the following screenshot:
    Time for action – creating an event to relocate the monkey
  8. Save the game. It's not ready for testing yet.

What just happened?

We've created a Custom Event called Relocate monkey. Custom events allow us to create a set of instructions that will be started by a unique trigger that we can specify.

The behaviors we have created so far rely on built-in events, such as when the value of an attribute is less than a specified number, or when actors belonging to the specified groups collide with each other. Our new custom event will listen out for the triggering of an event called RestartLevel, and we'll be creating an instruction to trigger that event in the next section, Time for action – triggering the custom event.

Once our custom event has been triggered, it will execute the instructions inside the when RestartLevel happens block. In this case, the instructions specify a new x and y position for the monkey, the actor will be placed right back where it started.

How did we know which numbers to specify for the x and y positions? If we open up the jungle scene in the Scene Designer, we can find out the start position of the monkey by clicking on the monkey actor in the scene and looking down to the lower left-hand corner of the Stencyl window, where we will see the coordinates of the currently selected actor, as shown in the following screenshot:

What just happened?

Note

The x and y coordinates depend on exactly where the monkey actor was placed in the jungle scene, so have a look at the coordinates, and adjust the numbers in the blue set x to … for Self and set y to … for Self blocks accordingly.

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

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