Instead of simply moving our sprite slowly across the X-axis, lets instead change it so we can move up and down using a gamepad, keyboard or onscreen controller. Thankfully with AppGameKit, this is really simple, as we can use a single virtual joystick interface that uses a real joystick if it exists, an on-screen joystick if needed or default WASD keyboard controls if on a PC without a joystick attached.
Now enter the following code:
#include "MainScene.scene" //Properties verticalSpeed as float = 5.0 MainScene_setup() SetJoystickScreenPosition ( 100, GetVirtualHeight() - 100, 90 ) if(GetVirtualJoystickExists(1)) SetVirtualJoystickVisible(1,1) SetButtonScreenPosition(1,GetVirtualWidth() - 50, 100, 80) SetVirtualButtonSize(1,100,50) SetVirtualButtonText(1,"Fire") endif do MainScene_sync() Print(ScreenFPS()) Sync() //Virtual Joystick handling yAxisMovement# = GetJoystickY() if( abs(yAxisMovement#) > 0.1) spriteX# = GetSpriteX(MainScene_Player) spriteY# = GetSpriteY(MainScene_Player) SetSpritePosition(MainScene_Player,spriteX#, spriteY# + (verticalSpeed) * yAxisMovement#) endif if(GetButtonPressed(1)) print("BOOM") endif loop
This code will display a virtual joystick on a mobile device or using an actual joystick. You can also use the WASD keys for up and down, as well as the spacebar to shoot. Note, if you don’t want default settings, you can handle each task manually such as handling raw input, raw joystick support, touch, etc.
Speaking of mobile support, now might be a good time to demonstrate one of the coolest features of AGK:S, the ability to broadcast to an Android or iOS device. To do so, you need to install the client from either the App Store or Google Play Store. Once installed, run the application on your device of choice.
You can now broadcast your application to your device using the broadcast button on the toolbar.