Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

(this is game maker)

For some reason when i press x only the first frame of my attack animation plays, then the rest of the animation only plays if i move horizontally. I believe my move variable is putting out either -1, 0, or 1 depending on weather or not im hitting left or right or both. But if any of the code above my attacking code affecting the attacking code??

I also tried removing

&& sprite_index != spr_player_swing

but the problem persisted (i thnik this is a check to make sure the animation doesnt try to play over itsself)

(1 edit)

I'm not familiar with GM, so forgive me if this is completely off the mark: let me make two assumptions about this code:

1. This snippet is run on every frame (i.e. in the game loop)
2. GameMaker reacts immediately to sprite_index being changed (by immediately changing the sprite)

With that in mind, I suspect your move code is affecting your attack code. Each time this script runs (presumably on every frame), sprite_index is reassigned to one of your other sprites in the move code:

if place_meeting(x,y+1,obj_wall) {
    ...
    sprite_index = spr_run_player
    ...
    else sprite_index = spr_test_player
} else {
    ... sprite_index = spr_player_jump; else sprite_index = spr_player_fall ...
}

So in the frame after you first pressed X, sprite_index will begin as spr_player_swing, but there is no scenario where sprite_index emerges from your move code as spr_player_swing (i.e. it's always changed). I assume GM reacts to this by changing the sprite and reseting spr_player_swing's image_index (what frame of the animation its on).

Then, in your attack code, you set sprite_index to spr_player_swing (if X is pressed), but the image_index has been reset! This causes only the first frame of its animation to ever be visible.

To fix this, try restructuring this code into one if/elseif/else tree. This is just a rearrangement, so no other edits are needed (although, judging from other people's code I've been perusing, you don't need sprite_index != spr_player_swing. I suspect GM doesn't react if you set sprite_index to whatever sprite it's already set to).

if place_meeting(x,y+1,obj_wall) {
    if (move!=0)
    ...
}</span> else if keyboard_check(ord('X')) { 
    sprite_index = spr_player_swing
    ...
} else {
    if (vsp < 0) sprite_index = spr_player_jump; else sprite_index = spr_player_fall ...
}

A lot of hunches at play here. Let me know if that helps!

I tried restructuring the code like you said, and even trying to put the attack animation in a loop (i probably didnt do that correctly though), neither worked for some reason, I finally found a fix for it though. Ill put it here in case someone else has a similar issue.

i had to make a variable in a create event and set it to false (attacking = false)


then make an animation end event and add this


it isnt working perfectly, i think other sprites might be happening during the animation depending on my inputs, but im going to try to make my attacking animation more than two frames and disable all input/movement during the attacking animation and see if it turns out looking and feeling better

or i might have to again restructure this like you suggested, ill post my code again whenever i get everything hammered out in case anyone needs it for reference!

also yes i believe your assumptions about GML are correct, this is in a step event so it runs every frame