Check out a free preview of the full Get Kids into Coding with Scratch course
The "Platform Collision Detection" Lesson is part of the full, Get Kids into Coding with Scratch course featured in this preview video. Here's what you'd learn in this lesson:
Steve implements the ability for the hero sprite to jump and discusses the pseudo-code for improving the previously created "fix landing" code block.
Transcript from the "Platform Collision Detection" Lesson
[00:00:00]
>> All right, I said we're gonna work on x, but I want to, first of all, address some little things that could possibly go wrong, which is this code. If the green flag was already running and then you went to drag him, well, it's checking to see if he's not touching it.
[00:00:13]
And then theoretically iterating the y so it's ready to drop real far. The other one is, if you don't use the color picker, you could end up with a color that almost looks the same. But unless Scratch has the exact same color, that triggering color won't do the trick, right?
[00:00:32]
Which is, again, an argument why you could choose to use Sprite collisions instead of colors as well. But a simple way to fix that is just grab it and pick the same color, right? Otherwise, slightly off, it doesn't, and if you're very close, it could be hard to see that, so just check that as well.
[00:00:50]
So before we do the x axis, let's add back in jumping, and then I wanna show an even better way to do this. Cuz we are constantly checking to see, every time, can we go back up, and doing it one by one. There should be an easier way.
[00:01:10]
If it was falling, we should know where we were and to be able to adjust a little bit easier as well. It can be a little bit more complicated. And I will argue with you that if you don't need something like this, then maybe you don't need to do it, and just go for something simpler.
[00:01:30]
But especially when it comes to getting the x axis working, I think it makes a little bit more sense to do this other way as well. Again, it's a little bit more complicated. First, let's put back the jumping so I don't have to keep dragging this dude, and then we'll talk about that.
[00:01:49]
So with that, we'll simply say, If, nope, not there, too much, do it over here instead. If they press the space bar, again, if you want a different key, go for it, then we'll set the velocity, I don't know, 10. To 10, I will actually put that in our block, otherwise nothing's gonna happen.
[00:02:25]
Cool, it works, now I don't have to keep dragging him all the time. I mean, considering I don't stop him from jumping, you can hold that space bar and get whatever you want. [LAUGH] Right, I guess it's also got a Kirby mode, but it's also got rocket ship mode, hold that space bar.
[00:02:44]
Yeah, depending on how you drew your course, I don't think, 10 might not even really do the trick for me here. But whatever, at this point, we just wanna make him hit the floor. Yeah, there's some definite flaws with this. Yeah, he has the teleportation, cuz that's our logic, right?
[00:02:59]
That is the code that we wrote, look at it. It says, if touching red, change y by 1, which is up, until you're not touching the red. My code is great, my mechanic is terrible. [LAUGH] My game design is not good. My programming is flawless right now. So, yeah, we want to adjust for that.
[00:03:23]
So one of the things we can do is we can begin to say, okay, I'm gonna show you. A lot of times in programming we tend to write what we call pseudocode, which is, we write out the plan before we do the thing, right? So here's our plan.
[00:03:46]
Before we were saying, if in any reason for any shape or form, you ended up in red, one at a time until you're back up. Now, as Caleb noticed, that has some unintentioned consequences. When we jump, we hit red, you teleport back up to the top. Okay, so that's clearly not what we want, cuz we want this to work in all directions.
[00:04:06]
In fact, what we're gonna want to eventually do is have the same logic work for moving left and right. Because right now, if my character could move left and right, he would shoot up to the top of every step as well. Caleb, the problem is worse than you think it is, [LAUGH] right?
[00:04:22]
This game is worse than you think it is right now. If we had left and right, it'd be even worse. But if we fix it now with the up and down, we can then reapply it to left and right and we will be happier people. All right, so what we're gonna do is, let's say we want him to move ten steps, right?
[00:04:43]
What we're gonna do is, instead of letting it happen and then trying to deal with the consequences. What we'll do is we'll say, we're going to write our own custom block that says, move ten steps, move one step, are you in the ground, no? Keep track of where you were.
[00:05:02]
Move another step, are you now in the ground, no? Keep track of where you just were. Take another step, you're in the ground now? Go back to that last spot and be done, right? That is our basic plan, which is, if I say move ten, Scratch will just move in ten.
[00:05:18]
We're gonna say move one ten times, right, each time check if you're allowed to go. And if you're not allowed to go, we're done with the last place you were allowed to go. No more teleporting through platforms and shooting up to the top, we're done with that, that's no way to live, yes?
[00:05:35]
>> I got another idea for a jump.
>> Okay.
>> If you were going up and hitting the red, then you go down until you're not hitting the red anymore. If you're going down and you hit the red, then you go up until you're not hitting it anymore, and then you should repeat it for all the-
[00:05:57]
>> Yeah, that'll work, too, absolutely. Cool, let's try this one. Yeah, cuz that would just, basically, you'd keep track of which direction, and then you would do the right code path for that, yep. This will actually, shockingly, work in all directions kind of out of the box, too.
[00:06:13]
So let's try this as well, and both would work.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops