Hello MakeCode Forum!
I was having a weird visual bug in my game’s code where my sprite’s animation was not being displayed as I wanted it to. The intended effect is that the walking animation will stop once the action button is pressed (A), then play the swing animation. However, I found it to also play some additional frames of the walking animation.
The extension I’m using for the animation handling is called “Animation”, which gives advanced functionality to the animation toolset. I also use sprite data to help with sprite-specific, like to find out what direction the character is facing
Below is the JavaScript code that is relevant for the animation, any help will be appreciated!
AttachFunctionToKeys(ACTION_KEYS, function () { // Function when A (or another action key) is pressed
if (!sprites.readDataBoolean(usr, "attacking")) { // Only run if the sprite wasn't attacking
sprites.setDataBoolean(usr, "attacking", true)
/**
INTENDED BEHAVIOR:
- Sprite stops current walking animation
- The attack animation plays
- Sprite can return to walking animation after a set amount of time
ACTUAL BEHAVIOR:
- Occassionally, the sprite will
*/
animation.stopAnimation(animation.AnimationTypes.All, usr)
pause(50)
animation.setAction(usr, sprites.readDataNumber(usr, "action") + 4) // +4 represents the offset the swing animation compared to the walking animation, as it is set second
/** Later in the code, once the appropriate sword picture is set */
pause(1000) // Lets swing animation play
animation.setAction(usr, sprites.readDataNumber(usr, "action")) // Resumes proper walking animation
sprites.setDataBoolean(usr, "attacking", false) // Reset attack variable
}
})
...
game.onUpdate(function(){
/** Code for moving sprite... Then: */
// Sets a variable for the user sprite, dependant on what key is being pressed
if (!sprites.readDataBoolean(usr, "attacking")) // Do not update facing direction once swinging
{ // Otherwise, set facing direction based on what direction button is pressed (functions return boolean value)
if (IsLeftPressed()) {
sprites.setDataNumber(usr, "action", 2) // Move Left
} else if (IsRightPressed()) {
sprites.setDataNumber(usr, "action", 1) // Move/Swing Right
}
else if (IsUpPressed()) {
sprites.setDataNumber(usr, "action", 3) // Move up
} else if (IsDownPressed()) {
sprites.setDataNumber(usr, "action", 0) // Move down
}
}
// Checks if the player is actually moving
// The sprite hitbox is to help determine collisions, not actually animated
if (!sprites.readDataBoolean(usr, "attacking") && Math.abs(usr_hitbox.vx) + Math.abs(usr_hitbox.vy) > 0)
animation.setAction(usr, sprites.readDataNumber(usr, "action")) // If it is, play animation
else if (!sprites.readDataBoolean(usr, "attacking")) // If the sprite is not attacking
animation.stopAnimation(animation.AnimationTypes.ImageAnimation, usr) // Otherwise, stop animation
/**Some more code*/
})
I can provide video of the visual bug occurring, but I’m currently going to be busy with other things for a bit before I can provide that. Hope this is enough!
