Code a Platformer Game | 8. Moving Platforms

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers yes oh yes moving platforms i'm so very very excited because well this episode is pretty much the whole reason that i began the classic platformer series yes i am griff batch and today we are adding moving platforms woohoo these platforms can come in all shapes and sizes and can have simple or complex movement patterns we'll also be covering the tricky issue of collision avoidance and the unavoidable and very sad squishing of cats yes it's all here so gosh what are we waiting for let's get scratching as we begin make sure to save a copy of your episode 7 projects for this is episode 8. now since we are going to want to add a moving platform right here on the very first scene of the level i'm going to click into the level sprites costume editor and make some space by deleting the central diamond shape cool now let's create a new sprite to represent the moving platforms i'm going to name it platform for short next draw a simple rectangle shape this will be my moving platform but don't worry it can be any shape or color you want you should play around with some of these for some fun but now we can drag the platform on the stage to a good starting position i'll pop it down here in reach of scratchy okay that's a good start if i run the project the moving platform is visible but it is far from solid we can run right through it this is the first thing we should solve as you might suspect it will be really easy click into the player sprite and find the check touching solid custom block you remember when i made you create this block and you probably thought gosh why go to all the effort for creating this only to do a touching sprite we could have done this without it well this is where we get the payoff where we originally had touching level now we bring in an ore and switch it to have two touching blocks the second one can become the new platform sprite so what have we done here yes everywhere in our code that uses the check touching solid and that's quite a lot of places now every one of them will now also count platforms as solid 2. oh yeah now that saved us a whole lot of time let's test it out walk over and jump on the platform yeah look at that the platform is indeed fully solid that's great just as solid as the level itself well next up walk onto the next scene ah the platform is here too and it's just as solid as it was on scene one so we need to tell the platform which scene it belongs to click into the platform sprite we have a nice event for changes of scene so bring in a when i receive event block and the event we'll use is ah yes change scene great the event only gets triggered when the scene is changed not every frame which is very useful to remember now to ensure the platform does not appear on every scene we'll hide the sprite then if scene hash equals one our first scene then show that's about as simple as it gets right but luckily this works just fine you can imagine we can add as many ifs here as needed to define moving platforms on lots of scenes but enough of that let's get that platform moving make a new custom block we'll name it animate platform i use the word animate because it reminds me that this is a long running operation and we should therefore not be ticking the run without screen refresh block it'll be clear why shortly so drop a copy of this new block into the change scene if block after we show the platform okay a little bit of teaching for you now we will shortly be adding scripts into the animate block that will move the platform up and down in a forever loop so the question is if we are looping it forever what will happen if the player changes scene will the script keep running the answer is no and why not well event receivers like this one have an interesting property in scratch that when they get broadcast again while a previous one is still running the previous one gets stopped right away before the new one begins running in its place so when we move to scene 2 the animate platform script gets stopped and the platform should hide ok lesson over let's make the animation as long as your project is stopped when you drag out your go to xy block it will be automatically set to the position of the sprite on the stage as it is right now that's useful now drag the platform to its next position up here now let's get a glide block cool again it's all set correctly i can test the project to check now it glides right up there but it's a tad too fast i'll change the glide speed to two seconds yeah that's much better next to glide it back down to the start drag in a new glide block oh wow it's set it to the original position i was expecting the new position but i guess this is because the project is still running it must remember the first position it was in well just make sure that the x and y of the glide are the same as the x and y of the original go to x y okay and set the glide speed to two seconds as before running that gives us a nice up and down motion so to keep the motion going we can surround the glides with a forever loop like this now one more thing that can make this look even better is to pause the platform at the top and bottom with a weight block for yeah one second will do okay this is looking perfect i'm all ready to give this a try but what will happen if we get on board oops oh so wow this wasn't so great the lift was able to move right through us how did this happen well this has to do with how and more importantly when our sprite collisions are detected and resolved at the beginning of every game frame we assume the player is not colliding that is not overlapping anything solid next we move the player check for collisions and then move them back out of any collisions again now what has changed here is that the platform scripts have now become the first thing to run in our game frame how do i know it runs first well that's down to how scratch operates the first thing to run is any script that is already running from the last frame like forever loop we have our main game loop and we have the platform forever loop but look the game loop broadcasts these events so these events get ready to run as soon as possible but not before this platform forever loop gets to run because it was already running from last frame and so we know that the platformer will always get in before the other broadcast receivers right then so the first thing to move in the game frame is the platform but now look that means we are beginning the player's scripts now only to find that they are colliding this is not a good state to be in as it is known as the stuck state in our game and it becomes impossible to move now why isn't the level turning red then to show that we are stuck well in the player sprite find a when i receive tick player look the switch background that we put in here is only looking for touching level not touching platform that was my bad we should have really used our custom touching solid block for this too shouldn't we anyhow what we need to do for starters is now realize that we may be stuck in the level even as this event receiver begins to run tell you what create a new custom block naming it moved by moving platform and yes we run without screen refresh this time we'll use it first thing as the tick player script begins to run i'll just move it into some space to decide if we are indeed colliding use the check touching solid block and check if touching is greater than zero if it is then we have most likely got trapped in a moving platform and for now the quickest way out would be to use our existing fix collisions in direction block setting the direction to zero to push the player up out of the collision okay cool so let's see what effect this has on the moving platform shall we just getting ready to jump on top and yes look at that we are going up this is awesome the platform feels super solid and is working surprisingly well in fact if you were careful with your level design you might even be content with this just as it is because it looks great so well are there any problems yeah there are let me just walk under the lift okay this isn't going to be pretty and did you see what i saw scratchy miraculously avoided certain death by teleporting on top of the lift that's crazy but also expected the fix collision in direction script we used was after all not designed for resolving collisions with moving platforms and sadly there's worse to come let's switch the direction of the animating platform click into the platform sprite i'll duplicate the animate script so we can keep a copy now let's quickly set the start position and matching end glide position and then fill in a new exposition for where the platform will glide across too okay running that now let's see how this affects the game if i jump on the platform oh wow it's hard to stay on but wait why is that well it's because although the platform is moving left and right unlike the up and down platform it's not pushing me and it's not pushing me because it's not colliding with me no it's just slipping by under my feet so there's obviously another mechanic missing here and that is floor friction we need the very fact that i'm standing on this platform to mean that my player is moved along with it the problem is to do that we need to know how far the platform has moved by each frame and look we are using a glide so how much is it moved by well let's work it out bring in a new when i receive block and we'll make a new event named tick platforms now just like all the other tick events as i described earlier this too will run after the anime block which is in a forever loop so by the time this runs each game frame the platform will already have glided along by a small amount well perhaps we can measure how much it has moved make a new custom block named tick and add a numeric input of new x and another named new y check the run without screen refresh great this new block will run under the new event and we can pass in the new position of the platform that is position x and position y remember that the glide has already had its chance to move the platform so this is the new position but where was the platform last frame we don't know do we make two new variables last x for this sprite only and last y for this sprite only then after the tick block has finished running we set last x to position x and then do the same for last y to position y this means the next time this event gets broadcast we will know where it was last frame and the great thing about that is we will be able to calculate how far it has moved we can test this using a say block and a subtract and we say new x subtract last x yep that gives us the difference between the two x positions run that now what oh sorry what a scatter brain we haven't yet broadcast the tick platform event click into the player sprite and find the when i receive game loop script we broadcast tick platforms and pop it after the first broadcast aha we have something showing up now do you understand what we are seeing the platform is telling us how many pixels it has moved in the game frame to get to where it is now notice when it stops the number changes to a zero because it's not moving i think that's really cool what with the fact we are using glides and all and i'm certain that this will help us move the cat on the platform so back in the platform sprite move the say block out of the way now bring in and if else what we need to know next is whether the cat is even touching this platform or not if touching player then for now say the speed again else say nothing that seemed like a good plan but sadly this doesn't work and why because the platform is simply never in contact with the player as we said before the platform is sliding along peacefully under our feet to really know it's touching we should move the platform up a little tiny bit before doing our touching check put another if else block above the first and check whether the new position is above the last one in this case the platform is moving upwards this is the easiest case because as such we can just move the platform up a tiny bit more change y by one and it's bound to be touching the player's feet but what if the platform is moving down and therefore away from the player that makes it a little bit more problematic because even if we move up a little bit it could be far from the player's feet but luckily because we know the previous position of the platform we can just put it back up there and then look above that position instead and then once more we will be able to locate a collision or not we are not quite there yet though as if you run that you can see that the lift is trying to drift away that's because all the moving the platforms has taken it off course of its original glide we must restore its position after all the checks are done use a go to and drop in the new x and new y that's where it's supposed to be run the project again i'm feeling really good about this i just need to get on the platform it's harder than it looks yes brilliant now we can see the speed of the platform but only when we stand on it the obvious next step is to get the player to move along with set platform keep the first say block handy but discard the empty one we're all done with that now for flexibility we are going to use some global variables to record the required movement of the player make two variables platform dx for all sprites and platform d y for all sprites now set platform dx to the new x subtract last x from the say block that now records the left and right movement but platforms can also go up and down so we also set platform d y to new y subtract last y so if we run the project now and i climb on the platform you will see platform dx changing value but when we jump off mid flight you'll notice that platform dx does not reset to zero or empty value but stays set that will cause us some confusion if left like that but i'm not going to set them to zero in the else here as you might have expected because in the long run we will hope to have more than one moving platform on a scene and if that is the case then even if we are standing on one platform the other platform would then set the variable to blank completely messing up our code so instead click into the player sprite and we want this tick first script this runs before the tick platforms so in here we'll set platform dx to the empty value and do the same for platform d y great so smash that green flag and we should be able to confirm that the platform values are now only set when we are indeed standing on the moving platform excellent we are really getting somewhere now find the custom block we created earlier today moved by moving platform and now before we check for collisions let's move the player we need a change x by and a change y by block and then drop in the new platform dx and platform d y variables oh yeah this is going to be great run the project and wait for it oh yes look at that doesn't that look great we are being carried along beautifully by the platform no obvious pixel slippage and we can even walk left and right as we travel along one thing we haven't implemented here is continuation of velocity you will find that as we jump off the platform we jump vertically upwards and are not traveling along with the platform any longer but perhaps that is one for another time maybe slightly surprisingly you can see that when standing in the path of this platform we even get pushed along by it that is because as soon as we collide the platform is counting us as being on the platform and moving us with the platform but this may be a bit finicky and prone to issues here let me show you i'll click into the level sprite and add a solid wall right in the path of the moving platform so what do you think will happen now as i travel along on the platform here we go oh oh now i was not pressing jump there in case you didn't understand what you were seeing what is happening here is that when a platform moves us into a collision we still have the fixed collision in direction script running and that attempts to lift us up out of a collision but in this case the collision cannot be resolved by moving us up no we need to move right to get out of this one as such we bounce upwards and things do not look so good so can we just set the fixed collision in direction to 90 to push us out sideways let's give it a go wow yes okay so that's a lot better so why did we set it to zero in the first place well because we were originally dealing with an up and down moving platform click back into the platform sprite and switch back the old animate platform script the ones that make the platform move up and down now let's see if this platform is still behaving itself oh what no as soon as i was on the platform and it moved i was wiggled right off without warning this mechanic has been completely broken by the change to fix collision in direction to 90. so just confirm that let's go back to the player sprite and change fix collision back to zero yeah look at that we have perfect movement once again but that means we have a problem and the root of it is simply that the fixed collision in direction is not suited to getting us out of an unknown direction of collision find the fixed collision in direction script and i'm going to move it into some free space now just to refresh your memory i've removed the run without screen refresh from this custom block for the moment so if i play scratch cat so that he's colliding with the level like so and then run the fix collision in direction zero script you can see him bounce up and down further and further until he is safely out of the ground but if we change the direction to be 90 then the cat is instead shaken left and right more and more until they find themselves out of the ground but the problem is this what if the way out of the collision is neither to the left nor up but perhaps diagonal what we really want is a fix up script that can find the closest point we can move to that is not colliding with the level so rather than searching back and forth like before a better solution may be to search in ever increasing circles looking for the first point that no longer collides sounds perfect how hard can it be make a new custom block naming it find closest space to with a numeric input of x and another of y that will be the middle of our search circle then add a label max and a numeric input of the same name max this will be the maximum distance we look for a way out of the collision then finally we most definitely run without screen refresh let's duplicate this fix collision indirection script as that will be relatively similar just separate off the repeat loop okay yes we want to record the original direction so we can point back to it once we finished and yes we want to begin looking just one pixel away and then work our way outwards but this point in direction just throw away the der parameter as this custom block doesn't have one of those anyway what i suggest is that we set it to zero we begin looking directly upwards okay cool we're set up so actually put back the repeat loop we'll use the max input variable as the number of times we will repeat around now remove the scripts from within the repeat loop but keep them handy the first loop is repeating to move us on ever increasing circles we'll need a second nested repeat loop to actually move around in a circle testing for collisions we need a compromise here though to avoid doing too many checks and i've chosen 16 points per circle we ensure we always begin at the same point with a go to x y and drop in the input variables x and y then since we are already facing the desired direction we move forward by distance so having moved are we touching anything solid bring back the check touching solid and it's connected if script if we are not touching solid then this restores the direction of the player and stops the script we would be done super but if not then we need to search on so right after the if check turn the player clockwise but by how much well 22.5 degrees actually how did we come to that number simple 360 degrees is a full circle right and we are repeating 16 times so 360 divided into 16 parts is yeah 22.5 great and so after a full circle has been searched we need to grow the size of our circle's radius change distance by one do make sure this is not within the repeat 16 but is within the repeat max stonking all that's left is to restore the original position if we didn't find a way out of this collision go back to the original position using a go to xy with the input of x and y and we stored the original direction in the variable orig so point in direction of rig so we are facing the right way here let me size it into view so this is our fine closest space script excellent but come on let's see it in action i'll position the cat with his feet colliding with the floor and then click the new find closest block bam we appear back on the surface we can test the wall to our side too and wham we move out of the wall to our left that is super but what about the tricky one overlapping both on the bottom and the side oh yeah we get moved diagonally out of the collision that is awesome shall we see it slowed down edit the custom block and disable the run without screen refresh temporarily and here we go now i've sped this up a tad but can you see how we are moving in circles that are getting bigger and bigger until we found a point of no collision it should be noted that usually the amount of overlap is quite small and so the number of checks required are far less than we are seeing here great so this clever script is the real magic that solves all of our platforming woes it's our get out of jail free card and we'll simply make everything work so what are we waiting for let's get it in the game okay so don't forget to re-enable the run without screen refresh if you unticked it click into the player sprite and find with me the define moved by moving platform we are going to remove the old fix collision in direction script and in its place we use the new find closest space 2 block the position is going to be the x position and the y position that's the current position of the player and r so what about the max distance to move what's that about why would we want to limit this so for now stuff in 16 and we'll talk more about that in a second it's about time we smashed the green flag and gave it a test because i am dead excited to see it work firstly jumping on the simple up and down platform oh yeah that is working just as well as it did before which is great news but stop right there what do we want to happen if we are standing right here will we squish through the wall be pushed through the lift or moved out of the way what do you think okay enough of that let's find out right it pushed me back to the left out of the way let's try this again but right underneath now pop it pushed us out the right hand side what if we were under the lift just the edge to start with oh well that was okay but how about right in the middle this isn't going to be pretty hold your breath oh boom there you go poor scratch cat so you remember that number 16 we put in the max value in the find closest space 2 block that was saying that when we collide the furthest we can be moved to safety is 16 pixels well when in the center of this lift there is no safe move within 16 pixels of us so boom game over before we think more about that let's try out a horizontal moving platform click into the platform sprite and switch around the scripts again and let's see jump on the platform and then when we hit the wall yeah perfect we are simply pushed back out of the wall and fall down gracefully this oh bother oh let me try that again oh whoops oh it's okay i got this and there we go yep and i should be able to be pushed along cool this is looking really spot on good then we should revisit scratch cat's demise yes sorry scratchy get back under that lift and we'll work out what's to be done with you now as soon as our fine closest space fails to find a free spot within 16 pixels what we want to do is well sorry to say this but that's game over kitty or at least we lose a life right and we can tell when this occurred by using an if and checking for when touching is still greater than zero then thank goodness we covered losing a life in the last episode because now we can simply broadcast lose life run the project and check that out okay here we go don't panic scratchy it's okay oh not so okay but thank goodness cats have nine lives another go super so how close to the edge can i get away with oh that was about it i'm guessing if you want things to be more strict then you'll have to try using a number less than 16 in your find free space block but brilliant we are almost done i'm just going to finish by trying out a few more platform movement patterns click into the platform sprite so first off don't think you are limited to horizontal or vertical movement only or even just back and forward motion look we can add in a third position with differing speeds let's give that a ride i'm going to have to jump over oh i'll bother okay so that's perhaps something to watch out for if we move off screen then the platforms all reset their movements now we could address that by keeping each platform as a sprite or clone and never stop or start it just hide it when it's not in the current scene or design your level to stop you moving off the screen so easily perhaps like having a roof i'm just going to remove the obstacle for now so you can see it moving around me there so cool but what else can we do let's run over to scene three i'm going to add a vertical sliding heavy door in here scroll to the when i receive change scene we can duplicate this if checking now for a scene of three if we run that then we'll end up with the same moving platform living on this scene too but what i want to do is flip this around and use it as a moving door so delete the second animate block and we can create a new custom block named animate 2. remember we don't tick the run without screen refresh we're animating here make use of it in the new if gosh my scripting area is getting a bit messy i'm sorry about that i'll just tidy this up and put together another vertical script like this you'll need to play with the values to fit these to your level but just let your imagination go wild because you could make any shape move around that you could think of what could you do with this right i bet you could think of some super ideas i just can't wait to see them in this week's studio there that's cool oh no oh i forgot to reset the rotation of the normal moving platforms hold on and there we go we are off and with that my dear fellow scratchers we come to the end of another episode but what an episode i hope you have learned a bunch of helpful stuff in this one so please don't forget to smash that like button and if you haven't already then subscribe to the channel right now who knows what day a video might pop into the notification area and you don't want to be late to the party next week i'm going to be mixing it up with something completely different so watch out for that but by no means are we 100 done with this series yet either i just want to thank all my channel members for their ongoing support and if you are a loyal fan or an educator who uses my teaching then please do consider joining the membership too there's some great perks to be had plus it helps me to be able to make even more videos win-win but until next time thanks for watching and have a great week ahead scratch on guys [Music]
Info
Channel: griffpatch
Views: 68,394
Rating: undefined out of 5
Keywords: scratch coding, scratch programming, scratch game, scratch 3, griffpatch, griffpatcho
Id: Vu_PO5YMDVQ
Channel Id: undefined
Length: 35min 28sec (2128 seconds)
Published: Sat Oct 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.