Tile Scrolling Platformer | 7. Drop Through Platforms

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers collectible coins awesome jump through platforms and per level spawn points i'm griff batch and welcome back to our series creating a tile scrolling platformer in scratch wow are we at part 7 already but there's so many other exciting game features yet to cover we've got some really good content today though all centering around the concept of non-solid tiles let me show you why this is so important if we quickly lay down some coins here on my level then as you are probably more than aware of we can currently jump up and stand on top of them why is this well it's because every tile in our level is treated as fully solid well except of course for the background tile costume 2. now if i run over here to the right i have these blue tiles these are supposed to be semi-solid platforms that i can not only run and jump through but here's the fun part they are still solid to stand on but for starters let's get mario collecting coins so digging into our scripts hold on folks something's different oh my gosh look at these cute scratch blocks oh man what is going on and you can tweak their ears oh yes i happen to record this episode during the scratch april fool's day update of course i'm so sorry if you don't see these beauties on your project the event only lasts for a few days a year so we'll start by making a new list named tile shape for all sprites this will work a bit like the tile keymap list we made for the editor sprite we'll be adding one row for each costume in our tile list we'll click then into the tile sprite so that we can see our tile costumes and their numbers i'm going to invent a code to represent the shape and solidness of a tile these first two costumes are easy they are empty tiles so i'll simply leave the first two rows empty for costume three this is a fully solid tile so i will use the hash symbol to represent it i can see that costume 4 to 8 are equally solid so those two can be hashes and scrolling down the costumes i can see 9 and 10 are also solid now here we are getting a bit more interesting these first blue platform tiles are completely non-solid so i'll enter no value into rows 11 through 16. but scrolling down further i can see costumes 17 18 and 19 are the top costumes for the blue platforms these are only solid when being walked upon so i'll represent them with an equal symbol and then costumes 20 to 23 are again solid blocks so put in a hash for these the final four costumes are coins and these are again to be non-solid so i'll make sure they are empty rows in the tile shape list excellent that's all set up let's make it do something click into the mario sprite and the code tab we'll need a new variable name it tile shape for this sprite only find our define key tile at x y block this is where we get all the information about a tile at a given point on the level at the end of the script the variable tile will be set to the tile costume that was found at x and y we'll add to this the shape of the tile so set tile shape to item tile of tile shape next find define fix point collision at x y here is where we are using the get tile at xy block we'll split off the scripts below and add in an if tile shape equals the empty tile that is we've touched a completely non-solid tile then we stop this script this makes the following check for a tile being greater than 2 completely redundant now so let's get rid of that the rest of the scripts can be brought back in under the new if let's give this a quick test okay good the floor is solid i can walk on it and nice i am able to walk through the coins that's a great improvement hold on a sec some of you may find your mario sprite is sitting behind the coins at this point if so then come out of full screen mode and simply drag the mario sprite with your mouse and when you release it it will be restored to the front once more now come over to the blue platforms so great i can already walk in front of the platforms but as of yet we can't jump up through them the top rows are solid tiles though which is a start but we're getting ahead of ourselves now let's go back and revisit these coins and make them collectible scroll in the scripts to the define check around player block this script is used to check what's going on around a player at present it is just looking for us falling off the screen we'll add in a check for collectibles so let's get tile at x y and then check if tile equals now which costume was the coin ah 24 so if we are touching a coin 24 then we'll replace item tile index of tile grid with two that will replace the coin with an empty tile we can give this a test now this is promising the bottom coins are collected but our reach is not quite high enough to get those upper coins now check that pressing the green flag causes the coins to all reappear you see it was important to wait until we had added level saving and loading before collecting coins had we collected a coin before saving the level the coin would have been permanently deleted when collected and would not have returned okay let's extend mario's reach to include the coins above two make a new custom block named check around player at x colon with an input of x a label of y colon and another input of y we can now substitute this new block for the get tile x y and its following if check move the x and y variables into the new block and then plug in the x and y input variables from the define block into the get tile x y now we can change the y variable to be y minus eight to check just below mario's center and duplicate the whole check block and change the minus eight for a plus eight to also check above center two to be honest you may consider changing these values to be quite a bit bigger like plus and minus sixteen anyhow let's give it a quick test yep that solves that but like i say you may want to experiment with slightly larger values so that you can collect things higher and lower down how about we add a quick coin counter to keep track of how many coins we've collected make a new variable coins for all sprites then we'll change coins by one after removing the coin from the level if you want to add a sound then this would be the place to do it all we need to do now is set coins to zero at the beginning of the define game loop to ensure the count is reset when the level first begins giving this a test and that works great the coin count shows how many coins we've collected now for something much more exciting these jump through platforms here's mario jumping up through a platform he never collides with these while traveling upwards but when he falls back down his feet can not pass down through the top of a platform tile we can detect this type of collision by starting with the variable mod y that gives us the distance from the bottom of the tile to the collision point and then subtract fixed dy which tells us the distance mario last moved to get to where they are now if the combined value is above the tile bounds then we must have moved down across the line to get to where we are now and a collision should occur so first we are going to edit the fix collision at point block adding a new label of part colon meaning which part of mary's body we are checking and an input also named part for the two usages of the fixed collision at point blocks where we are checking for y minus height enter the word feet as the part now scroll down into the define fixed collisions at point block itself we need to move the set solid to 10 below the set mod x and y blocks bring in an if before we set solid to 10 and check whether the tile shape equals an equal symbol that is whether we've collided with a jump through platform now we know in here that we're colliding with a platform at this point but we only want it to be solid when we are passing down through the top of it start by adding another if and add an ore block then not part equals feet this will be true for any collision that is not our feet allowing us to quickly rule out non-feet collisions now we also check for mod y subtract fixed d y is less than 32 if this case is true then we did not pass through the top of the platform either so we stop this script which means that any collision that was our feet and originated from above the tile will count as a collision so i can't wait to run this project and give this a go legging it now over to the blue platforms and jumping up i pass straight through the platform and land solidly on the top wonderful and now i'm just testing that i can land on each platform and also jump within a platform without any strange collisions nope i'm really happy about this it is working great now many platformers give you the ability to drop back down through a platform by holding the down key in my observations this may not be a general mario dynamic but i will still show you how to do this in case you want it in your game all we need to do is add another if checking whether down of controls is greater than zero that is the down key is pressed then stop this script so no platform collisions are reported when the down key is pressed too simple right let's just test that little tweak i can still jump onto the platforms and pressing the down key woohoo straight down through the platform that's really fun okay our last task for this episode is to invent a way to define a spawn point for each level that is the position where our player will appear when the level begins to start with we need a way of placing the spawn point from within the level editor for this to be possible we need to add a mario costume to the tile costumes so that it becomes placeable i'll just drag the first mario costume into the tile sprite like this now moving into the tile sprite here is its costume number 28. we'll have to remember that number for later 28. i also need to reposition the mario sprite because it's currently centered but i want it to be positioned standing on the floor of a single tile square i press ctrl a to select the costume and then drag to reveal the center point of the costume editor i want mario's feet to be positioned eight pixels and one more for the outline below also by holding shift whilst dragging the costume snaps to the horizontal or vertical position keeping things aligned nice next we need to map an editor key to the mario spawn costume tile click into the editor sprite and we'll make the tile key map list visible mario is costume 28 so at row item 28 of the list we'll enter a key mapping of four remember to click out to another row to make sure it saves that change and it's testing time i'll enter the level editor and press 4 until i see mario appear cool then i can place him where i want the player to spawn okay so i can see one potential issue i can place more than one mario this will cause us problems so let's stop that from being possible so still in the editor sprite find the when i receive move player script and scroll down to the bottom before we replace the tile on the screen add an if check whether brush equals 28 that's the mario costume remember make a new variable named found index for this sprite only then set it to item hash of brush in tile grid if another tile exists in our level of the same type as brush which in this case is the mario costume then this will find it lastly we replace item found index of tile grid with two the empty tile you should rightly ask here what happens if there isn't another mario on the level well in that case found index will be set to zero and happily replacing item zero of a list does nothing at all so we are safe to leave the script like it is without having to worry should we test again now when i place a new mario the previous one disappears that's neat and so all that's left is to actually get mario to spawn at this location switch back to the mario sprite here in the when green flag clicked script when the game starts we do the initial setup which loads the level and then we reset the player this is the first player spawn create a new broadcast and wait event named level hyphen start game loop hyphen respawn that's a long one we can then bring in an event receiver for this event and move the reset player and game loop blocks down into there okay we'll make use of this more in a moment in the meantime bring in a when i receive block and we'll make use of the existing level done loading event this is called just after the level is loaded and before the game loop starts so is a great place to process the spawn point we'll need a new variable named spawn index for this sprite only and set it to item hash of 28 in tile grid that is the index of the mario spawn point in the tile grid okay so now that we've found the spawn point if editor is less than one that is if not in editor mode then we replace item spawn index of tile grid with two to ensure the spawn point is no longer shown in the game we can give this a quick test notice right away that the spawn index has been set so the spawn point has been identified also note that mario's tile at spawn point is only visible when we are in the level editor so now to get the spawn point to work find the define reset player script and we'll check with an if else whether the spawn point is greater than zero a value greater than zero indicates that there is indeed a spawn point i'm going to duplicate the set x and y blocks into each side of the if and also below the f2 that's right three pairs so this top pair is the most interesting we want to change the spawn tile index into an x and y position on the level we haven't done this translation before having only gone from the xy position to an index luckily it's not too complex start by setting x to the floor of and then drop in a divide block on the left side of the divide we have spawn index subtract 1 and on the right we divide by grid height this will leave x set to the grid column of the spawn index to find the grid row set y to spawn index subtract 1 all modded by grid height okay so because we are now working in grid tile positions we can change the else condition below to default to spawn at tile 3 by 3 rather than pixel 100 by 100. this only happens when there's no spawn point set and then finally under the if else we convert back from grid location to world location by multiplying each of x and y by 32 and then adding an offset the x is offset by half a tile to the center of mario the y is offset by height so that he is standing firmly on the floor now while we are here in the reset player script let's just tidy up a few things we'll make sure to reset the camera back to the player's spawn point by setting camera x to x and camera y to y and also we'll importantly remember to set speed x and speed y to zero we should have done that from the beginning lastly pop in a point in direction 90 so that we are facing the right way and we'll place it just before the move camera block cool we can test this ah spawn is working well i certainly appear to have spawned in the right place and if i move away and click the green flag then i spawn straight back up here yay so how about if i enter the level editor and place the spawn point over here instead press the green flag i am now spawning in the new location okay so is there anything left for us to do well just one thing when we use the level editor to switch levels we are not taken to the spawn point the same applies to creating new levels luckily this is quite easy to add as we already have everything we need click on the level editor sprite and find the when l key press script at the bottom here we just switch the broadcast to use the new level start game loop respawn event remember i said we'd make use of this again we do the same in the when r key pressed script below this too changing to broadcast and respawn event there's just one more script to update if you can find the when zero key pressed we need to add a broadcast and weight to level done loading just before starting the game loop this ensures we pick up the spawn point when exiting the level editor and that is it we are done running the project and i can test entering the level editor and pressing the l to switch levels if i say i want to switch level 1 yes that's the one i'm on right now then the level reloads and look i am scrolled over to the spawn location as intended cool now to level 2. no spawn point was set yet so i appear at tile three by three let me set a new spawn point on this level too now exiting the level editor keeps me at the same position but using the level switching key for level two spawns me up here at the spawn point yay and back to level one yep that is amazing i'm really pleased that we're finally getting these much requested features into our game i've been honored to see quite a few projects being built by scratchers following along with this tutorial series and they are looking so good i hope perhaps we might be able to see some of them together later on when they get shared that would be so cool now what should we look at next episode feel free as always to drop me your suggestions under the video but i've already been asked a lot for either off screen enemies power ups sloping tiles or moving platforms of course i can probably only fit one of these in at a time so yeah let me know what you think in the comments below if you've enjoyed this tutorial then please smash the like button and don't forget to subscribe to this channel to avoid missing my next exciting video until then thanks for watching and scratch on guys
Info
Channel: griffpatch
Views: 67,291
Rating: undefined out of 5
Keywords:
Id: KljFDETaAiE
Channel Id: undefined
Length: 21min 13sec (1273 seconds)
Published: Mon Apr 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.