Clone Collision Tricks | Koopa & Shells | Tile Scrolling Platformer e17

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers i'm griff patch and today wow today we are going to be doing some quite revolutionary scratch coding this is episode 17 of the tile scrolling platformer series but some of the ideas we'll be using today have possibly not been seen before maybe they have but well let me know because we are adding the green koopa troopers and getting their shells to collide with other enemy clones well i hope you've got your brains all fluffed up and ready to absorb i am so excited let's get scratching hopefully you caught episode 16 where we introduced the piranha plant if so you've probably got a good sense of how we're going to begin adding coopers click into the enemy sprite checking out the costumes we can see cooper is sitting here at costume 5 yeah 5 and 6. we'll need these numbers later but our first job is to copy the first cooper costume into the tile sprite this will allow us to place it in the level editor okay cool cooper has a tile number of 69. now often we need to tweak the position of the costume in the editor to make it display in a sensible position if we zoom right out select the entire costume by pressing ctrl a or just drag a box around it and then press the up arrow key four times that should do it there that's the costume done now we need to map an editor key to it click into the editor sprite and bring up the tile key map list remembering cooper is tile costume 69 we can scroll right down and enter a 9 at item 69 yeah all the enemy tiles are being mapped to the number key 9 and koopa is no different do remember to click out to another list row to ensure scratch saves your last edit okay hide the list last step before we can test is to go into the enemy sprite and find the define spawn type custom block each enemy tile is spawned from here so duplicate one of these if blocks and check for a tile type of 69 we can give this sprite a unique name cooper will do what was our enemy costume number five okay enter a costume of five and then for the size width is still 16 and height that's 24 taller than the gumba but shorter than a piranha plant super that's the minimum we need to do to get the enemy to appear in the game so let's run the project right level editor with zero key and then press nine until we find koopa there they are excellent i'm going to pop one right up here no reason why i'm always glad to see the enemy appear though when i click the button and if i start the game by pressing zero there they remain in place this is great news the default action of an enemy is to stay still and turn to face the player we can't kill them and they can't do anything to us either our next goal then is to get cooper walking around the good news is that koopa troopa's basic movement is identical to gumbas so we can reuse lots of code find with me the when i receive move enemy script this script is getting quite long it contains many ifs to handle the different enemy types i'm going to duplicate the gumba if and put it on the end of the script i guess we could have just used an ore instead now that i think about it but never mind change the type check from goomba to koopa so let's see what this tic gumba script does so we have the movement in the x and y and then we set the costume ah okay so this costume 2 is goomba's initial costume number we can't use this for koopa they have a different root costume number i'm going to have to introduce a new variable to help us out name it root costume for this sprite only now where we had this costume 2 stuff in the root costume variable we just need to ensure we've set this variable to 2 for gumbas and 5 for coopers then if we look at the define spawn type custom block you can see when we spawn goombas using set type we are passing in a costume 2 here equally coopers already have a costume 5. so that sounds promising scroll down to the define set typescript below here here we are setting costume to this value so we already had a variable set to the correct value that's true but it doesn't stay on that number for very long and gets changed as we animate the sprite so now we can set root costume to costume as well great in which case let's give that a test to see whether the setting the root costume was enough oh yes here they come let me jump on them oh did you see that when they die they transform into a squished goomba let's add another one down here oh now did you see that the cooper down there didn't try to walk under the block he was too big that's cool so when her cooper is stamped on they obviously shouldn't be turning into gumbas no they should instead be retreating back into their shells this is a problem because we don't have any koopa shells in our enemy costumes but don't worry i have them all prepared for you so save your project now and come and find my tile scrolling tutorial asset project there's a link to it under the video if you can't find it click into the enemy sprite and check out these super new costumes yeah we have the green and red cooper shells awesome just backpack the entire enemy sprite that would be the easiest way to go now come back to your tile scrolling project and we can copy the back packed sprites in it appears magically as enemy 2 and that's fine so i can copy the new costumes into my enemy sprite starting at 26 and working all the way up to 33 excellent work i'll just double check the costumes are present yep so we can delete the enemy 2 sprite to tidy things up so when koopa is stamped on they currently show a squished goomba costume let's find out why in the enemy sprite find the defined tick goomba script since both goomba and cooper are using this now look further down here is the touching mario block and well there you go this is what causes the squished goomba to always appear we need to behave differently for goombas and koopas so use an if else block and check if type equals goomba now only the set type and the set costume are specific to goombas let's take the rest outside the if the add to particles can go below the bounce player can go above as we said type and costume these are goomba specific put those into the if and frame that can go above good this script can now be returned to where it came from under the point direction here so goomba should still work as before let's fix up cooper and switch him to his shell form i'll duplicate the if-else block stripping off those list blocks and we check for a type of yup cooper okay so now things get interesting we need to make up a new type for the shells let's have a type of cooper shell now for the costume we should look that up again yeah it's 26 for the first green cooper shell set costume then to 26. ah but hold on it will make sense to set the root costume now so that we can handle red shells later on one more thing we should do is to set the height variable to 15. a cooper shell is distinctively shorter than a cooper trooper nice nice nice we're getting somewhere we have changed the sprite from a cooper to a so-called cubic shell but our project does not know how to handle such an enemy type yet we must scroll to the when i receive move enemy script and scrolling down again [Music] duplicate another if script for our new cooper shell type but no ticumba script for this one the movement is a bit more customized make a new block naming it tick shell run without screen refresh and pop it under our new if okay all i want to do is test that things are working so let's muddle together a simple script set costume to the root costume we set up a moment ago we could do with some gravity so change speed y by -1 and then we use the move sprite y block to do the job nice that should be enough to let us test that the transition to the cooper shell works as planned so here we go and squish yeah that is really satisfying the transition worked great let's just try that again put some more enemies on the level [Music] and jump on them all ha ha take that great so what is missing it's the ability to walk into a shell and have it spin off away from you now all the interactions are quite straightforward we just need to be methodical first consideration when cooper becomes a shell we want a small delay before they can be kicked we'll give them three frame ticks if frame is less than three changing frame by one and then stopping this script that deals with that right then if we got here and frame is still three then this is a stationary shell so if the shell is not touching mario then again stop this script there's nothing we need to do we're waiting for mario to come along but if we are touching mario it's business time what direction do we want to send this shell off in if else when mario's x is less than the shell's x then mario should kick the shell off to the right so set direction to 90. in the else set direction to minus 90 to kick the shell left okay then we are off this shell should be animating we want quite a fast animation so let's change frame by perhaps 0.5 that will update the costume every other frame next we need to get the shell moving how about we set speed x to uh direction divided by nine now since direction is 90 or minus 90 that would be a speed of 10 or minus 10. the move sprite x block will then move it so we should be in a place to give it a test green flag and dispatcher cooper and kik wow look at that shell go the move sprite spritex is doing a great job moving the shell and bouncing it off the walls too hey we forgot to add the change of costume animation we're already changing frame by 0.5 and all we need is to change the costume because we have already set costume to the base costume up here so we now change costume by and we use a floor to account for the half frame increments and then a mod of the frame to cycle through the four costumes you can see each shell has four costumes in its animation loop let's see that in action and there we go a spinning shell i'm very pleased with that i just wish it would do some damage to those enemies down there with it but i guess first off let's do the easier job and get it to do damage to mario [Music] we can borrow some goombas scripts for this find the define tick goomba and scroll down to find the touching mario check duplicate everything from the paint sprite down and bring it safely back down to the tick shell script i'm gonna pop it at the end here this script handles both mario jumping on the enemy sprite and being killed by it so we do need to handle both of these with a spinning shell as they are deadly to touch but can be stomped on to stop them spinning the first if is for when we jump on the koopa shell let's tidy it up get rid of the point in direction block and oh wow we can scrap the entirety of this if block and below goodbye that's a lot simpler looking let's give it a test [Music] get the shell spinning and now i just need it to hit me oh oh gosh i accidentally stamped on it well that's good in that it shows that that works too oh except jumping on it again seems a little off there boom that's it quite capable of putting an end to one of my lives that's good let's see that jumping glitch again bounce bounce bounce well that's awkward not to worry i know what it is when we touch a shell and begin it moving for the first time we don't have any cool down time before a second touch can stop it moving again let's address that by adding an and to this condition and checking that frame has to be bigger than say 10 before we can both die or stomp on the shell this is doubly useful as it's important that we can't be killed by the shell right after it has been kicked anyhow so here we go [Music] kick off the first shell one jump two jumps oh pants it came back so fast still that was looking good again bounce kick bounce and kick i just need to oh no not again i'm not timing this so well [Music] oh seriously why is it doing this to me oh well it worked we all saw it okay only one thing left to do to get these shells to kill the other enemies but hold on we have a problem the shell is an enemy sprite clone and the other enemies are yeah also enemy sprite clones the problem with this is that firstly try and add a touching block and look for the enemy sprite in the list nope it's not there why because scratch thinks we won't want to detect collisions with ourselves hmm well actually we do because we are using clones right well luckily this is easy to get around we just create the sensing block in another sprite and set it to touching enemy and then copy it over haha job done but that's not the end of our problems we want only the enemies that are spinning shells to damage the other enemies right how can we find that out from a touching enemy block it only tells us that we are touching an enemy not the type of enemy or the other details about it well this has been a problem as scratchers have had to suffer for as long as we've been scratching there are many solutions of varying complexity and successfulness the most obvious is to use lists and record positions of sprites in there then use this to do the collisions instead mathematically but well i have a rather novel idea that avoids all of that which i think you will rather like before i tell you how we achieve it because that in itself may not be obvious let's take a look at the theory of what i want to do we have many clones of the same enemy sprite some are deadly to other enemies and some are not what we want to do is have all the normal enemy clones be able to detect if they are touching any deadly ones that is the goal here if we use the sensing block from a normal enemy it will report true if it's touching a deadly enemy but it will also report true if it's touching another normal enemy too and that's the problem now interestingly the sensing block only detects sprites that are visible and well that makes sense right so if we were able to hide all the normal enemy sprites then that would only leave the deadly ones to be detected so far so good the next step is to one by one make a normal enemy visible and then ask if it's touching an enemy if not let me hide it again and go on to the next now if one is touching another enemy then it has to be a deadly collision since all the other normal enemies are still hidden and then before the game loop finishes we quickly show all the enemies once more so to the player of the game they were not even aware that we were hiding things at all so have i sold this idea to you perhaps that seems pretty straightforward but the next problem is how on earth do we get scratch to let us do this all within a single screen tick the reason it's hard is that we need great control over the order in which we hide clones check for collisions and then re-show them again luckily as i've been teaching you on and off throughout these tutorials we do already control the order of script execution in the main game loop here as a recap our main game loop this forever loop only contains broadcasts no weights here at all and as such the loop will repeat over and over 30 times a second you can imagine that the player of your game will see the screen update once all the above broadcasts have had their chance to run what is important is that the broadcast receivers through all the sprites of your project will run in the order that we broadcast them here so all the when i receive check control scripts will run on all the sprites before the move player script will start and these will run before the move enemy scripts the move player after enemy and finally the position tile scripts it's only after these last scripts have had their turn that scratch will refresh the screen to show where your sprites ended up the magic of this is that say in the move enemy script we could hide the normal enemy sprites but if we then show them again in the when i receive position tiles then the player would not even see that we had hidden them and importantly when the move player after enemy script is running none of the normal enemies will be visible will they now isn't that something so make sure we are looking at the enemy sprite one small issue we have to address is that we don't keep track of which clones are visible on the screen this is important to do so that we can restore things back to as they were intended when hiding and showing the enemies as such make a new variable naming it visible for this sprite only best to initialize it in the green flag script so we'll set it to the empty value also we need to find everywhere we did a show or hide and set this visible variable luckily that wasn't so many places find the define set type script here's where we show the sprite when the game starts up set visible to one and throw away this worthless show block now notice that we are cloning this sprite here at the bottom well to make things easier let's then assume we can set visible back to the empty value after that since the clone is the sprite that wants to be visible not this one cool the only other place we show the sprite is in the define tick life script this is for making the life power-up appear from behind the mystery block again stuff in a set visible to one and remove the old show block the reason we remove the show blocks is that making these visible here could cause the sprites to be mistaken for deadly clones and we don't want that well if we give that a test we should find that no enemies are to be found ha that makes for one easy mario game right serves them right for constantly killing me earlier okay let's start at the end we'll add in the script that will make all the enemy clones visible again when i receive position tiles this will let us test the sprites will come back if visible is greater than zero then show only the clones with visible set to a number greater than zero will now be shown so click the green flag yeah the enemies strike back i'm especially pleased to see that oh man as i was saying it's good to see the mushroom appearing correctly from behind the mystery block our next goal is to hide all non-deadly normal enemies and we would do this in the when i receive move enemy script but we want to ensure we hide it after it's done all its other work the easiest way to ensure this happens is to split off all the scripts under the initial if check and make a new custom block naming it move enemy when without screen refresh then all these scripts can go in there and we'll use this block right away there and of course we want to hide the sprite but and here is something we have not talked about we only want to hide non-deadly enemies how are we going to identify these we could use the enemy type but that would mean a lot of if checks instead we will reuse this visible variable a value of 1 means it's a normal visible sprite and a 2 will mean it's an always showing deadly sprite so i rotate in cooper shell for example so only if visible equals one a normal enemy then hide it that will leave the deadly enemies visible sweet let's test that to ensure it's all still hanging together [Music] right so deadly enemies find the define tick shell script here is where we make the shell spin off in a given direction after it's been touched so it's here we need to set visible to the new value of 2 it's now a deadly enemy conversely when it is stomped on a second time it stops moving and we can set visible back to 1 restoring it to a normal enemy state and now there's only the detection of collisions to code up we are going to code this in a new when i receive move player after enemy block check first that we are not in the editor editor is less than one and that this is a normal visible enemy visible equals one right so this will be a hidden enemy at present so we need to show it then we check for collisions using a touching enemy ah problem scratch doesn't allow us to do that does it we must go into another sprite scripts add the touching enemy block from there and then drag it into the enemy sprite the old touching block can be removed and then click back into the enemy sprite and drop it into our if bit of a work around but hey right perhaps we'll just delete this clone for the time being that will be enough to show us that it's working and then hide this enemy sprite again afterwards this is really important otherwise if left on the screen other normal enemies will be able to see it and think that it's a deadly enemy that would break our clever process wow that should be it then it's time to test first we need a cooper i select this one off goes a shell yes it destroyed the gumba and ticked off a cooper too perfect now just to check i'll try placing a shell down here and i'm seeing if goomba could walk safely over it yeah this is great and now boom this shell is activated and the enemies stand no chance no chance at all and neither do i sadly all i want to do then before we end this episode is to give the dispatched enemies a better end animation luckily we already have the required enemy flipping code we use it when we head but a block under an enemy let's find the script it's called define check flip all we need is the bottom scripts here from the set speed y to 14. i'll make a new custom block flip and replace the scripts with this new block now scroll back to our when i receive move player after enemy bit of a mouthful and replace the delete clone for the flip block that's it run the project let's go okay jump up here and kick the shell yeah there we go the goombas flip upside down and the cooper follows shortly after brilliant that is working exactly as planned who would guess the tricks that we've put in place behind the scenes to get this working i love it hmm i wonder if we can kill off a piranha plant with one of these oh wow yes we can ha not sure it should work quite like that or even look like that but hey it's pretty cool in mario there are further mechanics to these shells like being able to pick them up and throw them also not so difficult to add is that they should despawn when they go off screen i bet you could figure that one out without me right let me stick a load of gumballs in here go go cooper shell oh yes yes yes that's awesome oh wait that reminds me the shells are supposed to also collect coins as they move along well go on we have time just for that click into the mario sprite to find the define check around player at xy script this is a simple script at present that just collects coins so it is perfect for us just drag it into the enemy sprite then returning to the enemy sprite i'll just tidy it up a bit and then find the define tick shell script scroll right to the bottom here and we'll drop in a check around player x y block checking at the enemies x and y variables we should just make sure we have the coin sound ready to play too back into the player sprite and in the sounds tab copy the coin sound into the enemy sprite too and that should be it run the project for the last time then here we go jump on a cooper and let that shell fly it goes straight through the coins and even our coin counter goes up absolutely awesome well i think we can have a lot of fun with this we've covered some exciting new scripting ideas here and i hope they might open your minds to some amazing new scratch project ideas if it does then drop me a comment and let me know all about it this is the end of the video though if you've enjoyed it then smash the like button and don't forget to subscribe to the channel you're pushing ever onwards towards that hundred thousand subscribers and you can be one of them plus you'll then get notified as soon as my next video lands if you want to support the channel further then you can become one of my paid members who do get extra perks like priority replies and comments early access to videos and even the scratch projects themselves wow so thank you for watching and until next time scratch on guys
Info
Channel: griffpatch
Views: 49,140
Rating: undefined out of 5
Keywords: scratch coding, scratch programming, scratch game, scratch 3, griffpatch, griffpatcho
Id: UsBFARVXOIM
Channel Id: undefined
Length: 30min 18sec (1818 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.