Animation problems

X

xPaz

Guest
So, in my game, at the left side of the screen there is a character that the user controls. At the right side of the screen there is a monster that walking toward the character. When the monster hits the character his hp turns to 0 and he dies. The player can attack the monster with 'X' and if the distance between them is <=5 he kills it.
now for the problem
1. Whenever i press X the attack animation ( sprite7 ) is played. problem is that if i release X halfway through the animation it stops. I want sprite7 to run a single loop every time I press X without considering if I'm holding the key or not ( holding will just cause another loop ).
2. I did not load the attacking sprite to the left but after I attack I immediately return to sprite 1 ( standing faced right ). How can I know if the attacking was made when the character looked to the left or right ?

Player Code :
Code:
// this part is about moving
if(keyboard_check(vk_right)) {x+=2;sprite_index = sprite2;image_speed =0.3;}
if(keyboard_check_released(vk_right)) sprite_index = sprite1;
if(keyboard_check(vk_left)) {x-=2;sprite_index = sprite4;image_speed =0.3;}
if(keyboard_check_released(vk_left)) sprite_index = sprite5;


if(hp=0) instance_destroy(); // Death

if(keyboard_check_pressed(ord('X'))) // Attack
{
    sprite_index = sprite7;image_speed = 0.3;
    if(distance_to_object(monster) <=5)
        monster.hp =0;
}
if(keyboard_check_released(ord('X'))) sprite_index = sprite1;
I hope you guys can help me, I really want to advance forward but I have no idea how to fix this since the lack of experience.
 
M

Multimagyar

Guest
sounds simple enough. you would first need to check if the sprite is sprite7 or not when you press x or not if it is not then you set it to sprite7 set the image speed and the image index to 0 so it starts from the beginning. You add an end of animation event where you force the sprite back to the respective idle animation.

For the direction checking since you use two sprites for two directions you can either set a variable to tell the program that you are facing left or not, or check if the sprite is one of the left facing sprites. So what I would do is:
in create event:
Code:
    left=0;   //we start by facing right
step event:
Code:
// this part is about moving
if (sprite_index!=sprite7)
{
   if(keyboard_check(vk_right)) {x+=2;sprite_index = sprite2;image_speed =0.3; left=0;}
   if(keyboard_check_released(vk_right)) sprite_index = sprite1;
   if(keyboard_check(vk_left)) {x-=2;sprite_index = sprite4;image_speed =0.3; left=1;}
   if(keyboard_check_released(vk_left)) sprite_index = sprite5;



  if(keyboard_check_pressed(ord('X'))) // Attack
  {
      sprite_index = sprite7;image_speed = 0.3;
      image_index=0;
      if(distance_to_object(monster) <=5)
        monster.hp =0;
  }
}

if(hp=0) instance_destroy(); // Death
In end of animation event:

Code:
if (left)
{
  if (sprite_index==sprite7)
  {
     sprite_index=sprite5;
     image_index=0;
     image_speed=0.3;
  }
}
else
{
  if (sprite_index==sprite7)
  {
     sprite_index=sprite1;
     image_index=0;
     image_speed=0.3;
  }
}
Something like this. (would personally do it a lot differently but different case)
 
X

xPaz

Guest
sounds simple enough. you would first need to check if the sprite is sprite7 or not when you press x or not if it is not then you set it to sprite7 set the image speed and the image index to 0 so it starts from the beginning. You add an end of animation event where you force the sprite back to the respective idle animation.

For the direction checking since you use two sprites for two directions you can either set a variable to tell the program that you are facing left or not, or check if the sprite is one of the left facing sprites. So what I would do is:
in create event:
Code:
    left=0;   //we start by facing right
step event:
Code:
// this part is about moving
if (sprite_index!=sprite7)
{
   if(keyboard_check(vk_right)) {x+=2;sprite_index = sprite2;image_speed =0.3; left=0;}
   if(keyboard_check_released(vk_right)) sprite_index = sprite1;
   if(keyboard_check(vk_left)) {x-=2;sprite_index = sprite4;image_speed =0.3; left=1;}
   if(keyboard_check_released(vk_left)) sprite_index = sprite5;



  if(keyboard_check_pressed(ord('X'))) // Attack
  {
      sprite_index = sprite7;image_speed = 0.3;
      image_index=0;
      if(distance_to_object(monster) <=5)
        monster.hp =0;
  }
}

if(hp=0) instance_destroy(); // Death
In end of animation event:

Code:
if (left)
{
  if (sprite_index==sprite7)
  {
     sprite_index=sprite5;
     image_index=0;
     image_speed=0.3;
  }
}
else
{
  if (sprite_index==sprite7)
  {
     sprite_index=sprite1;
     image_index=0;
     image_speed=0.3;
  }
}
Something like this. (would personally do it a lot differently but different case)
I understood your solution.
However, id like to ask how would you do it ? I'm aware that I'm not using GM correctly :p
 
M

Multimagyar

Guest
Well I would only use only one sprite for each side and then flip it with image_xscale to the opposite side, instead of checking depending on distance I would use collision boxes to check if something in front of the character so I can extend on it, This solution what I presented is glitchy because if I hold then release left for atleast a split second would show the wrong side so I would put the left into an else compared to the right key checking,

like this:
Code:
   if(keyboard_check(vk_right)) {x+=2;sprite_index = sprite2;image_speed =0.3; left=0;}
   else
   {
      if(keyboard_check(vk_left)) {x-=2;sprite_index = sprite4;image_speed =0.3; left=1;}
      if(keyboard_check_released(vk_left)) sprite_index = sprite5;
   }
   if(keyboard_check_released(vk_right)) sprite_index = sprite1;
[\CODE]

and I would only check the press for setting these things so I also would be able to set the image_index to 0 which would make a different if just to check if it is being helt down to move left or right.

in this case I would not even need the left variable because image_xscale will handle that for me, as well as I would save resources because everything just being flipped to the opposing side. 

I would need to think more about it to say what else would I do.
 
X

xPaz

Guest
Well I would only use only one sprite for each side and then flip it with image_xscale to the opposite side, instead of checking depending on distance I would use collision boxes to check if something in front of the character so I can extend on it, This solution what I presented is glitchy because if I hold then release left for atleast a split second would show the wrong side so I would put the left into an else compared to the right key checking,

like this:
Code:
   if(keyboard_check(vk_right)) {x+=2;sprite_index = sprite2;image_speed =0.3; left=0;}
   else
   {
      if(keyboard_check(vk_left)) {x-=2;sprite_index = sprite4;image_speed =0.3; left=1;}
      if(keyboard_check_released(vk_left)) sprite_index = sprite5;
   }
   if(keyboard_check_released(vk_right)) sprite_index = sprite1;
[\CODE]

and I would only check the press for setting these things so I also would be able to set the image_index to 0 which would make a different if just to check if it is being helt down to move left or right.

in this case I would not even need the left variable because image_xscale will handle that for me, as well as I would save resources because everything just being flipped to the opposing side.

I would need to think more about it to say what else would I do.
Thanks !
 
Top