[Intro] Hello fellow Scratch Bug Squishers!
For a few episodes of our classic platformer series, I’ve been getting reports of a bug
cropping up in the wall jumping code. Every now and then I’ve seen it too… Wait, there!!! Did
you see? - How many of you have seen this in your projects too…? Yeah, once you know how to exploit
it, it can be a real game breaker, and it's way overdue a revisit to put a stop to this madness!
[Phase 1 – Bug reproduction] But wait right there – If we are going
to do this, we should do it officially! And we’ll therefore break this up into 5 bug fixing phases.
Phase 1 – Bug Reproduction, or Repro for short. Before we can fix a bug, it's vitally important
to first be able to consistently reproduce the issue, over and over again, hopefully at will.
Gather as much information as you can from the hints left in comments from fellow Scratchers,
and then try to replicate it yourself. In our case, the main factors appear to be that
while we are wall sliding… we are then, somehow, able to jump back up the wall…
However, just what steps this takes are not super clear. So I put the tried and tested approach of
wildly mashing my keyboard into practice. Ah-ha! I have a repro – Now I just need to
work out exactly what I just did. And then hopefully ensure it is consistently reproducible.
Ok, yeah, I got it. Yeah… Yeah… This is most certainly it… Wow, I can really get to places
with this bug that should be completely out of reach! Oh it’s actually ‘too’ fun… but…
We gotta fix this… Really? Yeah, we do… But do we? Yeah. Seriously - it breaks the game dude?
[Phase 2 – Documenting the Bug] So we now proceed on to phase 2, the documenting
phase. This is where we list the steps that cause the bug so that anyone who follows them can
reproduce the same issue. This is mega important, as later on we need to be able to follow the
same steps to confirm that the bug is fixed. So, we jump against a wall. This triggers a wall
slide. Now, while still sliding down the wall, we pull away by pressing the right arrow key…
And there!!! Did you see it? I'll rewind, and slow the video down.
There! For one single frame, our player is facing the wrong direction!
And now we immediately mash the jump key… Then, boing - back up the wall we go!
So with those steps recorded, we have an easy to reproduce bug report. Nice work!
[Phase 3 – Understanding the Bug] Phase 3 - Understanding why the bug occurs.
In our case we can start by asking why we end up facing the wrong direction after pulling away from
the wall. Well firstly we have to remember that when we jump against a wall, we enter “wall slide”
mode and, importantly, appear to turn around to face the opposite direction as we slide.
Now, if you look in the costumes, you’ll see that the wall slide costume actually points in the
opposite direction to all the other costumes. Yeah, so when wall sliding we don’t actually
turn the sprite around at all. The player is still facing towards the wall, only
the costume is flipped to face the other way. But… as we press the right arrow to pull away from
the wall, our players direction is changed to face right. It just so happens the wall slide persists
for just the briefest of moments, and so, the wall slide costume appears facing left! Whoops!
So… is this important? Well, if our game thinks we are still wall sliding, then we have a tiny,
but very exploitable opportunity to perform a wall jump just after leaving the wall,
but while facing the wrong way! And THAT. That is exactly the problem we are seeing.
[Phase 4 – Fixing the Bug] Splendid! Sherlock would be proud – And we enter
phase 4 where we get to actually fix this bug! I feel that if we simply cancel the wall slide
as soon as we change direction, then the following wall jumping exploit will just go away.
So to the code – ensure we are in the Player sprite, and find the "define Controls - Left
and Right" script. This is, as the name implies all about handling the left and right controls.
This first IF checks for when we are ‘not’ pressing left or right, so KEY X is 0.
But what we are really interested in is below this, within the else branch.
Right, here we go. See this "point in direction" block. This is where we make
the player face the direction being pressed. Now, we only want to cancel the wall slide if
the direction changes, so we’ll pop in an IF block ‘before’ we actually set the new direction, just
here. Make sure to move the point in direction and change frame blocks below this new “If”.
Now to detect when a change is about to occur. We can compare the current direction
with the new direction. If they are different, that is “not”, “equal”,
then we cancel the wall slide. The current direction can go on
the left. The new direction is given just below in the point in direction block,
KEY X * 90, so duplicate that in here. Excellent. Now we know when the direction
is changing. All we need to do now is cancel the wall slide.
Bring in a set block, and “set wall slide to 0”. Easy-peasy when you know what you are doing!
[Phase 5 – Testing] Right! That brings us to the final phase
of our bug fixing exercise – The testing phase. To know whether the bug is completely gone,
we simply go over the exact same repro steps we originally documented in phase 2. If these steps
no longer result in the same bug, then by Jove, it means we’ve done our job, and the bug is fixed.
So here goes nothing. Jump, slide, and pull away. First impressions are good. I didn’t spot the
player facing the wrong way this time. So, let’s go full hog and try for a wall jump back up the
wall… I’m trying, I really am, but no matter what I do… nope, nope, nope! I canna do it captain!
Bug squished! – Excellent. [Phase 6 – Celebratory phase]
Ok… Did I say there were only 5 phases… Yeah, this is the unofficial 6th phase, the jumping around
party celebration phase… whoop, whoop! 😊 [Outro]
Right, that’s it for this mini bug fixing video,
I hope you enjoyed the process! Those who want more, don’t worry, the official episode 12 will
be coming out next, and what I have planned is just so super cool! Rotating platforms,
yeah - as showcased in my MMO platformer. I know right, these are so sweet and you just
don’t see them in Scratch projects so I’m so excited!!! If you can’t wait to get at them too,
then you might want to consider jointing my early access channel membership for the
chance to catch it before everyone else! Ok, you know the drill, don’t forget to
love the video, subscribe to the channel if you haven’t already.
And until next time, have a great week ahead, and Scratch on Guys!