Time for action – creating a behavior

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

We are going to use Stencyl's kill… instruction block to remove the collected fruit from the game, remembering that each fruit actor is a member of the Collectibles group. Execute the following steps in order to create a behavior:

  1. On the Dashboard, under the LOGIC heading, click on Actor Behaviors.
  2. Click on the green Create New button situated at the top of the right-hand panel.
  3. In the Create New… dialog box, enter the words Collect Fruit into the Name textbox and click on the Create button.
  4. The behavior editor will be displayed.
  5. Click on the + Add Event button at the top of the left-hand panel.
  6. In the pop-up menu that appears, move the mouse over Collisions.
  7. In the next pop-up menu, in the Any actor collides with… section, click on Member of Group.
  8. Double-click on the Actor – Group event label towards the top of the left-hand panel (immediately under the + Add Event button).
  9. Change the text to Collides with Collectibles and press Enter on the keyboard.
  10. In the orange when Actor hits a … block, displayed in the center panel, click on the Actor Group selector and click on Choose Group in the pop-up menu, as shown in the following screenshot:
    Time for action – creating a behavior
  11. In the Choose an Actor Group dialog box, select Collectibles and click on OK.
  12. Save the game. It's not ready for testing yet!

What just happened?

We have created a new actor behavior called Collect Fruit, which has a single empty event called Collides with Collectibles, but we haven't provided any instructions yet.

The event was initially called Actor – Group and although that is quite descriptive (we're responding to a collision between an actor and a group), we might have several Actor – Group collision events, for example, we will also need to check for collisions with actors in the Enemies group. To make it easier to identify what each event does, it's a good practice to give each event a descriptive name and, in this case, Collides with Collectibles is an appropriate name, because it describes the event that we're listening for.

Note

Note that this name is not used by Stencyl for referencing the event. It is simply a human-readable label, with the purpose of helping the developer to easily identify the events that we have created.

Stencyl behaviors can listen out for many different types of events, but we have chosen to listen for a very specific event, which is triggered when the actor collides with any actor in the specified group. When we clicked on the Actor Group option in the orange block, and specified the Collectibles group, we were telling the behavior to listen out for a collision event between this actor and any actor that is a member of the Collectibles group.

The orange when block in our new Collect Fruit behavior will listen out for a collision between Self and the Collectibles group, but which actor is the Self instruction referring to? The answer to this question demonstrates the beauty of Stencyl behaviors, because Self is whichever actor we attach the behavior to and we can attach it to any type of actor that we want. In other words, behaviors are reusable, they are not limited to being attached to a single type of actor. They can be attached to as many different actor types as we wish.

Note

The reusability of behaviors within Stencyl can be compared to Object Oriented Programming ( OOP) methodologies in traditional programming languages.

Currently, the behavior is not attached to an actor and we have not specified what action we want to take place when the collision event occurs, so let's do that now.

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

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