In this chapter, we will begin using Stencyl's signature visual programming interface to create logic and interaction in our game. We will create this logic using a WYSIWYG (What You See Is What You Get) block-snapping interface. By the end of this chapter, you will have the Player Character whizzing down the screen in pursuit of a zigzagging air balloon! Some of the things we will learn to do in this chapter are as follows:
One of the great things about this visual approach to programming is that it largely removes the unpleasantness of dealing with the syntax (the rules of the programming language), and the inevitable errors that come with it when we're creating logic for our game. This allows us to focus on the things that matter the most in our games: smooth, well-wrought game mechanics and enjoyable, well-crafted gameplay.
The first behavior that we are going to create is the Player Handler. This behavior will be attached to the Player Character (PC) that exists in the form of the Cowboy Actor Type. This behavior will be used to handle much of the game logic and will process the lion's share of the player input.
It's time to create our very first behavior! Go to the Dashboard, under the LOGIC heading, select Actor Behaviors, as shown in the following screenshot:
Click on This game contains no Logic. Click here to create one. to add your first behavior. You should see the Create New... window appear:
Enter the Name as Player Handler
, as shown in the previous screenshot, then click on Create. You will be taken to the behavior designer window, as shown in the following screenshot:
Let's take a moment to examine the various areas within the behavior designer window. From left to right, as shown in the preceding screenshot, we have the following:
Perform the following steps:
You will notice that we now have an event in our events pane, called Updated, along with a block called always on our canvas. In Stencyl events lingo, always is synonymous with When Updating. Take a look at the following screenshot:
Since this is the only event in our Behavior at this time, it will be selected by default. The always block (which is a yellow block with a red flag) is where we will put the game logic that needs to be checked on a constant basis for every update of the game loop (this will be commensurate with the frame rate at runtime, around 60 fps, depending on the game performance and system specs). Before we proceed with the creation of our conditional logic, we must first create a few attributes.
Switch to the Attributes context in the blocks palette, as shown in the following screenshot:
There are currently no attributes associated with this behavior. Let's add some, as we'll need them to store the important information of the various types, which we'll use later on to craft the game mechanics. Click on the Create an Attribute... button, as shown in the following screenshot:
In the Create an Attribute… window that appears, enter the Name as Target Actor
, set Type to Actor, check Hidden?, and click on OK, as shown in the following screenshot:
Congratulations! If you take a look at the bottom of the blocks palette window, you will see that you have added your first attribute, Target Actor, of type Actor, and it is now available for use in our code, as shown in the following screenshot:
Next, let's add the five Boolean attributes. A Boolean is a special kind of an attribute that can be set to either true or false. These are the only two values that it can accept. First, let's create the "Can Be Hurt" Boolean, by performing the following steps:
Can Be Hurt
.Can Switch Anim
Draw Lasso
Lasso Back
Mouse Over
If you have done this correctly, you should now see six attributes in your attributes list; one under Actor and five under Boolean, as shown in the following screenshot:
Now let's follow the same process to further create seven attributes, only this time, we'll set the Type for all of them to Number. The Name for each one will be as follows:
Health
(set to Hidden?)Impact Force
(set to Hidden?)Lasso Distance
(set to Hidden?)Max Health
(don't set to Hidden?)Turn Force
(don't set to Hidden?)X Point
(set to Hidden?)Y Point
(set to Hidden?)If all goes well, you should see your list of attributes updated according to the following screenshot:
We will add just one additional attribute. Click on the Create an Attribute… button again and perform the following steps:
Mouse State
.Excellent work! At this point, you have created all of the attributes you will need for the Player Handler behavior!
We need to create a few custom events in order to complete the code for this game prototype. For programmers, custom events are like functions that don't accept parameters. You simply trigger them at will in order to execute a reusable bunch of code. To accept the parameters, you must create a custom block, by performing the following steps:
You will see that a second event, simply called Custom Event, has been added to our list of events, as shown in the following screenshot:
Now, double-click on the
Custom Event in the events stack to change its label to Obj Click Check
(for readability purposes, this does not affect the event's name in the code and is completely ignored by Stencyl), as shown in the following screenshot:
Now, let's set the name, as it will be used in the code. Click between when and happens, and insert the ObjectClickCheck
name:
From now on, whenever we want to call this custom event in our code, we will refer to it as ObjectClickCheck
. Go back to the When Updating event by selecting it from the events stack on the left-hand side of the screen. We are going to add a special block to this event, which calls the custom event we created just a moment ago. Perform the following steps to add a special block:
Now, ObjectClickCheck
will be executed for every iteration of the game loop! It is usually good practice to split up your code like this, rather than having it all in one really long event. This would be confusing and terribly hard to sift through when behaviors become more complex!
There is a chance to assess what you have learnt from this chapter so far. We will create a second custom event; see if you can achieve this goal using only the skeleton guide, which is mentioned later. If you struggle, simply refer to the detailed steps we followed for the ObjectClickCheck
event:
Handle Dir Clicks
for readability purposes.HandleDirectionClicks
name. This is the handle we will use to refer to this event in the code.ObjectClickCheck
to HandleDirectionClicks
. Keep the PlayerHandler
value for the behavior field.Were you successful? If so, you should see these changes and additions in your behavior (note that the order of the events in the events pane does not affect the game logic, or the order in which the code is executed), as shown in the following screenshot:
Learning to create and utilize custom events in Stencyl is a huge step toward mastering the tool, so congratulations on having come this far!
As with all the fields of programming and software development, it is important to periodically and iteratively test your code. This way, it's much easier to catch and repair mistakes. On this note, let's test the code we've written so far, using print blocks. Navigate to and select Flow | Debug | print from the blocks palette, as shown in the following screenshot:
Now, drag a copy of this block into both of your custom events, snapping it neatly into the when happens block as you do so. Name them as follows:
ObjectClickCheck
event, type Object Click Detected
in the print blockHandleDirectionClicks
event, type Directional Click Detected
in the print blockWe are almost ready to test our code. Since this is an Actor Behavior, however, and we have not yet attached it to our Cowboy actor, nothing would happen if we ran the code. We must also add an instance of the Cowboy actor to our scene by performing the following steps:
0.8
:Well done! You've just added your first behavior to an actor type and added your first actor instance to a scene! We are now ready to test our code. First, click on the Log Viewer button on the toolbar, as shown in the following screenshot:
This will launch the Log Viewer, which we examined briefly in Chapter 1, Exploring the Stencyl Toolset and Game Engine. The Log Viewer will open up, at which point we need to only set Platform to Flash (Player), and click on the Test Game button to compile and execute our code, as shown in the following screenshot:
After a few moments, if you have followed all of the steps correctly, you will see that the game window opens on the screen, and a number of events appear on the Log Viewer. However, none of these events have anything to do with the print blocks that we added to our custom events. Hence, something has gone wrong and must be debugged. What could it be? Well, since the blocks simply are not executing, it's likely a typo of some kind. Let's take a look at the Player Handler again, and you'll see that within the Updated event, we've referred to the behavior name as PlayerHandler
in both the trigger event blocks with no space inserted between the words Player
and Handler
, as shown in the following screenshot:
Update both of these fields to Player Handler
and be sure to include the space this time so that it looks like the following screenshot (to avoid a recurrence of this error, you may wish to use the drop-down menu by clicking on the downward grey arrow, then selecting Behavior Names to choose your behavior from a comprehensive list):
Great work! You've successfully completed your first bit of debugging in Stencyl. Click on the Test Game button again. After the game window opens, if you scroll down to the bottom of the Log Viewer, you should see the following events piling up:
These INFO events are being triggered by the print blocks that we inserted into our custom events, and prove that our code is now working. Excellent job! Much of the rest of the code for the Player Handler behavior will be covered in Chapter 7, Complex Behaviors and Code. Let's move on to a new actor; prepare to meet Dastardly Dan!
Let's add the balloon actor to our game, and insert it into Level 1:
Balloon
, and click on Create.Default
.Game AssetsGraphicsActor Animations
and select Balloon.png
.1
, and click on Add to commit this frame to the animation.The Balloon actor type is now free of collision shapes, and hence will not interact physically with the other elements of our game levels. Next, switch to the Physics context as shown in the following screenshot:
Set the following attributes:
Just before we add this new actor to Level 1, let's add a behavior or two. Switch to the Behaviors context as shown in the following screenshot:
Then, perform the following steps:
Prefab behaviors (also called as shipped behaviors) enable us to implement some common functionalities without reinventing the wheel, so to speak. The great thing about these prefab behaviors, which can be found in the behavior library, is that they can be used as templates and modified at will. Let's learn how to add and modify a couple of these prefab behaviors now.
Some prefab behaviors have exposed attributes that can be configured to suit the needs of the project. The Wave Motion behavior is one such example. You can select it from the stack, and configure the Attributes as follows:
5
.64
.128
.Fantastic! Now let's add an instance of the Balloon actor to Level 1:
Before we test the game one last time, we must quickly add a prefab behavior to the Cowboy actor type, modifying it slightly to suit the needs of this game (for instance, we will need to create an offset value for the y-axis, so the PC is not always at the centre of the screen):
Y Offset
.Go back to the Cowboy actor type, and open the Behavior context. Navigate to File | Reload Document (
Ctrl+R or Cmd+R) to update all the changes. You should see a new configurable attribute for the Camera Follow behavior, called Y Offset. Set its value to 70
, as shown in the following screenshot:
Excellent! Now go back to the Dashboard and perform the following steps:
8.0
.18.117.188.138