teachinTech


Using a Gamepad

Console games make heavy use of the gamepad. Construct2 has support for gamepads.

Turn In: Gamepad-Testing.capx file

gamepads work very well for some games. This activity explores how to use the gamepad in a Construct2 game.

Note: Whether or not you have a gamepad, work through the activity so that you are familiar with how Construct2 works with gamepads.

The Gamepad

In order to use a gamepad, you need to have one and also be able to have it recognized by the computer using Construct2 or connected to your game.

If you have a wired gamepad with a USB interface that is capable of working with a computer, plug it into your computer. After doing so, the automatic installation of drivers may begin or you may have to manually install drivers on your computer for it to recognize the gamepad.

For wireless gamepads, you will need some sort of wireless receiver that plugs into your computer and talks with the wireless gamepads. For instance, the Xbox 360 Wireless Gaming Receiver for Windows can be used to connect up to 4 wireless controllers to a Windows computer

Once the gamepad is connected to and recognized by the computer, you are ready to start using it in your games.

The Gamepad Buttons

This section provides a quick review of the buttons on a gamepad. Your gamepad may be a little different and may require some research or experimentation on your part.

In the upper middle to right corner of the controller are the buttons: "Start / Menu", "Back / View", Button A, Button B, Button X, and Button Y. These buttons are either pressed or not pressed.

On the front of the gamepad are left and right shoulder buttons that are either pressed or not pressed. Just below the shoulder buttons are the left and right triggers.

Gamepads have a D-pad on the lower left corner of the face of the controller where you can press the D-pad up, down, left, or right.

The left and right sticks are on either side of the D-pad.

Gamepad Object in Construct2

Start a new Construct2 project. Make the layout and window sizes both 1024 by 768.

Insert new object then from the Input section, pick Gamepad.

The Gamepad object appears in the Projects window and also in the Objects window but there is nothing to put on the layout. Adding this object enables some items on the Event sheet that were not previously available.

Create a text object named Supported and change the text to "Gamepads not supported". Create a text object named HasGamepad and change the text to "Does not have gamepad". Create another text object called Connected and change the text to "Gamepad disconnected"

Run layout and verify that you can see the text created above.

On the Event sheet click on Add event and choose the Gamepad and pick "Gamepads are supported". Add action and pick Supported, click Next then pick "Set text" and click Next then put in the text "Gamepad supported"

Run layout and see if the gamepads are supported or not supported. The add the following events ...

Add event "Gamepad->Has gamepads" with the action "HasGamepad->Set text" then the text "Has gamepads".

Add event "Gamepad->On gamepad connected" with the action "Connected->Set text" then the text "Gamepad connected"

Add event "Gamepad->On gamepad disconnected" with the action "Connected->Set text" then the text "Gamepad disconnected"

Run layout and you will see the gamepad status and capability. If you have a controller, plug it in and unplug it while the game is running. The statuses on the game may change. You may have to click on one of the controller buttons for the "has gamepads" to show.

Note: When you Run layout, the gamepad might not be immediately recognized. You may need click your mouse on the browser or within the game to have it recognize the controller and make that web browser the active window.

A Little Game Play

Create a player, an enemy, and a paintball. Add the bullet behavior to the paintball.

Add a Gamepad event for "On button pressed" then click Next and choose the right shoulder trigger. Notice that the gamepad number is 0 which is the first controller. You can change the 0 to 1 if you have a second controller. Add action, choose the player and spawn a paintball.

Let's move the enemy with the D-pad. Add event, pick Gamepad, then choose D-pad up. For the action pick the enemy then move it 15 pixels at the angle of -90. Add events for the D-pad down that moves the enemy 15 pixels 90 degrees, D-pad left 15 pixels at 180 degrees, and D-pad right 15 pixels at 0 degrees.

The sticks work a little bit differently, specifically they work on how far they are pressed, not if pressed/moved. They also work on the X axis (left to right) and Y axis (top to bottom). The movement using the stick is more natural instead of just up/down/left/right.

Lets use the right stick to move the player, we are going to move when the stick is more than half way (the 0.5 shown). Add event, pick Gamepad then "Compare axis". Choose "Right analog X axis" then "greater than" and the value of 0.5. For the action, pick Player then "Move at angle" at 0 degrees for 5 pixels. Do the same except Right analog X axis less than -0.5 then move 5 pixels at 180 degrees. Then Right analog Y axis greater than 0.5 with player moving 5 pixels at 90. Right analog Y axis less than -0.5, move player 5 pixels at -90.

Run layout and verify that the right stick moves the player as you would expect.

For More Information

Use the link below for the manual page on the Construct2 Gamepad object. You can also search the tutorials and other resources for more information on using the Construct2 Gamepad

Construct2 Manual - Gamepad object


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