[Intro] Hello fellow Scratchers, I’m Griffpatch,
and today we are adding vertical scene switching to our classic platformer game. Yes, that
not only means, you’ll be able to walk off the left or right hand sides of a screen,
but also the top and bottom edges too! This allows us to build much larger, more exciting,
multi-dimensional levels! Not only is this super useful for platform games like this
one, but also for RPGs or other games you may like to have a go at coding! Oh yeah - The
same basic code can be used for both! Right though, before we begin, just quickly
make sure to save your project as a copy. And… name it episode 6.
OK! Let’s get Scratching! [Vertical Level Costumes]
We need to begin by expanding our level scenes. Click into the Level sprite.
Now you’ll remember that the scene switching is coded around each costume being named Scene,
and then a number. As we move across the level we go from Scene1, to Scene2, Scene3, on so
on. Now, I want to add a new scene, not to the
left or right, but directly above Scene 1. What we can do is use the number square technique
of saying each upwards movement doesn’t add a 1, but adds 100 to the scene number.
That would make the scene above scene 1 as scene 101. Then the scene to the right of
scene 101 is of course 102, then 103… Ok, so duplicate the costume for Scene1, and
I’m going to flip it upside down for now to match up with the scene below, but you
can draw this any way you like. Cool. Then go to town and make it look great, I’ll
add a walkway here. Now don’t forget to name this new scene…
This is the Scene above Scene1, so adding 100, that’s Scene101 (with a capital S).
[Switching Vertical Scenes] Now with the Scene drawn and named correctly,
we need to code up this new scene change. Click into he Player sprite, and scroll down
to the “when I receive - Show Tick Last” script.
This is already handling the player moving off the left and righthand sides of the screen.
It makes sense to extend it look for movements off the bottom and top too.
But look, this custom block “Begin Scene #” only has an input for an x position…
we are going to need a y position for this to work. Right click the block and we’ll
Edit it. Change the text label from x to be x & y now.
And then add a new numeric input, naming it y.
Ok, so the y inputs should be blank, and that’s the way we are going to keep them so don’t
fill in a value even of 0. More on that in a moment.
So, Duplicate the two IF block stacks and pop them right below. We need to adapt these
for changes in y position rather than x… So remove the x position blocks, and in their
place use the y position block. The maximum height of the screen is different
to the width, it’s 180 pixels from middle to top edge, so we look for y > 180, that
is off the top of the screen. Mirror that below with a check for y position
< -180. SCENE # + 1, nope now to move up a scene we
do as we said we would and add 100 to the scene. Then for the down script, use negative
100. That leaves the x and the new y inputs. Well,
the x one we can blank out as we don’t want to change the players x position when moving
up or down a scene. But we do want to change the y position. When we move off the top of
the screen, we must position our player back at the opposite edge, so a new y of -180.
And for moving off the bottom, a new y of 180.
So that’s the theory, we just need to modify this begin scene to make it work!
Here’s the set x block… but now this x input may be blank… We need to be careful
not to change the x when this is the case. IF x > blank then set x to the x input. Good.
Now we can do the same for the y input by duplicating that IF block, switching the x
inputs for y inputs, and making sure to use the position block “set y to” and pop
in the y input. Ok… nearly there.
Scrolling down a touch, do you remember this very useful custom block we created - “Fix
Collision is Direction”? The direction at the end is important here.
When moving scenes left to right, the 0 allows scratch to look up and down in case the player
is stuck in the ground after the change of scene to get them free.
In the case where we change scene by moving off the top or bottom of the screen, we don’t
want to search up and down for collisions, but instead, left and right.
We’ll use an IF ELSE block, pop a fix collision block in each.
Then grab the “x >” block from up here, and we’ll use that as the condition.
When we are setting an x position, we want to search up and down as normal for a collision,
so leave that one as is. But otherwise, we’ll assume this is a change
of scene up or down, so replace the second 0 with a 90. That will search first to the
right, then to the left to get us out of collisions. Perfect!
[Testing] Shall we dance? Smash that green flag.
And up we go… Jump, jump… oops! Oh man, let’s try that again haha!
If I can just time it right… It’s a little tricky it turns out! But, the good news is
without thinking about it, the scene is of course switching correctly up and down, so
that’s really great. Notice the scene changing from scene 1 to scene 101. Oh gosh, come on!!!
Lol I just need to hold right as I wall slide
to keep attached to this tricky right wall… Yesss There! Phew. Little celebration!
[Up and Across] Now, question… what happens if I walk off
to the right? Ouch! It’s scene 102… but of course, we
haven’t yet designed that one, so the costume stays on scene 101 and we get trapped in the
wall. I think we know what to do though right.
Click into the Level sprite, and we’ll duplicate Scene 101.
The numbering is not quite so automatic anymore since we have introduced the new 100s. It
appears as Scene6, but we want this to be Scene102. That’s one to the right of Scene101
of course. I’ll do my flip horizontal trick, repositioning
the entire scene in the canvas. Then making sure to unselect all the graphics,
lets give that a test! Holding my breath for the climb… Cool, I
think I’ve got the hang of that now, and then off the right – We have Scene102 – Success!!!
Indeed we can switch back and forward between scenes 101 and 102 with ease…
Perhaps I could drop down to scene 2… Oh. Bother. I added a roof to scene 2, so
that’s problematic. You do have to think your levels through a lot more carefully when
you add in up and down scene changes too! We can just quickly adjust scene 2, pulling
back the roof to leave a gap. Test test test test test… here we go…
And drop down! Woo hoo… Super. [One last thing]
Now, everything is pretty good here, it wasn’t the hardest change to add because luckily
we’d been quite forward thinking and most of the ideas were already in place from episode
2. Before we finish this smaller episode though,
can I just suggest one small change to our vertical scene changes.
Let me just rearchitect scene 3 to add a nice vertical wall jump area. Making heavy use
of holding the alt key to drag in a new duplicate of an existing shape.
Now, I’m going to duplicate the entire scene costume. Name it Scene103 so it’s directly
above the previous scene. And then I’ll flip it vertically.
Now I’ll clean it up – I want to keep the wall jumping area in the same place…
Great – now watch this… If we get to that area on the level. Now jump up here – Oh
I do love this wall jumping… What you should notice is that when I jump
onto the new screen, I’m often very close to the bottom and can slide off really quickly
back to the previous screen. This is ok, but it does make the game a bit
fiddly. You may prefer, like me, to actually move the player a little further up when they
enter the scene above. And it’s easy to do. Click into the Player
sprite, and find the when I receive “Tick – Last” script.
Here is where the player moves off the top of the screen. So instead of positioning us
down at -180… Swap that for, say -160. 20 pixels higher up.
Let’s see if we can see the difference! Jumping up here…
Yeah, so now as we enter the next scene, our player is positioned just that bit higher
up, giving us more time to wall jump again, and see what going on… I think that’s
better, don’t you? [Scratchers]
Well ok then! So this is a shorter episode, but there is plenty more to come very shortly.
But before I close, I’m going to just come back to my Scratch profile and show you a
few of the amazing projects that have been submitted to the classic platformer studio
for the previous episode! Ok, let’s see what you’ve been up to!