My First Game - DnD - Attacking & Collisions - Space Rocks (Part 3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the third tutorial on making space rocks with drag-and-drop in game maker this time we're doing attacks and collisions now you know game we're going to have two collision cases so one is the player colliding with the asteroids and the other is the players bullet colliding with the asteroids we haven't made the bullets yet so let's concentrate on the first one so in game maker the way our standard collisions work is that one object checks if it's collision mask or bounding box is overlapping with that of another and this collision mask is something that we set up in the sprite of the object so let's come over to the ship sprite so over here is the collision mask and by default it's going to be set to a rectangle that covers the entire right but there are some other options so we can set it to be an ellipse or a diamond and we can also set the mode to be manual so that we can control the dimensions of the mask and we can also make it precise collisions which basically means the collisions will be pixel perfect so they'll check for each pixel individually and you might be wondering why don't we just make everything precise why there are even options for rectangles and ellipses well collisions tend to be among the more costly computations to make in game development admittedly modern computers are extremely fast and you're only going to get serious performance issues if you have several hundred collision checks or more but it's a better practice to opt for efficiency when we can and rectangular collision checks are much more simple computations to make then checking a circle and certainly more than checking every single pixel which is what a precise collision check does so let's just make shapes that approximate the shape of our sprites so for the ship object we could just do a diamond and we can make the mode manual and then distort this shape so that it matches the ship sprite so generally speaking even if the hitbox doesn't cover the entire sprite especially if it's the players fight they aren't really going to mind if the tips of the sprite don't cause a collision because it kind of works in their favor but what will feel unfair is if you have a rectangle and the very out size of the sprite causes a collision where the player saw that it clearly didn't but this often comes out and playtesting so feel free to have a look at some of the different shapes and do what feels good for you so we set the ship's collision masks let's go ahead and do the asteroids so for these I think a pretty good shape for them is going to be an ellipse and I might actually just keep that at automatic but you can fit it a little bit better if you want all right and now let's actually program the collision event and this is where we should consider one more thing should I have the asteroid objects checking for a collision with the player object or should I have the player check for a collision with the asteroids well like I said before collision checks can be costly so we should go with the option that will result in the fewest number of collision checks so since there could be tens of asteroids in the room at any one time but just one player objects it's going to be more efficient for one player to check for a collision with multiple asteroids and the same is going to go with the bullets when we get to those so let's come over to the ship and there is a few ways we could do this so there are collision blocks that we could use right here in the step event but there is also a dedicated collision event that is triggered by a collision with an object so let's go at event collision with the asteroid so now what do we want the player to do once we've collided later on we'll probably be doing stuff like deducting lives and perhaps the score but for now I'm just going to make the player destroy itself so I'm just going to type this into the search destroy and right here we have destroy instance there we go so that's all we need let's run the game and see how that looks there we go so the player object deleted itself once it collided with the asteroid now let's do the bullets so for this we're going to need a bullet sprite so let's come over and create a new sprite so SP our bullet and I'm just gonna make this pretty small so 2x2 and I'll just color it all white and the collision mask for this will just be the whole thing so we can leave it at the default and we'll put the origin in the middle center and let's go ahead and make the object for the bullet obj bullet and we'll set its freighting next to the bullet alright so firstly we need to do the logic for creating this bullet and that is going to be done by the ship so let's come to the ship and we'll add a key press and I think I'll use the spacebar this time but we're not going to use key down like we have been using we're gonna use a key press because we only want to create one bullet at a time just when the player presses the spacebar alright so again let's just use this search right here and type create and right here is create instance so let's drag this in just that we are creating an instance when we press the spacebar alright so the object that we're creating is the bullet the location is going to be wherever the player is so now we just want it to be right at the player's X&Y relative to wherever it is in the room so we'll just tick relative and if we wanted to offset the bullet then we would be changing these but we want it right where the player is we do want it to be created in the instances layer because that was the layer in the room where we're putting all of our instances and now not only do we want to create the bullet but we also want the bullet to shoot off in the direction that we're facing so we need to get access to the bullet and change its direction to match the image rotation or the image angle of the player and now so that we can get access to the bullet that we just created we can save its ID into this target so whatever we put here so let's say new bullet it's going to create a new variable for this object to remember and it's going to save the value of the unique ID for this particular bullet object and now I can use this to change the value of not my variable but new bullets direction so instead of just typing direction if we put new bullet and then a dot this is going to access the new bullets direction so not the ships it's going to be the new bullets and then we can change it to match the ships image angle and finally one other thing that I want us to consider is this temporary tick here and we can tick it basically if we don't have any more need for that ID after this and that's true we don't need to remember this anymore so we can tick temporary we only need to remember what it was so that we could get access to the new blows direction so now the bullet will be going the right way but we also need to set its speed to actually have it moving so it's coming to the bullet at the create event there we go so we can just set its speed and maybe I'll set it to be six so that's coming a bit faster all right so now if you run the game and we press the shoot we should get a bullet being created there we go all right now let's do the collision event with the asteroids so we can add an event just like with the player collision asteroid so we want a few things to happen in this event so firstly we want the bullet to destroy itself so that's easy let's do that let's just drag in destroy instance and now we also need to get access to the asteroid that we're colliding with and have it destroy itself and also create two more asteroids so what we can do is use apply two right here and then any more blocks that we drag into here we can get something else to run it so not the bullet but we can say other and in this case other means the other objects in the collision so not the bullet but the asteroid and now when I say destroy here it is the other instance that will be destroying itself not the bullet so be very careful of what you put connected to this apply to all right so after the object destroys itself then if it's a huge asteroid then we want it to make two more smaller medium asteroids and if it's a medium asteroid we want it to make two smaller ones and if it's a small one we might just have it explode with some debris so firstly we need to actually check what kind of asteroid that we're dealing with it so we have to check what it's sprite index is so we can say if variable and the variable that we're checking is the sprite index is equal to the huge so if this is true then we want to create a new instance so put it there and the instance that we want to create is an asteroid and now again we want to make sure that this asteroid it's sprite index is a medium asteroid because remember at the moment which is having asteroids randomly select what their spark mix is going to be so this create event is going to run immediately after it gets created right so immediately after this block happens we have an opportunity to overwrite what it was just set to so just like before we're going to save this in a temporary variable so that we can have access to the asteroid and we want to just tick relative and we going to change new asteroid dot sprite index to be the medium asteroid but actually we wanted to create two didn't we so we could copy and paste this and have it perform this twice but another thing we can do is use a repeat loop so this will repeat in action however many times you want so now it will be repeating this action twice and then we'll just chain that on to here all right so now we want another case so if it's not huge then we might check if it's medium so what we can actually do is just copy this entire thing paste it here it's come up in the wrong place let's just drag this over to this so it's performing it in the right order but now we want to check if the sprite index is medium and we want to set the index to small and now finally for the last one for if the sprite index is equal to the small sprite I might do something a little bit sneaky and no matter what the sprite index is so if it's huge medium or small in all cases I'm actually going to create some debris so we haven't made the debris yet so let's go ahead and create a sprite and I'm going to make this even smaller than the bullet and have it just be one by one and I'll just call it that white alright and I'll make the object for it and we'll set its fright index all right so now it should come up if we try to change it in the bullet so and we're not going to wrap this in an if because we want to do it in any case so we'll just say create an instance and that's gonna be the debris will take relative again we don't need to save the ID but we do want to create ten of these so let's put in another loop and we'll repeat this 10 times because the drear pretty small and we want it to be a bit of an explosion so now I'm gonna have the debris all of these choose a random direction to shoot off in and then fade away over time so that we can't see them anymore so in its create event we will choose a direction so we want a random number again set a random number integer from 0 to 359 and the variable that we're changing is the direction all right and that's also set its speed just a 1 and now in this step event I am going to have it reduce its transparency so it looks like it's fading away and we can reference an instance variable that all instances have and this one is called image alpha and the Alpha of the image is kind of like its transparency and this goes between a value of 1 and 0 so 1 is fully opaque so it'll be a completely full white square and then as it reduces down to 0 it's going to fade to become fully transparent so every frame of the game I want to reduce this image alpha relative to what it currently is so let's choose a small number for it to decrease so does take away 0.01 every frame and then once it is invisible it's still going to exist in the game but we don't need it to be so we can have it check if image alpha hits zero and I could say equal but generally just to cover cases where for some reason the zero has been skipped and it continues down into the negatives maybe we have had some strange frame skip or something unexpected has affected the value it's good practice to just say less or equal to so that it covers the case of zero and anything below that so if we hit that then we want it to destroy itself and I don't think we had the player make any debris when it crashes it just got destroyed so let's add the same event to the collision in the player so let's just copy this and paste it here all right let's test that there we go so we're creating the explosion when we shoot the bullets the debris is fading away and the player also explodes when it hits an asteroid now at this point I want us to consider something so whenever you're developing your project you should consider where you can improve performance and an important way we can do that is to ensure the objects on screen here are the only ones that exist so far we've gotten around a ship and asteroids going outside the room by having them wrap around it when they go outside the border but we haven't done that for the bullets so for these when they go off screen they still exist in the game and they're still out there running code making that collision check and we don't need them to be doing that once they go off screen they're never going to come back in the game so we can add an outside room event in the bullets this is down in other outside room and that gets triggered whenever the bullet moves outside of the room then we can just say destroy itself all right so our collisions are looking pretty good in the next tutorial we're going to be adding things like lives in the score as well as setting up at different rooms in the game like a start room game over and win screen so I hope you enjoyed this tutorial that's it for now I'll see you in the next one
Info
Channel: GameMaker
Views: 65,566
Rating: undefined out of 5
Keywords: YoYo, Games, YoYo Games, GameMaker, Game Maker Studio, GameMaker Studio, game, maker
Id: lReluF7yFjk
Channel Id: undefined
Length: 13min 33sec (813 seconds)
Published: Tue Oct 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.