Chapter 4. Creating Behaviors

In Chapter 3, Detecting Collisions, we configured collision shapes for the actors and tiles so that collisions are detected when required, and our game now allows the player to traverse the scene with some basic, predictable interactivity. However, although the monkey is now colliding with other actors, there are currently no responses to these collisions, other than a bump when some of the actors collide.

We need to take control of the game, so in this chapter, we're going to learn how to create instructions that will carry out specific actions when collisions and other events occur within our game.

In this chapter we will be:

  • Creating custom behaviors
  • Understanding the instruction block palette
  • Creating a timed event
  • Examining screen size and scene size
  • Introducing randomness into our game
  • Implementing our first special effect
  • Understanding active actors
  • Creating a countdown timer
  • Implementing decision making in our game
  • Repositioning an actor during gameplay
  • Triggering custom events in our behaviors

Creating custom behaviors

When an event such as a collision occurs during gameplay, we would like to be able to decide exactly what happens, rather than being limited to using the built-in behaviors that have been provided for us. We're going to create our own custom behaviors that;

  • Respond to collisions and other events
  • Use a random number to make gameplay less predictable
  • Utilize a timer to carry out actions at regular or delayed intervals
  • Implement custom events that can be triggered by other behaviors

Creating our first custom behavior

As we discovered in the Using behaviors to interact with our game section in Chapter 2, Let's Make a Game!, behaviors are the instructions and rules that our game will use. We've already used some behaviors that have been built for us and are provided with Stencyl:

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

We found the previously mentioned behaviors in the built-in behavior library, and we attached them to the monkey actor so that it would follow the specified instructions. Up until now, we haven't concerned ourselves with how these behaviors work. We've just accepted that they do the required job, but now it's time to learn about what is going on in the background, and to create a custom behavior of our own.

Currently, when our monkey runs past a piece of fruit, nothing happens, not even a bump, because in Chapter 2, Let's Make a Game!, we configured all the fruit actors to be sensors.

However, we would like something to happen. In this case, we want the fruit to disappear so it can't be collected twice.

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

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