• 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!

How to keep state until animation end?

M

Midastouch

Guest
Hello everyone, i need some help for my little project
I try to put a dash animation, when i click on the space key i want my player doing a little dash forward.

My problem is that the sprite change only when the key space is pressed.
And this dash look more than a teleport than a dash.

Code:
var input_horizontal = keyboard_check(key_right) - keyboard_check(key_left);
var input_vertical = keyboard_check(key_down);
var input_jump = keyboard_check_pressed(key_jump);
var input_down = keyboard_check_pressed(key_down);

if input_horizontal = 1 {image_xscale = -1}
if input_horizontal = -1 {image_xscale = 1}

if input_horizontal = 0 {state = "idle"}
if input_horizontal != 0 {state = "move"}
if keyboard_check_pressed(vk_space){state = "dash"}


if state == "idle" {sprite_index = spr_Player_idle;} 
if state == "move" {sprite_index = spr_Player_move;
max_horizontal_speed = 8;}     
    
if state == "dash"

{max_horizontal_speed = 2
sprite_index = spr_player_dash;
image_speed = 0.5;
if image_xscale == 1 and not place_meeting (x-40,y,obj_Wall)
{
  
    x-=40;
}

if image_xscale == -1 and not place_meeting (x+40,y,obj_Wall)
{
    x+=40;
}
}
else {state = "move"}
 
You can simply have an Animation End event and put this line of code in it:

Code:
if (state == "dash") {
    state = "idle";
}
Also make sure each time you switch to the dash state, to set the image_index to zero so that the animation start from the beginning:

Code:
if keyboard_check_pressed(vk_space) {
     image_index = 0;
     sprite_index = WHATEVER SPRITE YOU WANT FOR DASH;
     state = "dash";
}
 
Last edited:

TheouAegis

Member
Well for starters...

Code:
if input_horizontal != 0
{
   image_xscale = -input_horizontal;
   if keyboard_check_pressed(vk_space) && state != "dash"
   {
      sprite_index = spr_Player_dash;
      image_index = 0;
      image_speed = 0.5;
   }
   else
   {
      state = "move";
      sprite_index = spr_Player_move;
   }
}
else
if state != dash
{
   state = "idle";
   sprite_index = spr_Player_idle;
}
What's this "max_horizontal_speed" variable in your code? What's the point of it? And why is it LOWER in your dash state than in your move state?
 
M

Midastouch

Guest
I use max_horizontal_speed to fix the maximum value of my speed, the maximun speed is different when i dash and when i move
 
M

Midastouch

Guest
I tried both of your ideas without success.
Actually the sprite_player_dash is only displayed when i press "space" (i see only 1 frame of it).


This is my actual code :

Code:
var input_horizontal = keyboard_check(key_right) - keyboard_check(key_left); 
var input_vertical = keyboard_check(key_down);
var input_jump = keyboard_check_pressed(key_jump);
var input_down = keyboard_check_pressed(key_down);

//if input_horizontal = 1 {image_xscale = -1}
//if input_horizontal = -1 {image_xscale = 1}
if input_horizontal = 0 {state = "idle"}


if input_horizontal != 0
{
   image_xscale = -input_horizontal;
   if keyboard_check_pressed(vk_space) && state != "dash"
   {
      state = "dash";
      sprite_index = spr_player_dash;
      image_index = 0;
      image_speed = 0.5;
      if image_xscale == 1 and not place_meeting (x-30,y,spr_player_idle)
      {max_horizontal_speed = -50
          horizontal_speed = -50;}
      if image_xscale == -1 and not place_meeting (x+30,y,spr_player_idle)
      {
          max_horizontal_speed = 50
          horizontal_speed = 50;}
   }
   else
   {
      state = "move";
      max_horizontal_speed = 8;
      sprite_index = spr_Player;
   }
}
else
if state != "dash"
{
   state = "idle";
   sprite_index = spr_player_idle;
}
 

TheouAegis

Member
Oh i see an error i made. Prob what is causing your issue now.

Code:
if input_horizontal != 0
{
   image_xscale = -input_horizontal;
   if keyboard_check_pressed(vk_space)
   {
      if state != "dash"
      {
        state = "dash";
        sprite_index = spr_player_dash;
        image_index = 0;
        image_speed = 0.5;
        if image_xscale == 1 and not place_meeting (x-30,y,spr_player_idle) //30 or 50?
        {
            max_horizontal_speed = -50
            horizontal_speed = -50b
         }
         else
        if image_xscale == -1 and not place_meeting (x+30,y,spr_player_idle)
        {
            max_horizontal_speed = 50
            horizontal_speed = 50;
         }
      }
      else
      {
        state = "move";
        max_horizontal_speed = 8;
       sprite_index = spr_Player;
      }
   }
}
else
if state != "dash"
{
   state = "idle";
   sprite_index = spr_player_idle;
}
I can't count brackets very well on my phone, so not sure if I got them all in.
 
Top