The Legend of the Tomato Quest Tutorial - Attacking Animation

The Images and Sounds
To create an attacking animation, and to play a sound when the player attacks, naturally we need some images and sounds to do this.

Download and extract the Swing.wav file from the following zip, and place it in the Resources\sounds directory of your game project.

[[Media:Swing.zip|Swing.zip]]

Now save these 4 Images and put the into the Resources\images directory of your game project.



Now in your project, inside the GameResources.cs source file, in the Resources class

Change the LoadImages method to look like this:

This will add the 4 Sword Slash Animations to our image library within the game.

Now we need to add the Sword Swing sound.

Change the LoadSounds method to look like this:

This will add the Swing.wav sound to our game library.

Adding the Sprites and Sounds to our Character
Now we need to add all these sprites and sound to our Character.

Add the following fields to the Character class in Character.cs

The first set of Sprites are for the up, down, left and right attack animations. The CurrentSlash Sprite will be used for the attacking animation the character is currently using.

We've also included a SoundEffect called Swing which will be the sound of the Sword swinging when he attacks.

We've got a Boolean called Attacking, this is used to tell our methods when the character is attacking or not, whichs helps us activate things like animations only when the characters attacking boolean is set to true.

The Cooldown is used to make sure the character can't attack at an insane speed, and forces the player to be able to attack only every few moments or so, depending on their attack speed of course

When we implement these new parts of the structure they will make more sense.

Setting the Default Attack Settings
We need to setup the Animations, and all of its variables

Modify the Character constructor in the Characters class, within Character.cs source file, to look like the following.

The first thing we do is create the Attack animation Sprites for each animation. Each frame has a delay of 1, and has 5 frames. The Width and Height's vary for each image, because of their different dimensions.

Next we set all the Attack Animation Sprites to a Stop Ending Action. What this does it, when the animation plays and stops, it wont play again, which is what we want. Later we'll use a command that will replay the animation, by resettings it. This type of animation is good for when you want to loop an animation every once in a while, not repetitively.

Next we set the default Attacking state to false, since the character hasn't attacked yet.

And lastly, we assign the Swing value with a sound from the Game resources, to be used when our character attacks.

Initiating the Attack
Now we need a method that will start out attacking sequence. We don't use a method that does all the attacking, we use 1 method to start the attack, and 1 to update the attack.

Add this method to the Characters class in Character.cs source file:

What this method is doing is, checking if the character is not already attacking and if the attack cooldown is 0. We do this so that he doesn't attack when hes already in the process of attacking, but also so that when he finishes his attack, he doesn't immediately attack again.

After that we have a switch, with 4 cases, each case for Up, Down, Left and Right, since he can only attack in 1 direction at a time. Basically all of these cases are the same with extremely minor difference, let's look at case CharacterAnim.Up.

In this case, the first thing we do, is copy the SlashUp sprite from the character to the CurrentSlash. The CurrentSlash is the current weapon begin used by the character, and since we want to attack up, we're basically switching his attacking animation to the upward animation.

We then use ReplayAnimation to reset the CurrentSlash animation, since its set to a Stop type animation, everytime it ends, it needs to be reset.

Next we set the character's attacking state to true.

We also set the Cooldown to the attack speed of the player, so that after this attack finishes, he will have to wait a moment before he can attack again.

And lastly we play the Character's Sword Swing SoundEffect, and break from the case.

This whole method sets up the attack. Next we'll look at how the game loop handles the attack every loop

Updating the Attack
The Next Step is to Update the attack sequence everyframe.

Add the following method to the Characters.class inside Character.cs source file.

The first step this method does is to check if the cooldown on the player is greater then 0. If it is greater, it reduces the cooldown by 1. With this, the player will slowly be able to get to a state where he can attack again.

Next we check if the players Attacking state is set to true and then we check that his CurrentSlash animation hasn't ended.

After that we go through a switch with 4 cases, depending on which way the character was facing using the _Anim value. For each case, the CurrentSlash sprite is moved near the Character, but with a different offset for each direction. For your game, to get these offsets correct, will require trial and error, but once you get the right offsets, the attack animation effect looks awesome.

After all the cases, we then use UpdateSpriteAnimation to update the CurrentSlash Sprite animation. But also we use DrawSprite to draw the CurrentSlash Sprite to the screen.

If you look back at the top where we check if the CurrentSlash animation has ended. You'll see below it, theres and else statement. And in this else statement we set the character's attacking state to false. Since if the CurrentSlash animation has indeed ended, then we want to stop the player from attacking again, until the cooldown gets to 0 and the player presses the button to attack.

Assigning the Control to Attack
Firstly Add the following line of code right at the end of the UpdateCharacterAnimation method in the Characters class in Character.cs source file:

The last thing we need to do, is to assign the key to make our character attack. Modify the UpdatePlayer method in the Controller class, in Controller.cs source file to look like this:

Now the first part of the new code, is checking if the Space bar has been hit, and if so, runs the IntiateAttack command for our character.

Now compile and run, and if you followed the steps correctly you should see your character can attack.

Finishing Up
Here's a screen shot of the attack animation in action.



The Project so far
You can get the Source code for the project up to the this point [[Media:RPGTutorial6.zip|Here]]. I Strongly suggest going through the tutorial while, looking at the source code, as it will help you understand it.

Next we'll look at making NPC's (Non Playable Characters), who will be in our level and to heal us when we have injuries.