teachinTech


First Look at Construct2 Behaviors

Behaviors provide specific capabilities to the objects in your game they are added to. In this activity, we are going to begin exploring Construct2 behaviors.

Turn In: File named Maze2.capx

We are going to make a new version of a game file then job into learning about and using behaviors.

Making a New Version of Your Game File

The computer software, including games, that you use and play often have a version number. Some use numbers in sequence starting with 1 for the version of their software and others use the year for the version of their software (i.e. 2016).

We are going to use a very simple method to keep different versions of the games we create. Doing this allows you to go back to what the game looked like in a previous version.

Find the Maze1.capx game file. If you do not have a Maze1.capx file, create it. Right click on the Maze1.capx file and pick copy. Right click in the white area near the Maze1.capx file and pick Paste. You will see a file named "Maze1 - Copy.capx". Right click on the "Maze1 - Copy.capx" and change the name to Maze2.capx

Construct2 also makes temporary backup files if you need to go back to previously changed items.

Note: Version control is a category of software that maintains versions of the games and programs. You can "check in" changes at any time to have the software keep that version and then be able to compare different versions and go back to previous versions. Git, Subversion, and CVS are three of the more popular open source (free) version control software programs.

Our First Behaviors

Open the Maze2.capx game file, if you don't have this file, create it. The windows size and the layout size should both be 320 by 480 which means you will be able to see the entire game surface all of the time.

There should be a player object in your game that will move through the maze. If you do not have a player object create it now.

Click on your player object to select it then in the Properties for the player, find and click on the Behaviors link. This will bring up the Behaviors window. You will see buttons to add behaviors, rename behaviors, delete behaviors, and move the existing behaviors either up or down.

Click the + button to add a new behavior. Scroll down and notice that their are several types of behaviors ... Attributes, General, and Movements. Like other pop-up boxes in Construct2, there is a Help link in the lower left corner. In the upper right corner is a search box. Type the number 8 into the search box and notice that found one match ... the 8Direction behavior in the Movements category. Click Add to give the player the 8Direction behavior then click the Behaviors box. Next, click on the Run Layout button to run your game. Once your game is running, use your arrow keys to move the player around. Be sure to go up, down, left, and right as well as go off the screen and try to come back on the screen. Observe how the player behaves (pun intended).

Back in Construct2, look at the Properties (on the left hand side) for the player and notice that it now has properties for the behavior 8Direction. When you click on the property name, at the bottom of the property will be a help specific to that property.

Did you notice that as you moved up, down, left, and right that your player was turning directions. Find the 8Direction property named "Set angle" and change it from Yes to No. Click on the Run Layout and try your game out again. Notice this time the player object does not rotate when turning.

Change the 8Direction property "Directions" to "Up & down" and run the layout. Notice which of the arrow keys work when you try to go up, down, left, and right. Try the other direction options.

The default Max speed is 200, Acceleration 600, and Deceleration is 500. Change the Max speed to 50, run the layout and observe the changes. Likewise, change the acceleration to 50 and the deceleration to 50. Run the layout and observe the changes. Set the properties to the 8Direction to settings such that the player moves as you would like them to.

Objects can have multiple behaviors. In the Object bar, right click on the player object and pick Behaviors from the menu. Notice that this is the same as clicking on the Behaviors link in the properties for the object.

Add the behavior, "Bound to layout". Run the layout and try to go off of the game surface. Notice that the player can go to the edge of the game but cannot leave the game's surface.

Go into the Behaviors for the player and select the "BoundToLayout" then delete it. Add in the Wrap behavior then run the layout. Again try to go off of the game surface to the left, the right, the top, and the bottom. Notice that you are "wrapping" from one side of the surface to the other side.

Duplicating Objects

The Maze2.capx game file should have both a Horizbar and a Vertbar object. If not create them by creating either a 10 by 100 black filled sprite and a 100 by 10 black filled sprite.

To create a maze we need some additional bars and also change the size of the bars. While we could create new sprites, a better way is to duplicate existing sprites. While holding down the Ctrl (or Control) key on the keyboard, click on the HorizBar and drag it to a new location. Let go of the mouse and the Ctrl key and you will have a duplicate Horizbar. Click to select one of the HorizBar images and use the white blocks on the end of the image to stretch it out to the size of your choosing.

Duplicate and resize both the Horizbar and Vertbar objects to make a maze of bars that your player can navigate through. You can click on one of the bars then click and hold to drag it to the desired location. Leave several openings in your maze.

Run the layout and move your player in the maze, also try to go through the wall of the maze ... notice that you are able to move your player through the bars.

The Solid Behavior

As its name suggests, the solid behavior is used to make an object solid. Select one of the Horizbar objects and add the Solid behavior to it. Notice that when you do, the Solid behavior is on all instances of the Horizbar but is not on the Vertbar. Click Run layout and try to navigate through both the Horizbar and Vertbar objects.

Add the Solid behavior to the Vertbar object, click Run layout, and verify that the player is not able to move through any of the bars.

The Solid behavior has one property called "Initial state" that is either Enabled or Disabled. Click on one instance of the Vertbar and change the "Initial state" from Enabled to Disabled. Run the layout and try to go through the other Vertbars and also the specific one that you disabled the "Initial state". Notice that only the one instance of the Vertbar that you disabled allows the player through.

The Bullet Behavior

The Maze2.capx game file should have an enemy character. If it does not, create a new sprite.

Add the Bullet behavior to the enemy object. Run the layout. As its name suggests, the Bullet behavior propels the enemy across the screen.

Add the Wrap behavior to the enemy to make this more interesting.

Initially the Bullet has the speed property of 400 which for many uses of this behavior is appropriate but for our enemy is a little too fast. Slow it down to the speed of 100. Run the layout and observe the results.

Change the Gravity property for the enemy's bullet behavior to 5. Run the layout. Notice that the enemy drops as if gravity has an effect on it.

Change the "Bounce off solids" from no to yes and run the layout. You may have to adjust your positioning of either the enemy or the bars but what how this change affects the enemy. Enemy add bullet behavior. Run layout.


Copyright © 2021 Eric Schumm. Permission granted to freely use this in your classroom.