GameMaker Studio 2: Your First Game (GML) - Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Sean Spaulding and welcome back to part 2 of our beginner tutorial series so we're already off to a good start we've got our player object that we can move around with the arrow keys and it points points himself towards the mouse cursor but it's still not really very much of a game that's not really very much to look at in general really so what we're going to do in this part is we're going to create a bullet that our player object and shoot around and create some little fireworks so the first thing I'm going to do just like before when we made our player object and it's sprite is we're going to make a new sprite for our bullet so I'm going to right click in sprites and hit create will take us back to our workspace and open up sprite editor and I'm just going to call this one SBR underscore blitz I'm going to click import and just bring in my bullet sprite click yes to that see it brings it in fine and then I'm just going to place the origin somewhere around here in the center of the sprite I'm not going to use the drop-down middle center anymore so I don't want the origin to be there I want it kind of to mark the center point of this sprite is being around here inside the sort of main chunk of the bullet itself rather than sort of that the the trailing blaze coming from behind it so I'm just going to click on the image just to place that origin about there I could type it in precisely with these corners if I want to but it's easy enough just to place it there so that's that's sprite so now that we have a sprite for a bullet you can guess what we might want to do next and let's create a new object for the bullets I'm going to right click in objects and hit create and then I'll create a new object editor for us I'm just going to link that where it says new sprite to SPR and score bullets so we have the bullet attached to the sprite and call this obj underscore bullet just in the exact same way that we settled the player object to begin with now we don't actually need to set up any events or actions in here just yet what we're going to do is come back to our player object and we're going to set up the code in here to dynamically create instances of obj bullets on the fly in the game so whenever we click the mouse button will create a copy or an instance of this object in the game world so the first thing we can do is we can actually get rid of this this comment at the top here that says you can write your own code in this editor we don't need that anymore but what we can do is actually just write a comment of our own just to label what each section of this code does now that we're starting to add a bit more to it trust me it's very easy to forget what certain areas of your code do and leaving comments in your code very easy way to track that write in a comment it doesn't actually do anything in your game it just just serves as literally a comment or a note in your code do it by just typing two slashes and you can see the text turns green after that and anything you write you can write anything after that anything you want and it won't be running or interpreted by the game as an instruction so I'm just going to write moving around there because that's what that chunk of code does that controls is moving around and looking at the mouse then underneath that I'm going to create a new comment it just says shoot okay and the code we put here is going to control our shooting so what I want to do is check to see if the mouse button the left mouse button is being pressed and if it is create a copy of obj underscore bullet at the position of the mouse for now we'll see what that does so I'm going to use an if statement just as I did before and type F and then open brackets just as I did before and this time instead of keyboard check I'm going to write Mouse underscore check underscore button and open brackets and type NB underscore left okay this condition here is doing exactly what I just said we're going to do we're going to check to see if the left mouse button is currently being pressed and if all that is true then whatever we type after this line of code will happen but sometimes when you write an if statement you want to do more than just one thing if if that statement is true um we only want to do one thing initially here but we may want to do more later so what I'm going to do is instead of simply writing the command immediately after the line is I'm going to open a pair of curly braces and by doing this after the if statement instead of carrying out whatever happens here we're going to carry out whatever happens inside these curly brackets now it's also common practice when you do this to press tab once for every time you open up up of these brackets so that whenever I type my code in here we can see very clearly when reading the code that it's part of this if statement if I type everything the same indentation like this it becomes harder to see where your if statements start and end even though the brackets are there and it just makes it a little bit more human readable to just a press tab once and just sort of indent the code that happens inside your if statements so what do we want to actually happen when we press the left-mouse button we want to create an instance of our bullet object in the game world so what I'm going to write is instance underscore create underscore layer and then as with most functions like this you typically have to open and close a pair of brackets and usually have to supply some sort of instructions or arguments to that function to tell it what to do you'll see when you write a function like this game maker actually shows you at the bottom here exactly what arguments and values and information that particular function needs so as you can see this one needs an x-coordinate a y-coordinate a layer ID which we'll come to in a second and the name of the object that you want to create so if the x and y we're just going to use the coordinates at the mouse so just separate it by commas here I'm just going to type Mouse and just go X comma Mouse on the score Y and that'll give us the x and y coordinates of our mouse now the next value that we need to supply is the layer now all instances in a game make a game or arrange two by layers and this is an important change between game X to do two and studio one the layer and instance is on determines the order in which it is drawn to the screen each layer is set to a certain depth layers with a higher depth are drawn first and therefore appear behind objects on layers with lower deaths you can create and manage layers from the room editor which we will come to in a bit for now in which we're just going to write layer which will take the layer our player is currently on and we'll create the bullets on that particular layer after that and in the comma and just write obj underscore bullet so that will create a bullet on the same layer as our player at the x and y coordinates a mouse so now if I go ahead and ruin the game and see if I hold the left mouse button or if I just click the left mouse button I can draw bullets all over the screen see if we don't actually need to use a use this function to create a bullets if we really wanted to we could create a fancy art game out of this it's quite fun just to sort of draw these bullets all around the place so that's how we create instances dynamically in the game world so first of all let's adjust this so instead of just creating the bullets at the mouse coordinates we create them at our player object because we're going to create them at the player and then cause them to shoot off by themselves okay so I'll close the game now and just where we wrote Mouse X and mouse Y before just simply shorten them to x and y just as the same as we were referring to our players x and y-coordinates when we were moving around so now what we're going to do is open up object bullet and move our workspace down here and add the create event this time so differently from the step event which runs every single frame of the game currently 30 frames a second um the create event only happens once and that's just when the object is made for the first time let's go real a comment at the top and I'm just going to write a set up motion because what we're going to do is basically set it up so a bullet just flies off into the distance the moment it's me first thing we want to do is get the direction from which we want the fire now the direction is going to be based on our player's position and the position of the mouse okay it's going to be the angle between the two so we can get that just by typing Direction equals point underscore direction and we use this before we just want to take two two coordinates an x and a y position in another X&Y position and work out the angle between them so we're just going to use the x and y position of this bullet as it's just been made so it'll be at where the player is our x and y position and the position of the mouse so mouse underscore x and some score Y just as we've used before okay but we don't want it to always be directly that angle we want to sort of random a little bit so it feels like we're sort of spraying the bullets a little rather than just shooting them directly a mouse cursor just to add up a little bit of game feel so I'm just going to type Direction equals Direction plus random underscore range and then this generates a random value between two values that we give it so I'm going to say minus four and four so we're going to add or subtract a value between minus four and four to our current direction just to make it a bit more randomized then we're just going to give our bullets and base velocity and speed to begin with some inner time speed equals um sixteen so there will be 16 pixels per frame it'll move in the direction we've just given it and we're going to set our image angle lastly to be our direction so the animal of the image adjusts to face the direction it's in just as we've done with the player before now all we have to do is run the game and we should find when we click the left mouse button that we shoot lots of lovely bullets everywhere now we create a nice sort of wheel fireworks display if I just sort of spin the mouse around like this you can see it looks a little silly at the moment because I bullets are creating on the same layer as our player object and so there are appearing like on top of our player like that because the game isn't really receiving any instruction about how to order those which is creating new ones and drawing them higher than whatever was drawn before okay in order to fix that we need to create a new layer for our bullets and I create the bullets on that layer instead so that they are player below our player as opposed to above so let's fix that one now let's close this and go to our room editor and we can see over here on the left or layers of the moment we have instances and background the background is a background layer just designed specifically for sprites in the background and not object instances you can also create more background layers but we're interested in our instances layers at the moment we have one by default that's just called instances and that's where our player object currently sets the indwelt the instance of our player object currently sits in the game world and what we want to do is create a new layer so I'm going to click here create new instance layer you see it's called instances one going to rename it to bullets Malaya okay press ENTER and I'm going to drag it underneath instances so that the bullets when we create them will appear below this layer so anything we found bullets layer will appear below instances because you can see if you look at depth of the bottom here bullets layer is set to a depth of 100 or as instances is set to 0 if we rearrange them like this it intuitively makes it so the bullets layer is now 0 and instances would be a hundred the higher the depth the the deeper into the screen the things will be drawn they'll be drawn before anything else that will be drawn behind other instances okay so we want the depth of our bullets layer to be higher than that of our instances so we drag that underneath so bullets layer has the higher depth depth of 100 and instances has a depth of 0 now all we have to do is change the layer that I'll put being created on so if we go back to our object editor for Ruby J let's go player where we created the bullets in the first place instead of just creating them on our current layer let's type in the name of the layer we just made which is bullets layer and I've wrapped that in quotation marks because we have to pass the name of the layering is a string you can't just type bullets lay have to type it in in quotation marks as a string for layers it's just how layers operate at the moment so if I run the game now you should find when you're shooting the bullets they will appear below the player now instead of just on top of them because they're being assigned to that layer which has a higher depth than the layer or player is currently sitting on the last thing we're going to cover in this particular part of the tutorial series is how to make those bullets I'm not shooting a continuous stream one every single frame or I'm just going to slow it down a bit they'll still fire constantly when you hold the mouse button but they won't just fire one every single frame like that will sort of create some space in between so they fire sort of a bit more reasonably so what we're going to do is we're going to create a new event and our player object by clicking add about we're going to create the create event similar to object bullit and this line of any code we put in here is going to run when the instance of the player object is made for the very first time okay so it's only going to happen once what we're going to do is we're just going to write cool-down equals zero what that means is we're basically creating what's called a variable okay so we're just assigning the word cooldown to equal zero so now whenever we refer to cooldown in our code we're basically referring to the number zero and we can change the number that this holds and do any number of things with it variable is very useful and very powerful as well soon find out now if I come back to the step event in here at the moment we're checking to see if the mouse button is held down every single frame of the game and the step event and if it is we're just going ahead and creating a bullet but what we're going to do is create basically a small cooldown period that's on that variable is going to be used for so instead of just checking if the mouse is held down we're also going to check to see if cool-down currently equals zero in fact we're actually going to check to see if it's less than one rather than zero the reason for that will become clear in a second so I'm going to type two ampersands symbols which basically represents and so if Mouse check button and be left and the following condition are true then do this stuff rather than just checking one condition we have to open another pair brackets for this condition um they are right cool down less than one okay so we're going to check to see if that variable that we just made which is zero at the moment if it's less than one which it currently is because that's what we've established it as a zero zero is less than one then create a bullet object okay well I'm then going to do is set that cooldown to equal three after we've fired the bullet okay and after that's happened will no longer be able to shoot so at the moment if we were to run the game would be able to fire one bullet because I'll cooldown is less than one but then cool down will be set to three which is greater than one and we wouldn't be able to shoot bullets any more because this would always return false all right now in order to fix that what we're going to do is outside of this code cooldown equals cooldown minus one so any frame where you know we're not shooting a bullet and setting it back to three the cooldown will slowly tick down by one every single frame until it is eventually less than one again okay this is why we check less than one rather than equals zero because we could go into minus numbers quite easily here on the air about minus 1,000 or so because we've not fired in ages but the whirlwind one the check is that it's less than 1 and if it is fire bullets at like cool down back to 3 and then continue to slowly tick the cool down and down so if I run the game now and if I hold left mouse button you can see we fire but at a slightly slower pace you know still firing quite a lot if I increase the value on that we set cool down to which I can demonstrate now if I set this to say 10 and ran the game it would mean we basically need to wait 10 frames between each bullets you can see it's firing much much slower now okay so that's how you create simple projectiles now that we have a game where we have a player object on that player can shoot some bullets the next part in the series is going to look at creating enemies for us to actually shoot at I hope you've been enjoying the series so far and I'll catch you guys next time
Info
Channel: GameMaker
Views: 218,880
Rating: undefined out of 5
Keywords: YoYo, Games, YoYo Games, GameMaker, Game Maker: Studio, GameMaker: Studio, game, maker
Id: 28pEbQ_TIcQ
Channel Id: undefined
Length: 16min 37sec (997 seconds)
Published: Wed Nov 02 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.