Legacy GM Movement Issues - Animation, Diagonals & grid snap

  • Thread starter Tofu Heavy Industries
  • Start date
T

Tofu Heavy Industries

Guest
1) Animation issue: Im using a 8 frame sprite with 2 frames per direction. When you initially move in a direction, the animation will overflow by a single frame before getting into the correct two-frame loop. How do I prevent the initial overflow?

Each Keyboard arrow key has this exact code (except for direction/frame #).
Code:
if place_free(x-8, y) {
            x -= 4;
            direction = 180;
            if (global.zone_fight == 1) battle_steps_traveled += 4;
            image_index +=0.4;
            if ( image_index >= 4 ) { image_index = 2; }
           
        }
2) Trying to prevent diagonal movement (with the same above code)

3) Snap to a 16 pixel grid if possible.

Right now, each move direction code is attached to their separate arrow key events. Theres a thread in this forum that has a nice example of using a single step event that prevents diagonals and also snaps to a grid, but I don't know how to add the animation to it.
 
1)

Code:
if  ( image_index < 2 || image_index >= 4 ) image_index = 2;
2)

You would have to add extra code in each key-pressed event to check that the other two perpendicular arrow keys were not pressed, the prevent the diagonal movement. At this point you would be writing so much repeated code that you might as well shift everything to the Step event to save yourself the effort.

3) You say you want to snap to a 16 pixel grid, but you are moving 4 pixels at a time. Snapping to 16 pixels would defeat the purpose....unless...you mean you want the instance to move by 4 pixels per step until it hits the 16 pixel grid and then stop, or something like that?
 
T

Tofu Heavy Industries

Guest
1)

Code:
if  ( image_index < 2 || image_index >= 4 ) image_index = 2;
2)

You would have to add extra code in each key-pressed event to check that the other two perpendicular arrow keys were not pressed, the prevent the diagonal movement. At this point you would be writing so much repeated code that you might as well shift everything to the Step event to save yourself the effort.

3) You say you want to snap to a 16 pixel grid, but you are moving 4 pixels at a time. Snapping to 16 pixels would defeat the purpose....unless...you mean you want the instance to move by 4 pixels per step until it hits the 16 pixel grid and then stop, or something like that?
1) thanks!

2) & 3) Heres the code I was referring to from the other post, I think that it auto handles suppressing diagonal movement by sticking to the grid. problem was when I injected a block of if/elses to check hsp & vsp to cycle the directional walking frames it just cycled the whole 8 frame sprite strip. Any idea how to add that animation code from above into it?

create
Code:
hsp = 0;
vsp = 0;
move_sp = 4;
step
Code:
var grid_size = 16; // Can also be a macro

if (x mod grid_size == 0) and (y mod grid_size == 0) // Check for grid alignment
{
   var move_x = keyboard_check(vk_right) - keyboard_check(vk_left), // Obtain the direction of the horizontal movement
       move_y = keyboard_check(vk_down) - keyboard_check(vk_up); // Same as above, but for vertical movement

   hsp = 0; // Reset speed values
   vsp = 0;

   if (move_x != 0) and (move_y != 0)
   {
       move_y = 0; // Quick and dirty check to prevent diagonal movement
   }
   
   hsp = move_x * move_sp; // Set the movement speeds based on movement direction
   vsp = move_y * move_sp;
}

x += hsp; // Move the instance if appllcable
y += vsp;
 
Top