• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Character stuck when using touch buttons, but does not get stuck with keyboards

wilmer

Member
Greetings to all:

Please help me on this, I am using skeletal animation. The problem is that the code that controls the gravity and collisions with the floor (the object of the floor is solid) works well with the keys assigned to move the character and jump (w, a, s, d, space etc.) , but since the code of my Touch stick has other variables, it makes my character jump to a vertical wall stuck, here I leave the code, if you see irregularities in the change of animation, that does not matter so much, I just want to know how to make When I use the touch stick my character falls and doesn't get stuck vertically.

Script of Touch Button:
Code:
/// scr_get_analog_stick_axis(obj,axis)
/// get a value between 1 and -1 for either the x or y axis.
var stick = argument0; // should be ID of obj_analog_stick instance
var axis = argument1; // should be 1 of 2 constants: AXIS_X or AXIS_Y
var value = 0;
// yields 0 - 360
var dir = point_direction(stick.guiX,stick.guiY,stick.stickX,stick.stickY);
// yields decimal from 0 - 1
var mag = point_distance(stick.guiX,stick.guiY,stick.stickX,stick.stickY)/(stick.radius*stick.image_xscale);
if (axis == axis.X) {
    value = lengthdir_x(mag,dir);
}
else {
    value = lengthdir_y(mag,dir);
}
return value;
Code for Player - Character:
Code:
Create event:
/// init
skeleton_animation_set ("still");
rate = 8;
Code:
Step event
/// axis controls

if (instance_exists (obj_analog_stick)) {

    // magnitude, ranging from -1 to 1
    var mag = scr_get_analog_stick_axis (obj_analog_stick, axis.X);
 
    // flip sprite
   if (mag <0) {
     // if (PSALV> 0) {
    image_xscale = -1;
    // rate = 8
    x + = mag * rate
     // image_yscale = 1;
     if skeleton_animation_get () == "still"
    {
    skeleton_animation_set ("running");
  }
   //}
   }
   else if (mag> 0) {
 // if (PSALV> 0) {
       image_xscale = 1;
    if skeleton_animation_get () == "still"
    {
    skeleton_animation_set ("running");
  }
 
  //}
    }
    // multiplication rate (8px) by percentage mag, then add to x
    if place_free (x, y) {
    x + = mag * rate; }
 
}
}
////// Code for Keyboard Keys - Step Event
Code:
var hmove = keyboard_check (ord ("D")) - keyboard_check (ord ("A"));
if (! climbing)
    var hvel = hmove * moving_speed;
else
    var hvel = hmove * climbing_speed;
repeat (abs (hvel))
{
    if (place_free (x + sign (hvel), y))
        x + = sign (hvel);
    else break;
}
if (! place_free (x, y))
{
   x - = sign (hvel);
}
if (! climbing)
{
    // sprite_index = sp_flaminio;
    if (hmove <> 0)
        image_xscale = hmove;
    else / * image_index = 0 * /;
 
   // image_speed = 0.3 * abs (hmove);
 
    repeat (abs (vertical_speed))
    {
        if (place_free (x, y + sign (vertical_speed)))
            y + = sign (vertical_speed);
        else
        {
            vertical_speed = 0;
            break;
        }
    }
    vertical_speed + = gravity_force;
 
    if (keyboard_check (vk_space) and! place_free (x, y + 1))
        vertical_speed = -jumping_speed;
For the Stick Object: Just in case
Code:
"Create Event"
enum axis {
    X,
    Y
};
globalvar StickPressed;
StickPressed = 4
//////////////////////////////////////
/// initialize stick
boundSprite = sprite_index; // should be spr_analog_boundary
stickSprite = spr_analog_stick;
radius = sprite_get_width (boundSprite) / 2;
alpha = 1;
snapRate = 24; // speed at which stick snapes back to origin
// as a note, image_xscale and image_yscale should be used to refer to the scale.
// the stick object will not move with the view, only sprite will.
guiX = x * display_get_gui_width () / view_wview; // multiply by user interface ratio accounts for any view scale problem
guiY = y * display_get_gui_height () / view_hview;
// using the mouse GUI coordinates is easier than compensating the room
guiMouseX = device_mouse_x_to_gui (0);
guiMouseY = device_mouse_y_to_gui (0);
stickX = guiX;
stickY = guiY;
// boolean
isPressed = false;
Step Event:
Code:
/// update analog stick
// This event occurs if I have NOT pressed the joystick range
if StickPressed = noone {
// Check if finger 0 is in the range of 200 pixels
if (device_mouse_check_button (0, mb_left)) && point_in_circle (device_mouse_x_to_gui (0), device_mouse_y_to_gui (0), guiX, guiY, 200) {
    StickPressed = 0; // I pressed the range with my finger 0
}
// Check if finger 1 is in the range of 200 pixels
if (device_mouse_check_button (1, mb_left)) && point_in_circle (device_mouse_x_to_gui (1), device_mouse_y_to_gui (1), guiX, guiY, 200) {
    StickPressed = 1; // I pressed the range with finger 1
}
// check if I pressed the range with either finger
if StickPressed! = noone {
    // update GUI mouse coords only if the mouse is pressed
    guiMouseX = device_mouse_x_to_gui (StickPressed);
    guiMouseY = device_mouse_y_to_gui (StickPressed);
    }
else {
// snap back to origin
    if (point_distance (stickX, stickY, guiX, guiY)> = snapRate * image_xscale) {
        var dir = point_direction (stickX, stickY, guiX, guiY);
        stickX + = lengthdir_x (snapRate * image_xscale, dir);
        stickY + = lengthdir_y (snapRate * image_xscale, dir);
    }
    else {// prevents overshooting
        stickX = guiX;
        stickY = guiY;
    }
}
}
// This event occurs IF I am pressing the joystick
else {
    // Detect if I let go of my finger, in that case disable the joystick
    if! device_mouse_check_button (StickPressed, mb_left) {
        StickPressed = noone
        exit
    }
    // Update the position of the Joystick, this allows the player to move even if the finger is dragged out of the detection range
    guiMouseX = device_mouse_x_to_gui (StickPressed);
    guiMouseY = device_mouse_y_to_gui (StickPressed);
// update stick coords
    if (point_distance (guiX, guiY, guiMouseX, guiMouseY) <= radius * image_xscale) {
        stickX = guiMouseX;
        stickY = guiMouseY;
    }
    else {// constriction adhere to limits
        var dir = point_direction (guiX, guiY, guiMouseX, guiMouseY);
        stickX = guiX + lengthdir_x (radius * image_xscale, dir);
        stickY = guiY + lengthdir_y (radius * image_xscale, dir);
    }
}
Draw GUI:
Code:
draw_sprite_ext(boundSprite,0,guiX,guiY,image_xscale,image_yscale,0,c_white,alpha);
// stick
draw_sprite_ext(stickSprite,0,stickX,stickY,image_xscale,image_yscale,0,c_white,alpha);
// debug
var axisx = scr_get_analog_stick_axis(self,axis.X);
var axisy = scr_get_analog_stick_axis(self,axis.Y);
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
Why do you duplicate the code manually when you can just have scripts for it? E.g. playermove_left and playermove_right. Put the working movement code there, then let both the touchbuttons and the actual buttons call that script. Problem solved.
 
Top