• 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 snap to grid moving issue

woods

Member
so i have a weird thing going on.. my player moves just fine.. but when changing sprite to a werewolf, if i move more than 2 tiles.. he gets stuck moving in that direction. the only thing was a sprite change....


another thing i tried was copy the player and make a new object.. change the sprite to the wolfman... same issue.. when going left or right, the werewolf gets stuck moving in that direction...

sprite is 32x32 pxls
collision mask is full image


this one has me confused....


create event werewolf
Code:
/// movement

xspd = 6; // Move speed along x axis, must be less than gridx
yspd = 6; // Move speed along y axis, must be less than gridy
gridx = 32; // Grid cell x size, I recommend your player sprite width
gridy = 32; // Grid cell y size, I recommend your player sprite height
dir = 0; // Direction, must be between 0 and 3. 0=Right, 1=Down, 2=Left, 3=Up
key_right = ord("D"); // Key to move right. D is ord("D") vk_right is arrow
key_down = ord("S"); // Key to move right. S is ord("S")
key_left = ord("A"); // Key to move right. A is ord("A")
key_up = ord("W"); // Key to move right. W is ord("W")


// Do not change
xspd = gridx / (gridx div xspd); // Makes xspd work
yspd = gridy / (gridy div yspd); // Makes yspd work
step event werewolf
Code:
/// snap to grid and movement



if !place_snapped(gridx,gridy)
{ // If you aren't snapped to the grid..
    switch dir{ // Check the direction
        case 0: // If it's 0 (right)
            x += xspd; // Move right by the x speed
            break; // Stop checking if it's right
        case 1: // If it's 1 (down)
            y += yspd; // Move down by the y speed
            break; // Stop checking if it's down
        case 2: // If it's 2 (left)
            x -= xspd; // Move left by x speed
            break; // Stop checking if it's left
        case 3: // If it's 3 (up)
            y -= yspd; // Move up by the y speed
            break; // Stop checking if it's up
    }
}
else
{ // If you are snapped to the grid..
        if keyboard_check(key_right) && !place_meeting(x+32,y,obj_block) && !keyboard_check(vk_shift)
    {
        dir = 0; sprite_index = spr_werewolf_right; // Set the direction to 0 (right)
        x += xspd; // Move right by the x speed
    }
else
        if keyboard_check(key_down) && !place_meeting(x,y+32,obj_block)  && !keyboard_check(vk_shift)
    {
        dir = 1; sprite_index = spr_werewolf_down; // Set the direction to 1 (down)
        y += yspd; // Move down by the y speed
    }
else
        if keyboard_check(key_left) && !place_meeting(x-32,y,obj_block)  && !keyboard_check(vk_shift)
    {
        dir = 2; sprite_index = spr_werewolf_left;// Set the direction to 2 (left)
        x -= xspd; // Move right by the x speed
    }
else
        if keyboard_check(key_up) && !place_meeting(x,y-32,obj_block)  && !keyboard_check(vk_shift)
    {
        dir = 3; sprite_index = spr_werewolf_up; // Set the direction to 3 (up)
        y -= yspd; // Move up by the y speed    
    }
}


//stop animation if not moving
if x == xprevious && y == yprevious
    {
    image_speed = 0;
    image_index = 0;
    }
else
    {
    image_speed = 0.3
    }

// facing player with shift+WASD

if keyboard_check(key_right) && keyboard_check(vk_shift)
    {
    dir = 0; sprite_index = spr_werewolf_right;
    }
else
if keyboard_check(key_down) && keyboard_check(vk_shift)
    {
    dir = 1; sprite_index = spr_werewolf_down;
    }
else
if keyboard_check(key_left) && keyboard_check(vk_shift)
    {
    dir = 2; sprite_index = spr_werewolf_left;
    }
else
if keyboard_check(key_up) && keyboard_check(vk_shift)
    {
    dir = 3; sprite_index = spr_werewolf_up;
    }

edit...

thought i fixed it, but nope ;o)
 
Last edited:
N

NeonBits

Guest
xspd = 6; // Move speed along x axis, must be less than gridx
gridx = 32; // Grid cell x size, I recommend your player sprite width
// Do not change
xspd = gridx / (gridx div xspd); // Makes xspd work
I can't concentrate. You have "xspd" above and then "xspd" under; could it be possible that its value (6) is removed. Maybe you should change the "xspd" under for "spdx" and edit the step event with it? But I don't understand why you need to divide "gridx"; wouldn't it be simpler to just go with "xspd = 6;"? You also have ";" missing on image_speed.
And this "keyboard_check(vk_shift)"; if you're not snapped to the grid, you check for "dir", but if "shift" is not pressed, you have only "0" (right direction) as a value or its last one. If you're snapped to the grid, you can move in a direction only if you do not hold "shift"; your code at the bottom has no "x +=" / "x -=" and no "y +=" / "y -=".
edit: I've never used "place_snapped" before.
 
Last edited by a moderator:

woods

Member
honestly, the grid snap code was fed to me... it makes sense that the first xspd would be overwritten... but he moves faster when changing the 4 to the (current) 6 ...so its not beinf overwrote(dont think thats a real word ;o) )
but it seems to work ;o)

";" gamemaker is pretty lax on these... small typo ;o)

vk_shift // hold shift to turn the player facing without moving .. ie i want to face north but not walk

====
weird thing is.. it works with spd 4 no problem.... and the sprite for the player works just fine too


ill monkey with the multiple xspd issue for a bit and see what happens
 
N

NeonBits

Guest
Why do you need the "snapped to grid"? If it's not necessary, remove it and work with simple "keyboard_check". Could it be possible that the object gets stuck in between two situations? The code feels strange a bit; in one situation, it uses "switch" while the others follow a different option. With "shift" you still move if you're not snapped to the grid.
Code:
 xspd = gridx / (gridx div xspd); // Makes xspd work
moves faster when changing the 4 to the (current) 6
The four? I don't understand.
Code:
 xspd = 32 / (5.33) -> 6
If I am correct, "xspd" returns "6". if "gridx" and "xspd" never change, then removing the code above to keep the first "xspd" in "create event" isn't supposed to make a difference.
 
Last edited by a moderator:

woods

Member
the 4 comes from the original code that i found .. stuck that on the player... when i made the wolf, i wanted him to move a bit faster.. so i changed it to 6..
i know the whole thing is way over complicated.... but it was basically a bunch of different bits of code that i found thru google trying to make a grid based movement system.

i think my best option right now is..
since ive relearned some basic skills... i want to basically redo the whole thing so it reads a hellofa lot simpler ;o)



end result is basically i want a smooth motion of my characters snapping onto a 32x32 grid
 
Top