Archive
Construct
19min
in this guide you will learn how to use the construct2 airconsole plugin to add local multiplayer to your game if you are new to airconsole then you should read the getting started guide crystal control made with construct2 setup you can download the plugin files at our airconsole construct2 github repository download the airconsole c2addon airconsole c2addon file on github open construct2 drag and drop the airconsole c2addon airconsole c2addon file into the construct2 window enable airconsole to be able to use the airconsole in a construct2 game you have to add a new object type you will find the airconsole plugin in the "web" category conditions if you create a new event in an event sheet, then you will find the following condition options conditions reference name description on device custom state check triggered when a device custom state matches a value (e g when calling airconsole setcustomdevicestate({key value}) on a controller) on message triggered when a device sends a message ({message "your value"}) to the screen and the device id matches on specific message triggered when a specific message is received from a specific device on message from triggered when any message is received from a specific device on message key triggered when a device sends a message ({key "your value"}) and the key matches (use this if the sender device id does not matter e g for an action to start the game) on device join triggered when a device joins on device left triggered when a specific device disconnects on any device left triggered when any device disconnects on too many players triggered when max players is exceeded onpause triggered to pause the game pause should also pause any audio onresume triggered to resume the game again actions the following actions are available actions reference name description broadcast data sends a message to all connected devices send data sends a message to a specified device id game ready sends a message to all devices that the game is ready ( use this always on game start! ) set custom device state sets the screen device custom state (same as calling airconsole setcustomdevicestate({key value}) in the api) set active players takes all currently connected controllers and assigns them a player number can only be called by the screen the assigned player numbers always start with 0 and are consecutive you can hardcode player numbers, but not device ids once the screen has called setactiveplayers you can get the device id of the first player by calling convertplayernumbertodeviceid(0), the device id of the second player by using convertplayernumbertodeviceid(1) you can also convert device ids to player numbers by using convertdeviceidtoplayernumber(device id) you can get all device ids that are active players by using getactiveplayerdeviceids() show ad show ad on every connected controller and screen onadcomplete is called when showing ad is over expressions update december 2022 name description getlanguage gets the language iso code (e g 'en', 'es') gettranslation gets the translation for a translation key you can find more about expressions in this tutorial properties max players sets the maximum amount of players the game can handle (all other players who join after that should get a message like "game is full") use translations set to true if your game uses translations and you want to load them method examples look at some of the examples on how to use the methods controllerdeviceids mastercontrollerdeviceid activeplayerdeviceid storage connect screen with controllers for a usual html5 game we would normally just wait for the screen html to load and then handle the devices with construct2 we have to wait for the entire game to initialize, because we are "talking" with airconsole in the construct2 context construct2 game ready event asking "who is here?" simply call the airconsole gameready airconsole gameready action once as soon as the game has started this will broadcast a message to every already connected controller with the data { handshake true } { handshake true } code in controller html replying "me is here" the controller listens for the { handshake true } { handshake true } data in the onmessage onmessage method the controller will send a message back to the screen and trigger airconsole ondevicejoin airconsole ondevicejoin events in construct2, which you will also have to add ) controller messages to send a message from a controller to the screen you have to stick to certain keys in the json data you send, so that the construct2 plugin knows which condition you want to trigger key type description handshake handshake \<boolean> send this as soon as the game is loaded and to register the controller key key \<string> applied to the airconsole messagekey expression message message \<string> applied to the airconsole message expression for example \<script type="text/javascript" src=" https //www airconsole com/api/airconsole 1 9 0 js ">\</script> \<script type="text/javascript"> var air console = new airconsole(); // air console message(airconsole screen, { key "show main menu" }); air console message(airconsole screen, { message "move left" }); \</script> export your game export the game as a usual html5 game airconsole will request a /screen html /screen html , which is why you should rename the index html index html into screen html screen html and of course, don't forget to create a controller html controller html in the same directory, which includes the functionality for the controllers as soon as your game is ready make a zip file of the whole directory (screen html and controller html in the root) and publish it on airconsole example pong game walktrough to show you how all of this works together we will go through our example (very basic) pong game play example download game https //github com/airconsole/airconsole construct2/tree/master/example project event sheet trigger the gameready event as mentioned before, you always have to trigger a game ready event when the game has loaded, so that the controllers can tell us they are there store and assign device ids every controller owns a paddle sprite which means if controller 2 presses the up button, then we want the right paddle to move up that means we have to know which device belongs to which sprite we can achieve this by creating global vars for each device the game will support (in pong two global vars, because we will just have two players in our game) if the first controller connects "on device join", then we assign the joined airconsole device id to the global deviceid1 move the second devices which connects get assigned to deviceid2 move device ids are random the value of deviceid1 does not have to be always "1" the device with another device id (2, 3, 6, ) could join before the device id 1 device joins, set the device id with the condition airconsole ondevicejoin airconsole ondevicejoin we can listen for events when a new device joins if device joins, then assign the global variable device id to the value of airconsole deviceidjoin airconsole deviceidjoin the tricky part here is to have the order of devices which join in mind this means the first player who joins should be assigned to deviceid1 the second player should be the deviceid2 consequently we also have to check if the deviceid1 already has a value if yes, we check if the deviceid2 var has none and if that is true we assign the device id move paddles to move our paddles we create another two global vars called deviceid1 move deviceid1 move and deviceid2 move deviceid2 move each of them can have one of the 3 values 1 = move up 0 = stop 1 = move down send a message from a controller to the screen our controller will only consist of two buttons move up and move down if we press one of this buttons we will send a message to the game (screen) and the corresponding paddle sprite object should move or if we release a button it should stop our controller html controller html contains the following code move up move down now the game has to listen to a message which has the content "up", "down" or "stop" in our event sheet, you can see two events of "on receive message data" they were added by the airconsole onmessage airconsole onmessage event like this to sum it up if message == "up" and device id matches the value of deviceid1 then we set the variable deviceid1 move deviceid1 move to 1, which means our sprite moves down the same happens with message == 'stop' or message == 'down' external tutorials howto construct2 and airconsole construct 2 and airconsole little expression guide troubleshooting game won't work because of javascript errors if you get any javascript errors try to export your game without checking the "minify script" checkbox