Learning Stencyl 3.x Game Development Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – attaching a behavior to an actor

The game file to import and load for this session is 5961_02_06.stencyl.

Note

We will be working with the monkey actor, so ensure that it is currently on display in the Animation Editor.

Stencyl comes packaged with many useful behaviors—and we first need to tell Stencyl to attach the required behaviors to the relevant objects within our game. In this case, we need to attach a behavior to the monkey actor, so that we can control it with the keyboard.

  1. Click on the Stencyl Book Monkey tab to display the monkey in the Animation Editor.
  2. Click on the Behaviors button in the row of buttons at the upper-center of the screen.
  3. Click on the message Click here to choose a Behavior to attach to this Actor Type that appears in the main panel.
  4. In the left-hand side panel of the Choose a Behavior dialog box, click on the Controls option that appears under the FROM YOUR LIBRARY heading.
  5. Scroll down the list of behaviors that appears, until you can see the thumbnail image for the Jump and Run Movement, then double-click on it.

What just happened?

We needed to find a behavior that would allow us to control the monkey with the keyboard, so we first selected the tab for the monkey actor, and then we chose the Jump and Run Movement behavior, so it is now attached to the monkey actor.

The instructions in the newly-attached behavior will now apply to the monkey actor but, before we test our game, we need to provide the behavior with some information, so that it knows exactly what to do.

Configuring behaviors with Attributes

Some behaviors must be provided with information so that they can do their job but, at the moment, the Jump and Run Movement behavior doesn't have all the information that it needs.

The configuration information that we supply to a behavior are called attributes, and an attribute could be almost any type of data such as a number, text, a type of actor, or even a list of items.

As we can see, if we scroll down the configuration information for the Jump and Run Movement behavior that we have onscreen, there are quite a lot of attributes needed to configure this behavior!

Fortunately, the Jump and Run Movement behavior only needs us to provide the following attributes to get started:

  • The keys that will control the actor.
  • The animations that will be used when then actor moves.

Our next task is to specify these attributes so that we can move the monkey with the keyboard and see the correct animations as the monkey moves around the scene.