Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 24, 2024, 04:46:52 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Animation on button press not working
Pages: [1]
Print
Author Topic: Animation on button press not working  (Read 688 times)
Kentz
Level 0
**


zenith au lait


View Profile
« on: April 03, 2017, 04:01:58 PM »

So I've been working on a platforming engine these past few days, and now I'm trying to solve this little problem: my attack animation (which should play when I press the Z key) is acting quite strangely. If I press Z while standing or jumping, it plays the first frame and then returns to the previous sprite. If I press Z while walking, it plays a random frame (from a total of 4) and then returns to the walking sprite. I've googled it multiple times but none of the answers I found worked.

Here's the animations code:

Code:
if (hspd > 0) { 
    image_xscale = 1;
} else if (hspd < 0) {
    image_xscale = -1;
}

if (state == "walk") sprite_index = spr_walk;
if (state == "air") sprite_index = spr_jump;
if (state == "idle") sprite_index = spr_idle;

if (state == "attack") {
    canAttack = false;
    sprite_index = spr_attack1;
}

And here, the states code:

Code:
if (hspd == 0 && on_ground) {
    state = "idle";
}

if (hspd != 0 && on_ground) {
    state = "walk";
}

if (!on_ground) {
    state = "air";
}

if (keyboard_check(ord('Z')) && canAttack) {
    state = "attack";
}

This is probably a pretty simple question, but I'm stuck. I'd really appreciate any help!
Logged
andyfromiowa
Level 1
*



View Profile
« Reply #1 on: April 20, 2017, 08:06:01 PM »

I see you've been waiting a while for help on this, so I will take a crack at it.  Full disclosure: I am far from skilled when it comes to debugging code just by reading it online.

My best guess here is that you have multiple state checks that are simultaneously true.

For example:

Code:
if (hspd == 0 && on_ground) {
    state = "idle";
}

This statement does not take into account whether you are carrying out an attack or not.  So, your character is "idle" when standing still and also "idle" when standing still and attacking.

Generally speaking, the step event will write your state to "attack" because that line of code is last.  However, every step, it's first changing it to one of the other states before it overwrites it as "attack."  This is my best guess as to what's causing the flickering and incomplete animation.
Logged

mokesmoe
Level 10
*****



View Profile WWW
« Reply #2 on: April 24, 2017, 05:31:05 AM »

1. your state system doesn't seem to be checking if you are doing an attack, so it puts you back to either idle/walk/air next frame

2. When you change sprite_index it doesn't automatically reset image_index to 0, which is why the walking attack uses different frames (whatever frame the walking animation was on)


Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic