Android [80%] Doubt with virtual_key

Hello everybody!
I'm having trouble creating a virtual_key system with the keyboard keys on GMS2. I'm following the tutorials all and doing it the right way but it does not work on android.

CREATE
Code:
var vx=camera_get_view_width(view_camera[0]);
var vy=camera_get_view_height(view_camera[0]);
display_set_gui_size(vx,vy);
DRAW GUI
Code:
if instance_exists(ob_bleft) { global.vb_left= virtual_key_add(30, 400, 70, 70, vk_left) }
if instance_exists(ob_bright) { global.vb_right= virtual_key_add(60, 400, 70, 70, vk_right) }
if instance_exists(ob_batack) { global.vb_atack= virtual_key_add(200, 400, 70, 70, ord("C")) }
if instance_exists(ob_bjump) { global.vb_jump= virtual_key_add(300, 400, 70, 70, ord("X")) }
 

chorrus

Member
Hi!

You only need to set up virtual keys once, so use virtual_key_add function in the create event. To check where they are being drawn which is useful when setting them up to make sure everything is correct use virtual_key_show
And even though in the official docs they are using a global variable for this I wouldn't a simple variable will work too.

Let me know if you have any problems, I'm sure I'll be able to help :D
 
Hi!

You only need to set up virtual keys once, so use virtual_key_add function in the create event. To check where they are being drawn which is useful when setting them up to make sure everything is correct use virtual_key_show
And even though in the official docs they are using a global variable for this I wouldn't a simple variable will work too.

Let me know if you have any problems, I'm sure I'll be able to help :D
So, it actually got much better and lighter following your tips, I took the global var and put it in create_event. But in androind does not work. On the PC is working perfectly porem on Android does not work.:(
 
Last edited:

chorrus

Member
That's good you are almost there!

Alright the problem is probably because the virtual key isn't being placed where they should. I'll show you what I'm using. Try this:

Code:
//Here I put the position where I want the button to be(xx and yy are there because I let the players modify the position of the on screen buttons)
x = 56 + xx
y = camera_get_view_height(view_camera[0]) - 46 + yy

// The position in the gui layer is usually different, so you need to use this
guiX = x * display_get_gui_width() / camera_get_view_width(view_camera[0])
guiY = y * display_get_gui_height() / camera_get_view_height(view_camera[0])

//Now you can draw it
kright = virtual_key_add(guiX , guiY - 40, 40, 80, vk_right);
With that the results should be the same in your PC and mobile device.
 
Top