My First Game - GML - 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 GML in game maker today we are doing attacks and collisions now in our 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 that's concentrate on that first one so in game maker the way our standard collisions work is that one object checks if its collision mask or bounding box is overlapping with that of another object and this collision mask is something that we set up in the sprite for the object so let's come over to the ship sprite over here you'll see the collision mask so by default it's going to be set to a rectangle that covers the entire sprite but there are some other options we can set it to be an ellipse or a diamond we can also set the mode to be manual so that we can control the dimensions 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 are there 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 serious performance issues are probably not going to happen unless you have several hundred collision checks or more but it's better practice to opt for efficiency when you can rectangular collision checks are much more simple computations to make than 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 a sprites so for the ship objects we could just do a diamond and we can make the mode manual and then we can kind of distort this shape so that it matches the ship spine so generally speaking even if the hitbox doesn't cover the entire sprite especially if it's the player sprite the player isn'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 for example is if we had a rectangle and the very outsides of the sprite did cause a collision where the player saw that it clearly didn't so we've set the ship's collision mask 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 leave that just at the 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 fewer 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 the asteroids and the same is going to go with the bullets when we get to that so let's go move it to the ship and there's a few ways that we could do this there are collision functions that we could use in this step event but there's also a dedicated event for a collision so let's go ahead and click colliding with the asteroid and so just like the step event this event gets triggered when there is a collision when those bounding boxes are intersecting so now we just have to work out what we want the player to do once we've intersected with an asteroid so later on we will want to be doing stuff like deducting lives and perhaps deducting the score but for now I'm just going to make the player destroy itself and there is a function for this so we can type instance destroy and this will destroy whatever instance is calling this function so if we just run the game and move our player into an asteroid you can see that it destroys itself now let's do the shape in the bullets so for this we should go and make a bullet sprite so it's add a new sprite I'm going to call this SPL bullet and I'm gonna make this pretty small so it's just going to be two by two and I'll come into it and I'm just gonna call this white okay and the collision mask for this will just be the entire thing so we can leave it at the default Center the origin in the middle center let's go ahead and make the object for this bullet and we will set it sprite index to be the bullet all right so firstly we need to do the logic for creating the bullet and that is going to be done by the ship so let's come into the ship and let's add another key press to this so I'm just going to copy one of these and now for my keyboard button to shoot I'm going to use the spacebar alright and now instead of using check like we have been doing I'm actually going to use check pressed because I only want to create one bullet when I press the spacebar if I left it at check then this would actually be true for any time that the player is holding down the spacebar so you could create 60 bullets per second if you just held down the spacebar for a second and that's not what we want we just want to do it when we press the spacebar so in the case that we do press the spacebar then we want to create a bullet so there is a function for this instance create and there's two options here so you can create it in a layer or add a certain depth so if you remember in the rooms we have different layers here and if you see right here they actually get assigned a depth like this so you can make it at a certain depth or you can just put it into an existing layer and that's what we're going to do I'm just gonna put it into the instances layout so we'll use instance create layer and now where do I want to create this bullet I want to do it at the players location and then I want to kind of shoot off in the direction that my player is facing so I wanted to kind of match the image angle of the player so let's say X Y because that's the location of the player right these are some instance variables that every single object has as you can tell by the fact that it's green just like our image angle and sprite with then it's asking me what layer I want to put it in so I want to put it in the instances layer and make sure that you type that with the correct capitalization because it is case-sensitive and then finally what object do we want to create a bullet alright so this function will create a bullet object but it also returns the ID of the object that it's created so every instance that gets created in the game has its own unique ID and with that ID we can access objects and change things about them so for instance I will want to change the direction that the bullet is facing so to do this we can actually save the ID that this returns like this so that way we are making a variable equal to the ID that this function is going to return and right here I have declared a new variable right this didn't exist before it's not a built-in variable like these green ones it's coming up in blue and normally this will set up a variable that is saved in the object until it gets destroyed but we don't really have to remember the ID for the bullet I only really care about it in the instance that it's being created all I want to do is save its ID so that I can change its direction so we can make this a temporary variable by putting this var before it so now it's coming up in yellow and these temporary variables or local variables they only exist while the script is running and at the end of the script it's discarded from memory so this is great if you only need to use this variable one time all right now I need to get inside this object and change a variable about it so generally we can change a variable by just writing something like this and it will change the instance that is running this code but I want to change a different instance so how do I do that well I can access an instances variables by typing inst dot and then a variable name like direction and that means I am now accessing this instances Direction variable not the players ship object so then I can set it to anything that I wanted so we're just going to make the direction match the image angle alright so now we've set its direction but the bullet at the moment doesn't have any speed so just like we did for the asteroids let's add it some speed for the bullet and I want it to be pretty fast so I'm gonna use the value six so if we run the game now I just see what that looks like there we go alright now let's do the collision event with the asteroids so just like with the player let's go out event collision with the asteroid so in the event that the bullet collides with the asteroid we want a couple things to happen firstly we want the bullet itself to be destroyed so that's easy let's just do that and now secondly we also want to destroy the asteroid and then depending on the shape of that asteroid I want to kind of split it into two so if it's a huge one I want to form two medium ones if it's medium I want to form two small ones and for the small one I might just make it kind of explode with some debris so firstly I have to actually get access to the asteroid that I'm colliding with so unlike before where we could get access to the ID by creating the instance we're gonna have to use a different tact and we do have to be careful because we don't want to refer to every single asteroid object we can't just say obj asteroid dot whatever I only want the one instance that I'm colliding with so for this event it actually has a special keyword that we can use called other and this in this very specific case in this event so you can only use this in particular places this will refer to the other instance in the colliding events so the instance other than the bullet so whatever instance of the asteroid so that's why we use this keyword and instead of using a dot I'm actually going to use what's called a width statement and this is another way to kind of get access to a different object in the card block so now whatever I put in between these curly brackets my asteroid is going to run instead of my bullet so if I said instance destroy in here the bullet doesn't call this the other instance does because it's in a with statement so be very mindful of what's inside these brackets and what's outside all right so we do want the asteroid to destroy itself and then we want it to create two new ones so now I'm going to have the asteroid check if the sprite index is equal to and remember we put a double equals here if we're comparing instead of setting a variable which is where we put one equals if the sprite index is equal to SP our asteroid huge then we want to create at our XY location in the instances layer a new asteroid and I want to make sure that this asteroid is a medium asteroid and remember by default our asteroids were kind of randomly choosing what sprite index they would be and when we create an object this event this create event will get run immediately so as soon as this happens the asteroid will run it's create event choose a random index and now I am free to kind of overwrite that and set it to be medium so again let's just save the new asteroid so you can call these whatever you want it doesn't matter and then we want to set new asteroid dot sprite index I'm gonna set this to be SP our asteroid medium okay but I don't want to just do this once I actually want to create two and I could just copy and paste this just like that and it would create two but another way that we can do this is to use what's called a repeat loop and it works like this so we just type or pate and then the number of times you want something to repeat so I want to create two asteroids so to do this open the curly brackets and we will wrap all of that code in here so it will repeat this code twice all right so now we want another case so if it's not huge we want to check then if it's medium so I'm gonna use not if but else if and then actually let's just copy this so this else--if means that these two statements are linked together it will only check this if this one isn't true so if sprite a nice is huge it will run this otherwise else if sprite index is medium then it will run this and it will create two small ones all right and now finally for a case that it's small well I might actually do something a little bit sneaky and no matter what case of the sprite indexer if it's huge medium or small every time I'm actually going to create some debris at the location of the asteroid in the instances layer and we haven't made this object yet so let's go ahead and do that now so let's create a sprite for it SPI debris and this one's gonna be even smaller than the bullet just going to be one by one and we'll just call it that it might and we will create the object for it obj debris set this Friday next and so I'm going to create some of those and this will just create one of them but let's do another repeat loop and let's do it ten times because these are very small all right sir all up let's have a look at what we're doing in the event that we collide with the asteroid we destroy ourselves then we go with the asteroid have it destroy itself and in depending on if it's huge or medium we create two more asteroids and then we also create some debris so if we're the smallest asteroid we're not going to create any more asteroids but we will still create the debris but at the moment when it creates these ten to Bri objects these debris objects aren't going to be doing anything I want the debris to move off in a random direction and also just fade away so let's come over to the debris and say when it's created so just like the asteroid objects let's have it choose a random direction so we'll use the AI random range again between 0 and 359 and I'll just set its speed to 1 so it's moving off and also while it is moving I want it to do something so in the step event I wanted to gradually fade away and then once it has faded away completely I want it to destroy itself so we can use what's called the image alpha and the Alpha of an image is kind of like it's transparency and this goes between a value of 1 and zero so one is fully opaque right so it's just going to be a completely white square and then as it gets reduced down to zero it's gonna slowly fade to be partially transparent and then completely invisible so every frame with the game I want it to equal itself - and then I'm gonna use a very small number because I want the fading to be pretty gradual so again we could just put - equals zero points everyone right they mean the same thing and then finally I just want to check if the image alpha is less than or equal to zero so I could put it just is equal to zero and then say instance destroy but generally just to cover cases where for some reason this is your has been skipped because sometimes you can get bugs and games where a frame gets skipped or something that you haven't taken into account is going to affect the value so again it's just kind of good practice use this instead of just equals a flat number and I think at the moment we didn't make the player make any debris when it crashes it just kind of gets destroyed so let's add that same event to the collision here so we want the player to also repeat 10 times instance create layer XY instances obj debris all right so now let's give that a run all right let's go ahead and shoot this so it works it made the debris and there we go so it is working all right now at this point I want us to consider something whenever you're developing your project you should consider where you can improve performance and an important way we can do that is ensure the objects on screen are the only ones that exist and so far we've gotten around our sheep and asteroids going outside the room by having them wrap around it but we haven't done that for the bullets which means when they go off screen they still exist in the game so they're still out there running code and making collision checks 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 which will be triggered when the bullet goes outside the room so down here in other outside room and have them destroy themselves all right so our collisions are looking pretty good next time we're going to be adding things like lives and the school as well as setting up the different rooms in the game like the start room the GAMEOVER room and the wind 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: 107,470
Rating: undefined out of 5
Keywords: YoYo, Games, YoYo Games, GameMaker, Game Maker Studio, GameMaker Studio, game, maker
Id: kpjqweJv_0g
Channel Id: undefined
Length: 16min 37sec (997 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.