Let's stun an enemy by bouncing on his head...and 3 times for a kill!
Here's a bit of fun. In this tutorial we're going to jazz up the stock 'HORIZONTAL PATROL' script that MPAGD ships with and make it possible to stun the enemy if we jump on its head....and if we do it three times in a row...we kill him, turn him into a skeleton and let him float up into the ether. Along the way we'll learn some useful tips for handling sprite TYPE switching and we'll be using READ and DATA for varying some sprite movement.
Hopefully we should end up with something like this...
So, a few things to note:
If we bounce less than three times the enemy will be stunned, recover and then continue moving in the direction they were travelling before they were stunned.
If we bounce three times on their head they will be turned into a skeleton, and then gracefully wobble their way up to the heavens
Here's the code for the enemy knight:
If you are familiar with the standard MPAGD horizontal patrol the first half of the script is pretty much the same, in essence it checks the enemy can move in a direction and isn't going to hit a wall, the edge of the screen or fall off the edge of a platform. The stock routine uses DIRECTION = LEFT and DIRECTION= RIGHT although, in reality, these are stored as numbers 0 and 1.
We are going to take advantage of this by using the number 3 (and above) to indicate the sprite has been stunned.
First we check if the player collides when they are above the enemy, by at least 14 pixels. Anything lower than this will be a failed stun and the player will be punished. As in previous tutorials I'm assuming you are using a couple of Global Variables to store the players X and Y (I use F and G).
You could make the test for a successful Stun a bit more challenging by testing the players X position (F) against the enemies X to see if they are directly (or nearly) above the enemy when they collide...but I'm keeping it simple.
Each time the player bounces on the enemies head we increment DIRECTION, so that when it hits 5 (the third bounce) we start the kill routine.
At this point I change the sprites IMAGE (to a Skeleton), its TYPE, and I also reset the FRAME to 0 and its SETTINGA to 0.
I need to do this because of an important characteristic of MPAGD when we switch a Sprites TYPE. Basically all of its parameters are switched to the new TYPE. Now, my knight sprite has 2 frames (0 and 1), whereas my Skeleton sprite has only 1. You might think that I could overcome this by adding the correct parameters into the SPRITE INITIALISATION script, but TYPE switching does not initialise a new sprite, it simply changes its TYPE. So if I was on FRAME 1 right before the Knight became a Skeleton, the Skeleton would try to display FRAME 1...which doesn't exist, and would get confused and display garbage or another sprite frame entirely.
Similarly, as both of the scripts for the knight and the skeleton use SETTINGA, I need to reset it to 0 before switching.
Right, with that out the way, lets have a look at the Skeleton script:
This is pretty straightforward. We start with a little timer so that the skeleton doesn't move for a second or so, then we have a series of potential directions that the sprite can move, we then change the DIRECTION value by reading the next value from the DATA each time we cycle through the code. BY change the DATA values or adding new pattern statements, we can add interesting movements to our sprites.
Finally, we check where the sprite is, to see if he is moving off the top of the screen before removing him.
And that's its, stunning enemies and floaty skeletons....huzzah!