How-to code ONE WAY platforms in Scratch - e13

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers thank you for dropping by today we are back talking about how to add jump through platforms to our sprite based platformers you know just what i'm talking about right the main premise is jump through platforms act just like fully solid platforms when you drop onto them from above but from below it's another story you can jump right up through them almost as if they were not there this can lead to all sorts of super creative level designs not only giving us a bit more variety to the scenes but since you can't drop back through these platforms we can make one-way entrances to certain level areas or even construct ladders so cool what else do you think you could do with these let me know in the comments below before we get into coding let's look a little deeper at these platforms to understand exactly how we are going to code them up here we are above the jump through platform this platform will be part of a completely new sprite so that we can detect when we are touching it gravity pulls us down and boom we are touching the jump through platform sprite now what determines whether we are blocked by this platform is the direction we enter the platform collision from above or below so at first guess we might consider using the player's speed y to decide this in other words a negative downward speed would cause the platform to act as solid sadly though if we consider the case when the player is running up an inclined drop through platform here the player's speed y is positive upwards and we obviously still want this platform to count as solid so speed y is not enough okay then so instead we perform a second collision check below the player if we continue to find a collision then yes the ground below is solid but if we were approaching from below then the second check will find no collision and this means we can treat the platform as non-solid excellent so are we done no not quite what happens when we jump up into a non-solid platform but don't fully pass through instead changing direction and dropping back out again we certainly don't want this to then trigger as a solid collision collision should only occur as we enter a platform that is go from not gliding to colliding well that's easy to solve we just need to keep a record of when we enter a non-solid platform and reset it again when we leave new solid collisions are then only triggered when we are not already inside a non-solid platform brilliant i think that's pretty much all there is to it so what are we waiting for guys let's get scratching so i'm opening up my project from where we left off in episode 12. but just to be sure our project is safe save this as a new copy for this is episode 13 yeah let's go my first scene hasn't got much room to play with so i'll just make some space click into the enemy sprite and to stop the enemies spawning at all i'll surround the entire scene one spawning scripts with an if block since it has no condition the scripts within won't run sometimes that's better than just deleting scripts if you might want them again later i'll just run that and yes the enemies are gone from the starting scene then clicking back into the level i'll just edit the costume and pull the main slope back a little great lots of space to work with okay so i'm going to add a new jump through platform just here we begin by duplicating the level sprite naming it jump through or jump through platform if you like then i'll delete all the scene costumes except scene 1. our new platform can be a simple rectangle i'll drop it in here like this before removing all the other parts of the scene that are not drop through platforms to indicate this is a special jump through platform we can make it a lighter color than the other platforms in the scene then let's smash that green flag and take a look okay the platform is there all right but what happened to the rest of the scene do you notice the nice outline and shadow is now only visible on the new platform what gives look at the change scene script in the jump through platform when the scene changes we erase all the pen drawn outlines and shadows and then draw the new ones but we do just the same in the level sprite too and that is the problem the level draws its outline and shadow and then the new platform sprite comes along and erases a level's drawing and draws its own instead but we want to keep both drawings the problem is that the erase all is done as part of this change scene luckily for us though we already have an event that is broadcast right before the change of scene in the level sprite when i receive about to change scene and simply move the erase all block under that event instead in case you are just wondering how i moved that one block just then it's a scratch add-on feature that i'll go into more in another video so to clean up click back into the jump through platform sprite and remove the erase all block from the change scene event excellent now the scene should be erased in advance before both change scene events draw their outline and shadow effects one thing i am going to tweak is to reduce the amount of shadow under the jump through platforms compared to the fully solid ones under the stamp shadow script change y by only negative 10 and then back to 10 afterwards and there subtle but really cool now it's not quite all sorted yet if we wander on scene 2 ah we've seen this problem before the platform is visible here as well that's not right can you remember when we had a similar issue before click into the danger sprite remember this special blank costume trick if we peek in the scripts we are cunningly setting the costume to the blank costume before we try switching to each scene's new costume and then if the scene doesn't have a matching costume the previous blank costume remains in place and that's just what we want a blank costume okay then we'll do just the same in the jump through platform sprite add a new blank costume naming it blank and then in the scripts under the change of scene event switch costume to blank before trying the new scene costume shall we give that a spin scene one is good and scene two perfect the jump through platform is gone yay right down to business we'll begin by making these new platforms fully solid click into the player sprite and find the define check touching sprite script this is responsible for all collisions with platforms in our game engine to make introducing the jump through platforms easier i'm going to rearrange this script just a little i'll swap this if else for a simple if block if we are touching anything solid then set touching to one this is a certain solid collision then there's no need to consider jump through platforms in this case so we stop this script literally then under this if we restore the set touching to zero okay that change should have no effect on how this script behaves but now we can drop a new if else block below to capture the jump through platform collisions check if we're touching the jump through platform sprite and then as i said to test this just set touching to one to make it act like any other solid platform cool smash that green flag and yeah we've got ourselves a nice solid platform i can stand on top of it and also jump and smash my head on it from below okay so thinking back to how i said we'd code this up we said we'd need to keep track of when we were within that is passing through a jump through platform for this we'll make a new variable call it jump through for this sprite only so one thing we can be sure of if we are not touching the jump through platform that'll be the case in this else then we can set jump through to zero great when jump through zero we are not within a platform then excellent we can use this when we detect we're touching a jump through platform we then use a new if and check if jump through is less than one what does this mean it means the script in here will only run if we are touching a jump through platform for the first time that is we're entering a new platform okay well once again to test that pop the set touching to one back in there and hit the green flag so no change yet to allow us to enter the jump through platform we need to know if we are entering from above or below for this we are going to check for a collision just below the last collision change y by negative six then we want an if else block if we are touching the jump through sprite then yes we do set touching to one as we are traveling down into the platform and that's not allowed but if there is no collision below us then we are traveling up into a platform so set jump through to one recording that we are beginning to pass through a jump through platform finally change y back up by 6 to restore our sprite's position to where it was before we shifted it this whole script can be dropped beautifully into the awaiting if block to complete that little bit of coding magic and oh my word i am pumped to give this a try so on top first yep no problem there but how about from below here we go oh wow look at that what do you know we went right through just as weird hoped amazing hold on what the how come i can't collect these apples anymore player level two how did my sprite get renamed as that that must be a scratch bug i believe it's caused by using the control zed undo shortcut i've seen it before but i think just renaming the sprite back to player should fix that hold on phew yes back to normal gosh i hope that gets fixed soon okay let's test some more does this code still work with angled jump through platforms if we approach from above can we run up a slope yes we can that's awesome and from below perfect still holding together really well so what about with two platforms angled different ways but perhaps running close together testing now jump on the first and straight on to the second looking good very good and if we walk into the narrow ah okay so did you expect this we find our path is blocked not perhaps what you would want why is this happening though even our jumping is prohibited if i stop the project and bring up the player's main hitbox costume hitbox version 2 then look at how we are currently colliding since to walk right we collide both with the bottom and the top platforms at once the only way we could enter either of these platforms would be if we were considered to be entering from below but since we are touching both the second jump through platform check below the current position implies that we are trying to drop down through a platform so it's considered solid both the one below and the one above the truth is though we only ever want to consider our feet collisions for jump through platforms perhaps we can improve this by changing our hitbox to exclude our head yes i like this plan so duplicate the hitbox version 2 costume and change its name to be hitbox feet awesome then with the shape tool select the top points and click the delete button to create a much smaller hitbox super that's tiny see look that will easily slip through between these layered platforms so back to our code in our ever expanding script it's these touching jump through checks that need to use the new hitbox but we need to be careful to only switch to this costume when checking for normal hitbox collisions otherwise we'll mess up the feet leveling code and all sorts so we'll need an if block and within this we can switch to the costume hitbox feet what we'll look for is the current costume name checking that it equals the normal hitbox costume what was that called again here it is hitbox version 2. we need to be absolutely sure to get this name correct so i'm copying it to the clipboard and then paste it back in the comparison then we'll make sure to switch costumes before we check touching jump through okay nearly there one more problem is that if we've changed the costume we should make sure to change it back again at the end of this script tell you what let's make a new variable name it hit costume for this sprite only and before we do our little costume check up here set hit costume to the sprites costume name and since we now store that in this variable drop the variable into the comparison below and of course now that we have a record of what the costume was before we changed it at the end of this script we can just switch the costume back to hit costume variable eezy smash the green flag no problem then jumping up through these platforms as before but can we walk through the smaller gap yes we can much better that is looking very smooth now this is almost perfect but you may find that there are still minor issues when jumping towards platforms from the side the reason being that our wall jumping and sliding mechanics are all still active even on jump through platforms let's add in a vertical platform so that you can more easily see what i mean okay so we can jump on this as before but coming from the right ah there you go we wall slide if this is a jump through platform we really shouldn't be blocked here should we it feels wrong after all the rules about entering a jump through platform are that it is only solid from above and this is from the side right so probably should not be treated as solid the second hitbox check below the player's collision is responsible for making this distinction and because it only looks below then it finds a collision even on vertical walls we want this to not be the case so a solution i propose is to make the second check use an even narrower hitbox than the first tiny costume then we should only find a collision if there truly is a platform directly below us and side collisions will be excluded cool then find the hitbox feet costume and once again duplicate it we can keep the name feet 2 why not then we select the hitbox and while holding down the alt key drag the side of the shape inwards to roughly half its size we want to ensure we do all this without moving the actual shape that's so important with these hitboxes now back to the code since we are changing costumes again we again have to ensure only to do this when replacing the standard hitbox so duplicate that if check that switches costumes and make sure to leave the original one where it was this time we'll switch to the new hitbox feat 2 costume drop it in right before our second touching jump through platform check just after we changed y by negative 6 checking below our player's feet splendid this is exciting hit the green flag and let's take a wander so jumping up here ah that's interesting so you'll notice that this fix does nothing to prevent us being stopped by a wall if we are still standing on a platform that's inevitable but this is looking better we can now jump right through the problematic platform from the right man this is fun we can do amazing things with this i think so what's left i guess something to consider is how do we want enemies to interact with these platforms hmm shall we have a quick look ah so do you remember this safe zone sprite this prevents an enemy walking into this area i should move it a little bit since the level has now been changed i wanted to stop an enemy walking off the top of this platform here now click into the enemy's sprite to position a new enemy click the show sprite button here and drag them on the stage to where we want them to appear and i'll still keep these old scripts here just in case i want to return to them but i'll duplicate a new spawn block the new position is x 101 and y of 50 or 60ish that'll do run the project and okay so you can see our enemies are completely unaware that these jump through platforms even exist and that is no surprise because if we find their define move down script they are only looking for collisions with the level sprite shall we fix that we just need an or operator and now check for both touching level or touching jump through yeah but hold on we also have to modify the move left right script below that to continue the pattern just duplicate the inner if check and we can now throw away the extra stop this script we only need one extra touching check so remove the ore and we check for not touching jump through okay it's testing time will the enemy now see the jump through platforms yes they do thank goodness of course they don't have all the extra clever coding we granted to our player but most of the time these enemies don't go jumping through platforms anyway but it wouldn't be beyond us to extend our code to introduce that feature too if we want it so we have all our code in place all that's left for us to do is have an absolute blast re-architecting our levels to make the most of this awesome new feature now i could have a one-way ladder up to the room above and then a special escape route back down to the level below i really like that idea i'll just give that a play through but actually no the wall jumping was much more fun what i'll do is keep the original wall jumping section but with the one-way platform at the top now the enemy can patrol fully back and forth and we save the player from that death fall to the spike on the climb up nice but since i like those stairs how about we try that out in the final area to the right of the level i'll remove the old wall jump since we already have a wall jump area and replace them with a ladder up to the section above yeah let's give that a play through here we go collect the huge apple and jump up onto the first platform ah whoops can i actually make that ah right okay i can't make it whoops hold on one extra platform should do yeah we made it you only find these things out by testing right brilliant and we are at the summit and we are also at the end of this tutorial i do hope you've enjoyed watching if you did then please smash the like button and subscribe to the channel so as not to miss the next exciting video and so you should because we've got exciting things coming up and i'm so stoked for the brand new series on coding an rpg engine woohoo it's going to be crazy cool just have to ensure i have it all planned out first it's been fun interacting with you in the youtube community especially those who have joined my griff match channel membership here as it comes with the priority comments so we get to have a lot more direct contact that way which is nice a shout out to my more active members like jensen daniel cassidy david was a goo sorry if i pronounced that wrong honest remix super and shark just to name a few and my awesome patrons creeper craft 55 male 18 35 and [Music] i think that probably sums that one up man you are the best thank you so much well that's it thank you for watching have a great week ahead and scratch on guys
Info
Channel: griffpatch
Views: 153,498
Rating: undefined out of 5
Keywords: scratch coding, scratch programming, scratch game, scratch ai enemy, scratch 3, griffpatch, scratch, block coding, scratch tutorial, how to make a game in scratch, scratch tutorials, scratch games, coding, scratch platformer, how to make a platformer in scratch, scratch tutorial game, scratch coding tutorial, scratch platformer tutorial, scratch coding games, jump through platforms, one way platforms, semi solid platforms, scratch one way, scratch jump through
Id: ytB0oaJcjYU
Channel Id: undefined
Length: 23min 6sec (1386 seconds)
Published: Mon Jun 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.