Time for action – adding an action and attaching to it an actor

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

Ensure that the Behavior editor is currently displaying the orange when block for the Collides with Collectibles event in the Collect Fruit behavior, as shown in the following screenshot:

Time for action – adding an action and attaching to it an actor

When following the given steps, carefully refer to the screenshots, as they will assist you in correctly placing the blocks.

  1. Click on the Actor button above the instruction block palette at the top of the right-hand panel, to ensure that it is selected, as shown in the following screenshot:
    Time for action – adding an action and attaching to it an actor
  2. Click on the Properties category button in the row of buttons that is displayed immediately below the top buttons in the instruction block palette, as shown in the following screenshot:
    Time for action – adding an action and attaching to it an actor
  3. Locate the blue kill Self block in the Alive / Dead subcategory of the right-hand panel.
  4. Click and drag the blue kill Self block, and drop it into the blank area of the orange when Self hits a… block in the center panel as shown in the following screenshot. When the white snap-line appears, let go of the mouse button. This will allow the block to snap into place:
    Time for action – adding an action and attaching to it an actor
  5. Locate the blue of group block at the top-right of the orange Self hits a ... of Collectibles block, and drag it onto the word Self, which is inside the blue kill Self block that we just added, as shown in the following screenshot:
    Time for action – adding an action and attaching to it an actor
  6. Click on the green Attach to Actor Type button at the top-right of the block palette as per the following screenshot:
    Time for action – adding an action and attaching to it an actor
  7. In the Choose an Actor Type dialog box, double-click on the Stencyl Book Monkey icon, the behavior list for the monkey will be displayed.
  8. Click on the Collect Fruit tab to return to the completed event, which should now look like the following screenshot:
    Time for action – adding an action and attaching to it an actor
  9. Test the game. Make the monkey collide with a piece of fruit.

What just happened?

When the monkey collides with a piece of fruit, the fruit disappears!

We had already created the Fruit Collected behavior, and had specified that it must listen out for a collision event between Self (the monkey) and any member of the Collectibles group. However, we had not specified which action should take place when the collision occurred, so we added the kill Self instruction block to the when Self hits a... of Collectibles event, and configured it to remove the actor that we just collided with. The blue of group block is a label that provides a reference to the other actor involved in the collision, that is the actor we want to kill.

Finally, we attached the behavior to the monkey actor. Now, in our game, when the behavior runs, any references to Self in the behavior blocks will be considered to mean this monkey actor, and any references to of group will refer to the member of the Collectibles group that the monkey collided with.

Tip

A very common mistake is to forget the final step, attaching the behavior to the appropriate actor! If a behavior appears not to run at all, this is the first mistake to check for.

Sometimes when developing or reviewing the design of a behavior, it can be helpful to express it in English, rather than in technical terms. If we were to write our new event in English, it could be stated as follows:

When this actor hits a member of Collectibles, kill the Collectibles actor that we just collided with.

In the previous sentence, the phrase before the comma is the event that is being listened to in the orange when self hits a ... of Collectibles: block. The phrase after the comma is the action that is to be carried out when the event is triggered, that is the instruction in the blue kill Self block.

Almost all behaviors in Stencyl are designed with this basic principle—what am I listening for, and what should I do when it happens? Behaviors can be far more complicated than this, but this is the essence of most behaviors.

The actor's behavior screen

When we attached the Collect Fruit behavior to the monkey actor, the actor's behavior screen was displayed. However, we switched back to the Collides with Collectibles event, so that we could compare it with the screenshot. Let's return to the monkey's behavior screen by opening the monkey actor from the Dashboard option, and by clicking on the Behaviors button at the top of the right-hand panel.

The actor's behavior page provides some information that is useful for us to know. If we look at the left-hand panel, we can see a list of behaviors that are currently attached to this actor:

  • Jump and Run Movement
  • Cannot Exit Scene
  • Camera Follow
  • Collect Fruit

Also, at the top of the right-hand panel, there is a red Deactivate Behavior button, which can be used to disable the currently selected behavior. This feature can be useful for debugging because we can disable behaviors during the process of tracking down a problem. It's also useful to know that behaviors can be deactivated and reactivated within instruction blocks. This is a more advanced feature but it's a feature that's worth remembering for future use.

Adding an additional event to a behavior

We have created an event that makes fruit disappear when the monkey collects it, and we now need to add a new event that will kill the monkey when it collides with an enemy actor.

It is quite possible to create a brand new behavior that will do this job, but we're going to add an additional event to the behavior that we have just created. This means that the behavior will have two events to listen out for, namely:

  • A collision between the monkey and a member of the Collectibles group
  • A collision between the monkey and a member of the Enemies group

Originally, we created the behavior to do one job, and we gave it a sensible name Collect Fruit. However, as we're going to add a second event that is not related to fruit collection, this name won't make sense any more, so we need to give the behavior a more relevant name.

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

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