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:
When following the given steps, carefully refer to the screenshots, as they will assist you in correctly placing the blocks.
Collect Fruit
tab to return to the completed event, which should now look like the following screenshot: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.
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.
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:
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.
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:
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.
3.15.3.167