|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "The gamepad package" |
| 4 | +date: 2017-6-10 19:49:11 +0200 |
| 5 | +author: arcadio |
| 6 | +categories: gamepad packages |
| 7 | +heropic: /pictures/gamepad.jpg |
| 8 | +--- |
| 9 | + |
| 10 | +The gamepad is the standard input method in console games, but it can also be used in other devices such as PCs, and thanks to the `gamepad` package it is really easy to incorporate it into your game. |
| 11 | + |
| 12 | +First of all, you will need to add the package as a dependency to your game. You can do it using the command line `clockwork-tools`, opening a command line in your project folder and typing |
| 13 | + |
| 14 | +`clockwork add gamepad` |
| 15 | + |
| 16 | +Once you have added the dependency, you just need to spawn a `gamepad` object in any level you want to detect gamepad input. Add it to the level like this: |
| 17 | + |
| 18 | +{% highlight xml %} |
| 19 | +<object name="gamepad" type="gamepad" x="0" y="0" ></object> |
| 20 | +{% endhighlight %} |
| 21 | + |
| 22 | +and you will be ready to start detecting keyboard input! |
| 23 | + |
| 24 | +The `gamepad` object you just created will trigger several events that let you know when the user is interacting with it, here is a component that will listen to all of them: |
| 25 | + |
| 26 | +{% highlight js %} |
| 27 | +{ |
| 28 | + name: "gamepadListener", |
| 29 | + events: [ |
| 30 | + { |
| 31 | + name: "gamepadAxis", code: function (event) { |
| 32 | + //This event is triggered each frame, sends the state of the thumbsticks |
| 33 | + event.player; //The id of the gamepad |
| 34 | + event.values.forEach(function(thumbstick, i){ |
| 35 | + i; //The id of the thumbstick |
| 36 | + thumbstick.x; //The x value of that thumbstick |
| 37 | + thumbstick.y; //The y value of that thumbstick |
| 38 | + }); |
| 39 | + } |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "gamepadTrigger", code: function (event) { |
| 43 | + //This event is triggered each frame, sends the state of the triggers |
| 44 | + event.player; //The id of the gamepad |
| 45 | + event.leftValue; //The value of the left trigger |
| 46 | + event.rightValue; //The value of the right trigger |
| 47 | + } |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "gamepadDown", code: function (event) { |
| 51 | + //This event is triggered when a button is pressed |
| 52 | + event.player; //The id of the gamepad |
| 53 | + event.name; //The name of the button |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "gamepadUp", code: function (event) { |
| 58 | + //This event is triggered when a button is released |
| 59 | + event.player; //The id of the gamepad |
| 60 | + event.name; //The name of the button |
| 61 | + } |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "gamepadDisconnected", code: function (event) { |
| 65 | + //This event is triggered when a gamepad is disconnected |
| 66 | + event.player; //The id of the gamepad |
| 67 | + } |
| 68 | + } |
| 69 | + ] |
| 70 | +} |
| 71 | +{% endhighlight %} |
| 72 | + |
| 73 | +Just like that, you can add your own logic to detect gamepad input and act acordingly. |
| 74 | + |
| 75 | +Finally, remember that if you need to remember something about how the component is used, you can quickly access the package documentation from Visual Studio Code, running the `Browse Clockwork package documentation` command. |
0 commit comments