Android [SOLVED] virtual_key_add positioning

I

innercitysumo

Guest
Hi,

Brand new here and at coding (please be gentle). I'm having some difficulties adding the virtual controls to my app in Android - the virtual key position doesn't seem to tally in any logical way to my object.

To get things tested I'm just trying to add a single key action (vk_left) to the large square at the bottom of the screen (as below). However, when run on my phone the action only works in the smaller square above and off to the left.



Room size = 4096 x 4096
View in room / Port on screen = 1024 x 768
The view is set to follow a ship object

The sprite for the virtual key is 206 x 205 (origin is centered 103, 102)

Code as follows:

Virtual key object
- Create event
Code:
global.key_left = virtual_key_add(x-(sprite_height/2),y-(sprite_height/2),sprite_width,sprite_height,vk_left);
- Draw event
Code:
draw_sprite_ext(sprite_index,image_index,view_xview[0]+143,view_yview[0]+614,image_xscale,image_yscale,image_angle,image_blend,image_alpha);
(The draw event I put in to stop it lagging when the view was moved, which my separate HUD object was doing until it was added)

The virtual key object is placed in my room at 40,512.

The phone I'm testing it on is a Nexus 5 (1920x1080 resolution).

Any assistance gratefully received, thanks.
 
Last edited by a moderator:
I

innercitysumo

Guest
I must have something fundamentally wrong here. I've tried to follow the tutorials as closely as possible, tried switching the view size to match my phone's resolution, removed/changed the Draw code, changed the XY references to the exact coordinates on the room view....

It might be something simple, but nothing I try has any effect on it.
 
I

innercitysumo

Guest
Thanks for the tip. It hasn't made a difference unfortunately.

However, I've now noticed that it seems OK when in portrait mode (something I'd previously switched off) - the inaccuracy only comes in when the phone is flipped over and used landscape, so I wonder if it is tied into resolution / aspect ratio.
 
P

ParodyKnaveBob

Guest
Just fwiw, have you tried drawing your button GUI in the Draw GUI Event? You'd be able to remove all those view_*[] array offsets that way. Might not solve the whole problem, but at least it should help simplify to reach the full solution sooner.

Regards,
Bob
 
I

innercitysumo

Guest
Thanks Bob, that's a huge help. I've added the draw event in Draw GUI as you suggested and the virtual key now aligns with my sprite, but for some reason both are halfway up the screen when in landscape (correct position in portrait). Progress though!
 
W

Wraithious

Guest
What I've found is you have to get the correct ratio between your display size and your game window size, yoyo's blog66 is very helpfull but I added a little to it to find the ratio and then use it in the virtual keys, this is how I did it,
In the first room's creation code:
Code:
var base_w = 960;//width of original game
var base_h = 540;//height of original game
var aspect = base_w / base_h ; // get the GAME aspect ratio
global.dw=display_get_width();
global.dh=display_get_height();//get the actual display size
if (global.dw < global.dh)
    {
    //portrait
    var ww = min(base_w, global.dw);
    var hh = ww / aspect;
    if(global.dw & 1)
    global.dw++;
    if(global.dh & 1)
    global.dh++;
    global.sx=ww/base_w;//Sets up scale ratio for width
    global.sy=hh/base_h;//Sets up scale ratio for height
    }
else
    {
    //landscape
    var hh = min(base_h, global.dh);
    var ww = hh * aspect;
    if(global.dw & 1)
    global.dw++;
    if(global.dh & 1)
    global.dh++;
    global.sx=ww/base_w;//Sets up scale ratio for width
    global.sy=hh/base_h;//Sets up scale ratio for height
    }
surface_resize(application_surface, ww, hh);//resize the game
display_set_gui_size(ww,hh);//resize the drawing canvas
in the object that displays your virtual keys creation event:
Code:
global.vikey1=virtual_key_add(x,y,31*global.sx,32*global.sy,vk_up);
global.vikey2=virtual_key_add(x+32*global.sx,y,31*global.sx,32*global.sy,vk_down);
global.vikey3=virtual_key_add(x+190*global.sx,y,32*global.sx,32*global.sy,vk_enter);
global.vikey4=virtual_key_add(x+223*global.sx,y,32*global.sx,32*global.sy,vk_end);
global.vikey5=virtual_key_add(x+65*global.sx,y,122*global.sx,32*global.sy,vk_space);
I made a small tutorial .gmz you can check out: https://drive.google.com/open?id=0B4uEFC9Ii8BnNXlSZzVtOUJTaWs
 
I

innercitysumo

Guest
Thanks, I will try to figure this out - I'm getting a 'global variable not set error' for the global.sx/sy variables, so I must have overlooked something. Probably need some sleep.
 
I

innercitysumo

Guest
The position of the virtual key and object in landscape was solved by adding this to the start of the Draw GUI event (after looking at the example code above I though 960x540 made more sense for a mobile game).

Code:
display_set_gui_size(960, 540);
Thanks for your help, guys - appreciate it.
 
W

Wraithious

Guest
Glad you got it figured out, scaling for mobile is imo one of the hardest things to get right, especially on android devices
 
I

innercitysumo

Guest
I guess we'll see how right it is when I try it on another device (or get the Android emulator working properly). For the moment I'm very happy that it's working on my phone.
 
W

Wraithious

Guest
It should, i tried it on 3 different android devices with widely varying screen sizes and api's and it worked fine
 
Top