[Intro] Hello fellow Scratchers, I’m Griffpatch.
This is going to be a short tile scrolling tutorial today, because I’m on my holidays!
Woo… So what are we adding? Yep, crouching and sliding! Cool. So let’s get to it 😊
[Crouching Key] We’ll begin by adding support for the down
key to begin a crouch action. The best place for this is in the existing Handle Keys – Jump
custom block. Yeah, it’s not jumping but it is an up and down movement, so we’ll
rename the custom block to be Handle Keys – Jump / Crouch.
Ok, Separate off the IF UP block and below, and we put in a new IF ELSE block here, checking
for down of Controls being greater than 0. If you remember all our key controls are coded
in the Controls sprite. Now, we only want to crouch if we are on the
ground, not while falling in the air. So another IF checking whether falling < 2. We are either
on the floor, or have just been there. Now, we simple set player action to the word
“crouch”. This is great as we will now be able to check whether Mario is crouching
by looking at this player action elsewhere in our code. For example, to set the costume.
Next up, set jumping to 0 since we are definitely not jumping now.
So, what about when we have let go of the crouch key… we’ll check for this in the
‘else’ where the down key is not pressed. IF ELSE, and check for player action = crouch.
If it was, then we’ll try setting player action back to the empty value, meaning that
Mario is no longer crouching. Finally, in this second else, we are not crouching,
so bring back the original jumping scripts.
[Costumes] Before we can test this, we need to get Mario’s
costume to change too. Find the “define Paint Sprite”, and you can see we are already
doing a similar thing with the PLAYER ACTION of lost life here. We can duplicate that IF
condition then checking instead for a player action of crouch. Then switch the costume
to MarioCrouch. Ensuring we still follow that with a stop this script.
Superb, let’s give that a test. We press the down key, and there you go, a
brilliant little crouch action right away! So, can we stop there, are we done? No, I
don’t think so! Behold, my awesome new crouch-sliding mechanic! Weeeee! Yes we can still walk around
whilst crouching. Hilarious to watch, but not something we want to keep long term.
However, since we can slide now easily around the level, let’s make use of this to test
another required feature of the crouch, that of being able to slide into smaller gaps.
For this we should make some small changes to the level to create a one block high tunnel,
something like this.
[Crouching Hitbox – Fitting though small gaps]
And now, if I slide towards this new obstacle, we can see that we have yet some work to do
here too. Although Marios costume is the correct size to go through this gap, his hitbox is
not. Come with me to the “define fix collisions
in direction dx” script. This is where we check 6 points on Mario, the bottom left and
right, middle left and right, and top left and right points.
We could, when crouching, change the height variable such that Mario was shorter… but
if we do that then Mario’s feet will lift off the floor since they are centred around
their middle point… For ease then, how about we just stop checking for the top two collision
points whilst crouching. That would reduce our hitbox by half in height while keeping
it positioned on the screen correctly. We’ll start be rearranging the order of all these
fix collision at points so that the second values are paired together. Their feet first,
then their middle, then their head. Good. And now… Surround the last two checks, these
are the top checks for where Mario’s head used to be, surround them with an IF, and
we’ll only check this when Mario is not crouching. (Not player action = crouch).
Its testing time, run the project – We can start by making sure we are still prevented
from walking into this gap when standing up straight. And now crouching – Yes, that’s
really good. Out smaller size lets us slip through the gap now – Very good.
[Standing Back Up] So, this means we can now get into a position
we couldn’t before, and that is to be crouching under a block. We then have to consider, what
will happen if we let go of the crouch key while we are still under this block!
Ah – So that’s what happens 😊 – As soon as we stand up, our hit box suddenly
in contact with the level and things go a bit crazy, popping us up through the level
out of the ceiling above. What we really want to do here is prevent us standing up again
when there is no room to do so. Come back to the “define handle keys – Jump
/ Crouch” script. Here is where we handle the crouch key press,
and here is where we get back up again by setting player action to the empty value.
We are going to need to do a little more work before we allow Mario to stand up, so I think
it best we create a new custom block, naming it “handle – get up”.
We’ll switch it round to now use this in place of the set player action. There.
Ok, so at this point Mario will have got up. We should take this opportunity to check whether
we are colliding with anything, but remember that checking for collisions will also move
Mario out of the collision, so… Let’s keep track of their current position in the
“orig y” variable first so we can restore it afterwards (orig y stands for original
y). Set orig y to y. Now, we use fix collisions in direction dx
of 0 and dy of 1 (as our head is moving upwards). We can then check if we bumped our head, IF
solid > 0. Right, so if we did bump our head then this
is no good, we need to crouch back down quickly! Set player action back to crouch, ‘and’
remember to restore our y position back to orig y so we stay where we were.
Now, before this will work there is another fix we must make. Come with me to the “define
handle keys – left right” script. Look here, we are setting player action to walk
right at the top. This is no good, because this undoes our good work of keeping the crouch
action set! This needs to change a little to account for crouching too.
Add an IF ELSE block right at the top, and check IF player action = crouch. The rest
of the script can be placed into the else below.
Nice, let’s run the project and check that out…
[Slowing Down] Ok, this is interesting! Now when I slide
underneath I can no longer get up, BUT, I also continue to slide all the way through
and cannot stop! Ha.
We need to begin by making Mario slow down when he’s crouching.
First we’ll check whether they are moving at all, IF the absolute value (that’s abs)
of speed x > 0.4. That means the speed is greater than 0.4 or less than -0.4.
Then we want to slow down by 0.4, towards 0. We could do that with another IF ELSE,
but let’s do it with maths. Change speed x by -0.4 * (speed x / the same abs of speed
x. There. Dividing a number by the abs of itself will result in either a 1 or a -1 if
the number was negative. Quite a useful thing to remember. That means speed x will be changed
by either -0.4 or 0.4 here, slowing the player down.
Next, add a “make skid smoke” for extra effect.
Now, in the else we can ensure we are now stationary by setting speed x to 0, and setting
player frame to 0 too to keep the animations in check.
I’m testing this now, Oh yeah – so crouching is now looking much more cool as I slide to
a stop with the particle smoke. Of course there is then one question remaining… What
happens if I slide under…
[Getting Unstuck] Ah… Yes, just what I was wondering. As I
am crouching and cannot move… and I cannot get up… I am stuck!!! Oh no! lol
Well, we have three options here – We say too bad, and lose the player a life – But,
that would just be cruel. We could keep the player sliding like we did previously so that
always make it out the other side… That would be ok… but it wouldn’t make any
of these sliding obstacles a challenge. So, the third option and the one we will take
is that we will force the player back out the way they came.
Find the “define handle – get up” script. When the player is trying to get up, but there
head is colliding (that’s here). We will change their speed x by direction (that will
be 90 or -90 depending which way they are facing), divided by -200. I just picked a
good value by trial and error. Negative so that they move backwards from under the block.
Come on them – Let’s give it a test. I’ll slide under the first block… oh yeah,
did you see that. It was subtle but Mario definitely slid back out. I’ll take a longer
runup… Yeah there you go – You can see it working
in all it’s glory there as we slid right back out. Excellent work!
[Outro] And dare I say it – We are done for this
short episode :D – I hope you enjoyed it, and if so, then please do hit that like button.
I’ll keep trying to get tutorials out over the holiday season so do subscribe to catch
them as they become available. Until next time though, Thank you for watching, and Scratch
on guys!