Trouble with attack animation (AGAIN!)

NimNom1234

Member
Okay
So another member of this forum helped me with my problem a day or two ago. I feel his instructions were helpful but i did something wrong. Im trying to get my character to play through his attack animation once at the press of the space bar. However, every time i press the space bar, he just jitters and plays the first frame of the animation. This is all the code that has to do with that.

if keyboard_check_pressed(vk_right){
direction=1
}
if keyboard_check_pressed(vk_left){
direction=2
}
key_right = keyboard_check(vk_right)
key_left = keyboard_check(vk_left)
key_jump = keyboard_check_pressed(vk_up)
key_crouch = keyboard_check(vk_down)
attacking = keyboard_check_pressed(vk_space)

if attacking = false {
if key_right = true {
x += 4
sprite_index = spr_char_walk_right
}
if key_left = true{
x -= 4
sprite_index = spr_char_walk_left
}}
if attacking = true {
alarm[0] = 24
sprite_index = spr_char_attack_right
image_speed = .5

}

if attacking = false {
if direction = 0 && (place_meeting(x,y+1,obj_wall)) && key_right != true && key_left != true {
sprite_index = spr_char_idle_right
image_speed = .3
}

if direction = 1 && (place_meeting(x,y+1,obj_wall)) && key_right != true {
sprite_index = spr_char_idle_right
image_speed = .3
}

if direction = 2 && (place_meeting(x,y+1,obj_wall)) && key_left != true {
sprite_index = spr_char_idle_left
image_speed = .3
}
}
else {
sprite_index = spr_char_attack_right
}

//And in the alarm 0 event:
attacking = false





Can someone explain what im doing wrong? Thanks!
 
Last edited:

Blaize

Member
I think I see it.

add this line:
Code:
 image_index=0;
just before the sprite_index in your attack check.
 

Danei

Member
Where is the first part of your code located? If it's in a step event, then you are setting 'attacking' to false every frame except for the one frame you pressed space on.

Code:
 attacking = keyboard_check_pressed(vk_space)

Probably what you want to do instead is something like
Code:
 if keyboard_check_pressed(vk_space){ attacking = true;}
That way attacking should remain true until turned off by the alarm.

Oh wait, but also, you're setting the alarm to 24 every frame that attacking is true too, if indeed that's in the step event. Once attacking is true the alarm will repeatedly be set to 24 and never finish counting down. So maybe what you want to do is more like

Code:
 if keyboard_check_pressed(vk_space){ attacking = true;
alarm[0] = 24;
sprite_index = spr_char_attack_right;
image_speed = .5;
}
 
Last edited:
i have a couple of ideas for you:

first, "direction" is a built-in GML function, and it may give you grief to use it as a variable.

second, i would ditch the alarm, and use the Animation End event, in conjunction with your "attacking" switch, to switch back to your idle animation, after the attack completes.
 

NimNom1234

Member
Where is the first part of your code located? If it's in a step event, then you are setting 'attacking' to false every frame except for the one frame you pressed space on.

Code:
 attacking = keyboard_check_pressed(vk_space)

Probably what you want to do instead is something like
Code:
 if keyboard_check_pressed(vk_space){ attacking = true;}
That way attacking should remain true until turned off by the alarm.

Oh wait, but also, you're setting the alarm to 24 every frame that attacking is true too, if indeed that's in the step event. Once attacking is true the alarm will repeatedly be set to 24 and never finish counting down. So maybe what you want to do is more like

Code:
 if keyboard_check_pressed(vk_space){ attacking = true;
alarm[0] = 24;
sprite_index = spr_char_attack_right;
image_speed = .5;
}
Thank you!
 

NimNom1234

Member
Where is the first part of your code located? If it's in a step event, then you are setting 'attacking' to false every frame except for the one frame you pressed space on.

Code:
 attacking = keyboard_check_pressed(vk_space)

Probably what you want to do instead is something like
Code:
 if keyboard_check_pressed(vk_space){ attacking = true;}
That way attacking should remain true until turned off by the alarm.

Oh wait, but also, you're setting the alarm to 24 every frame that attacking is true too, if indeed that's in the step event. Once attacking is true the alarm will repeatedly be set to 24 and never finish counting down. So maybe what you want to do is more like

Code:
 if keyboard_check_pressed(vk_space){ attacking = true;
alarm[0] = 24;
sprite_index = spr_char_attack_right;
image_speed = .5;
}
Actually, I've run into another problem. Its probably easily fixed, but my walk cycle facing right has fewer frames now. This is my full code:

Code:
if keyboard_check_pressed(vk_right){
direction=1
}
if keyboard_check_pressed(vk_left){
direction=2
}
key_right = keyboard_check(vk_right)
key_left = keyboard_check(vk_left)
key_jump = keyboard_check_pressed(vk_up)
key_crouch = keyboard_check(vk_down)
if keyboard_check_pressed(vk_space) && direction = 1 || 0{attacking = true
alarm[0] = 24;
sprite_index = spr_char_attack_right
image_speed = .5
}
if keyboard_check_pressed(vk_space) && direction = 2 {attacking = true
alarm[0] = 24;
sprite_index = spr_char_attack_left
image_speed = .5
}
if attacking = false {
if key_right = true {
x += 4
sprite_index = 0
sprite_index = spr_char_walk_right
}
if key_left = true{
x -= 4
sprite_index = spr_char_walk_left
}}
if (vsp < 10) {vsp += grav}
if (place_meeting(x,y+1,obj_wall)){
vsp = key_jump * -jumpspeed
}
if (place_meeting(x+hsp,y,obj_wall)){
while (!place_meeting(x+sign(hsp),y,obj_wall)){
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
if (place_meeting(x,y+vsp,obj_wall)){
while (!place_meeting(x,y+sign(vsp),obj_wall)){
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
if attacking = false {
if direction = 0 && (place_meeting(x,y+1,obj_wall)) && key_right != true && key_left != true {
sprite_index = spr_char_idle_right
image_speed = .3
}
if direction = 1 && (place_meeting(x,y+1,obj_wall)) && key_right != true {
sprite_index = spr_char_idle_right
image_speed = .3
}
if direction = 2 && (place_meeting(x,y+1,obj_wall)) && key_left != true {
sprite_index = spr_char_idle_left
image_speed = .3
}
}
if (!place_meeting(x,y+1,obj_wall)) && direction = 1 {
sprite_index = spr_char_air_right
}
if (!place_meeting(x,y+1,obj_wall)) && direction = 2 {
sprite_index = spr_char_air_left
}
if (!place_meeting(x,y+1,obj_wall)) && direction = 0 {
sprite_index = spr_char_air_right
}
Theres also some less important stuff outside of the step event
 

PlayerOne

Member
Actually, I've run into another problem. Its probably easily fixed, but my walk cycle facing right has fewer frames now. This is my full code:

Code:
if keyboard_check_pressed(vk_right){
direction=1
}
if keyboard_check_pressed(vk_left){
direction=2
}
key_right = keyboard_check(vk_right)
key_left = keyboard_check(vk_left)
key_jump = keyboard_check_pressed(vk_up)
key_crouch = keyboard_check(vk_down)
if keyboard_check_pressed(vk_space) && direction = 1 || 0{attacking = true
alarm[0] = 24;
sprite_index = spr_char_attack_right
image_speed = .5
}
if keyboard_check_pressed(vk_space) && direction = 2 {attacking = true
alarm[0] = 24;
sprite_index = spr_char_attack_left
image_speed = .5
}
if attacking = false {
if key_right = true {
x += 4
sprite_index = 0
sprite_index = spr_char_walk_right
}
if key_left = true{
x -= 4
sprite_index = spr_char_walk_left
}}
if (vsp < 10) {vsp += grav}
if (place_meeting(x,y+1,obj_wall)){
vsp = key_jump * -jumpspeed
}
if (place_meeting(x+hsp,y,obj_wall)){
while (!place_meeting(x+sign(hsp),y,obj_wall)){
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
if (place_meeting(x,y+vsp,obj_wall)){
while (!place_meeting(x,y+sign(vsp),obj_wall)){
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
if attacking = false {
if direction = 0 && (place_meeting(x,y+1,obj_wall)) && key_right != true && key_left != true {
sprite_index = spr_char_idle_right
image_speed = .3
}
if direction = 1 && (place_meeting(x,y+1,obj_wall)) && key_right != true {
sprite_index = spr_char_idle_right
image_speed = .3
}
if direction = 2 && (place_meeting(x,y+1,obj_wall)) && key_left != true {
sprite_index = spr_char_idle_left
image_speed = .3
}
}
if (!place_meeting(x,y+1,obj_wall)) && direction = 1 {
sprite_index = spr_char_air_right
}
if (!place_meeting(x,y+1,obj_wall)) && direction = 2 {
sprite_index = spr_char_air_left
}
if (!place_meeting(x,y+1,obj_wall)) && direction = 0 {
sprite_index = spr_char_air_right
}
Theres also some less important stuff outside of the step event


From what I can see your code has a sprite_index that equals zero. You are declaring a non-existent sprite with a value rather than an actual image/sprite.

Code:
if key_right = true {
x += 4
sprite_index = 0 // <<< Remove this line
sprite_index = spr_char_walk_right
}
I see you also declared your image speed as .5 when you press the attack key, but did not reset the image speed back to normal when not attacking. In this instance use this code:

Code:
if !attacking=true{image_speed=1}
else{image_speed=0.5}
Just remove the image_speed arguments in your code before copying and pasting this into the step event.
 

NimNom1234

Member
From what I can see your code has a sprite_index that equals zero. You are declaring a non-existent sprite with a value rather than an actual image/sprite.

Code:
if key_right = true {
x += 4
sprite_index = 0 // <<< Remove this line
sprite_index = spr_char_walk_right
}
I see you also declared your image speed as .5 when you press the attack key, but did not reset the image speed back to normal when not attacking. In this instance use this code:

Code:
if !attacking=true{image_speed=1}
else{image_speed=0.5}
Just remove the image_speed arguments in your code before copying and pasting this into the step event.
Thank you, sir!
 
Top