Adding behaviors

Behaviors are quick ways to make an object act a certain way. For example, you can add a Platform behavior to an object, and the Solid behavior to the floor, and you instantly can jump around like a platformer game. You can usually do the same in events, but behaviors are much quicker! Construct has a wide range of behaviors, but here are a few that we'll use in this tutorial.

  • 8 Direction movement: this lets you move an object around with the arrow keys. It will do nicely for the player's movement.
  • Bullet movement: this simply moves an object forwards at its current angle. It'll work great for the player's bullets. Despite the name, it'll also work nicely to move the monsters around - since all the movement does is move objects forwards at some speed.
  • Scroll to: this makes the screen follow an object as it moves around (also known as scrolling). This will be useful to keep the view centered on the player.
  • Bound to layout: this will stop an object leaving the layout area. This will also be useful on the player, so they can't wander off outside the game area!
  • Destroy outside layout: instead of stopping an object leaving the layout area, this destroys it. It's useful for our bullets. Without it, bullets would fly off the screen forever, always taking a little bit of memory and processing power. Instead, we should destroy the bullets once they've left the layout.
  • Fade: this makes an object fade out, which we will use on the explosions.

Let's add these behaviors to the objects that need them.

How to add a behavior

Let's add the 8 direction movement behavior to the player. Click the player object to select it. In the Properties Bar, notice the Behaviors category. Click the Behaviors link there. The Behaviors dialog for the player will open.

The Behaviors dialog The Behaviors dialog

Click Add new behavior in the behaviors dialog. Double-click the 8 direction movement to add it.

Do the same again and this time add the Scroll To behavior, to make the screen follow the player. Then also add the Bound to layout behavior, to keep them inside the layout. The behaviors dialog should now look like this:

Close the behaviors dialog. Now try pressing Preview to run the game so far! Once the preview starts, notice you can already move around with the arrow keys, and the screen follows the player. You also can't walk outside the layout area, thanks to the Bound to Layout behavior. This is what behaviors are good for - quickly adding common features. We'll be using the event system soon to add custom features.

Adding the other behaviors

We can add behaviors to the other objects by the same method - select it, click the Behaviors link to open the behaviors dialog, and add some behaviors. Let's add these other behaviors:

  1. Add the Bullet movement and Destroy outside layout to the Bullet object (no surprises there)
  2. Add the Bullet movement to the Monster object (because it just moves forwards as well, just at a slower speed)
  3. Add the Fade behavior to the Explosion object (so it gradually disappears after appearing). By default the Fade behavior also destroys the object after it has faded out, which also saves us having to worry about invisible Explosion objects clogging up the game.

If you run the game, you might notice the only thing different is any monsters you can see suddenly shoot off rather quickly. Let's slow them down to a leisurely pace. Select the Monster object. Notice how since we added a behavior, some extra properties have appeared in the properties bar:

This allows us to tweak how behaviors work. Change the speed from 400 to 80 (this is in pixels travelled per second).

Similarly, change the Bullet object's speed to 600, and the Explosion object's Fade behavior's Fade out time to 0.5 (that's half a second).

Create some more monsters

Holding Control, click and drag the Monster object. You'll notice it creates another instance. This is simply another object of the Monster object type.

Object types are essentially 'classes' of objects. In the event system, you mainly deal with object types. For example, you might make an event that says "Bullet collides with Monster". This actually means "Any instance of the Bullet object type collides with any instance of the Monster object type" - as opposed to having to make a separate event for each and every monster. We'll cover more on object types vs. instances later. For now, a good example to think about is different types of enemy are different object types, then the actual enemies themselves (which there might be several of) are instances of those object types.

Using Control + drag, create 7 or 8 new monsters. Don't place any too close to the player, or they might die straight away! Remember you can zoom out with Control + mouse wheel down if it helps, and spread them over the whole layout. You should end up with something a bit like this.

Now it's time to add our custom logic with Construct's visual method of programming - events!

Share this Tutorial

Make games in your browser

You may use this tutorial for any purpose (even commercial) if you properly attribute it. Click here for more information.