GameMaker Studio 2 Tutorial - Simple Character Movement (L, R, U, & D)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this tutorial I'm gonna be showing you how to get an object to move around the screen left right up and down as shown on this game on the screen right now you see this guy moves up down left and right he's animated as he walks and when he you let go he stops moving and his animation stops so that's where that's where we're gonna trained it done here let me move this out of the way and first thing you got to make sure that you've already got your sprite set up if you do not have your left right up and down sprites already created then please do so before continuing with this video or if you don't know how to do it there is a video that explains how to do it about how to make a sprites out of a unaligned sprite sheet so if you need to watch that video first and go through it so that you've got these and I'm gonna go through real quick just so everybody can confirm that you have what you need so I'm going to open up these four sprites here and you'll see that they are all in the different directions here you're gonna want to double check a few things here make sure that they are all facing the right way they're named properly and that sort of thing the name should all be similar in this case see I've got SPR Knight left and then I've got right and up and down on each one just I would just double check that everything's okay just run the animation make sure it looks good and again scroll down to the others make sure that they look the way they should the other thing you want to check is that the origins are all in the same place and the way you do that is just go through and look at on this part right here where its marks where the origin is make sure they all say top left or they they all could say Center but as long as they are the same you see this little crosshair right here that's what you want to be concerned with please avoid clicking down here because then you see that that offsets everything in messes things up so just leave it on top left or Center for each one for this tutorial eating one of those works okay so they all look good I'm gonna shut them all down here and then we will get on with our our tutorial from here all right so the first thing you want to do is we've got to make a object I'm going to do that right here just like when we created sprites you just go to the objects right click and go create okay and just like with the sprites I'm going to use a naming convention I'm gonna use a very similar naming convention that I'd used with the sprites instead of SPR I'm going to be using obj as the prefix and I'm gonna use the same name that I use for the sprites but this time I'm I'm not going to add a left or right or up or down the one object will use all for those sprites I'm gonna set the sprite the sprite I'm going to use is the down sprite the one where he's facing them yeah I picked that guy that's what it looks like all right so next step I want to do is I'm gonna go ahead and put them in the room now so I can demonstrate a few things and show why we're doing why we have to do some of the things we do so go over here if this is not expanded this little triangle right there you click that and expands and you can double click on see room 0 to zoom in and out here you just hold the control key down and spin your mouse wheel forward to make it to zoom in spin it backwards to zoom out to move around you just you middle Mouse with the middle mouse button you just push down on the wheel and you grab it and move it around okay all right so what we're gonna do is we're going to drag this guy the knight character and just click with the left mouse button and drag it I want to put them roughly in the middle the screen okay and then I'm gonna play the game show you what it looks like all right you see he's sitting in there he does not move left right up or down at all but you do see that his little legs are moving so that's one of the first things we have to stop we got to get to stop doing that and the way you do that is you basically ask yourself a question when do I want him to not move his legs and the answer is when the game starts or another way to say it is when he is created when that object is created we want his animation to stop moving I'm gonna close this out on the side here so I get a little more room to work with so that's my event there's add event you click this button an event is something that happens that causes other actions to happen so if you press a key you run into something the game starts that sort of thing that's that's the event so the create event occurs when the when the character first appears in the room and that's when we want this to happen we want us animation to stop so I click create and then the actions window pops open and we come over here and we look down at the instances section these are all actions that can occur in the create event so we want his animation to stop that's this one right here it's the little guy looks like he's chasing a couple of triangles here so we'll drag that into place and this is set animation speed and what this current number means is every step it will advance to the next sub image so if I open up my actually let's use the down one because that's the one we were actually saying so when the game starts and you see the default animation speed is 1 every step he goes here then here then here the NIR and he just cycles through and that's what gives the illusion of motion so we'll go back to our object and we're gonna set this to zero and what that does is now he will not advance to the next sub image and you can quickly see that if we play the game you will see that he is no the animation is no longer happening and it looks like he's just standing there now just like that okay alright next we're going to start getting a move and the way we're going to do it is I'm going to I'm gonna do the left direction completely and then we can just duplicate it save ourselves some time we can just duplicate it and and just make a few tweaks to the other direction to make it work okay so the event I'm going to use to get him to move to the left is the when I press down on the left arrow key I want him moving okay now you may want a different key a lot of times in games people use the W a s and D keys and instead of the arrow keys but so in this case if you decide to do that you may do so but just be aware that I'm gonna be using left right up and down for simplicity's sake but I will try and tell you when when those changes are made so first again out of it so I'm gonna add a vent and so again I click this button the add event button and I go to key down this means whenever the key is down and it is the left key down you see right there then I want whatever I put in this actions window will happen okay so the first thing I wanted to do is I want it to move left so I come down here to the movement section right here movement and I want to pick this first button set Direction fixed it's a real simple one just click that and tell it click on the arrow for the direction you want it to go now just like in real life Direction doesn't mean anything and if you go for movement unless you have some speed so we've got to add some speed here so I go over here in the same section movement you see set speed just drag that right under there and these can these don't have to be in this order it just just the way it is but typically people in real life when they turn in the direction they want to go and then they set a speed so we're just trying to simulate real life a little bit better so I'm gonna set a speed of four now what that number means is the number of dots on the screen if you've zoom in really close on a on a computer screen you'll see it's made up of a bunch of dots and what that number means is every step it is going to move four dots in in this case in the left direction so that's what that means alright so I'm gonna play the game and you see the up and down and right do nothing so if I hit up nothing's happening if they down nothing's happening if I hit right nothing's happened but as soon as I hit left he starts moving to the left now he won't stop but he see he's not turning to the left and he's not he's not animating though so we're going to we're gonna fix that so first thing we want to do is let's go ahead and set the the Anam that the changes sprite so that he's facing to the right and the way we do that is you up here in the instance section right here instances you will see this one looks like a Pac Man drag this this is what changes his sprite now if we is going to the left we want him to use the left sprite so I'm gonna click this little pull down and you see there's my animations if you want to enlarge these you hit this a little slider right there then you can you I think you can hit these little buttons right there and it'll I guess not it'll just enlarge so drag this slider to enlarge them so you can see them as big as you want however you need to so I'm going to pick the left guy okay and you see I'll go to this later but if we play you see now when if I go up and down and right nothing happening could with a go to left see now he turns a light to the left but you see he's sliding and this is because remember in the create event we said his animation speed to zero so he is not cycling through all of his sub images so if I look on the left one here see he's staying on this one right there so this is what is happening so let me go back to the object if I go now I saw I got to start his animation up again so I go just in the same event that you or the same action that I used to stop his animation I'm gonna drag that in and now I'm gonna start his animation and again speed one means that he will cycle through one step or one sub image every step so in the first step he'll on this one the second step he'll be on this one third step here for the fourth step you'll be on the third one okay so now we're gonna play this and you're gonna see something strange happen okay now I'm so up doesn't work down doesn't work right doesn't work because we have a program in now I'm gonna what it's gonna happen here is if if I just tap the left key he'll walk but if I holding if I'm holding down the left key as I am now his legs his animation will stop and there's a reason for it in the way we program that we basically told it to do that and we're gonna fix that so that it doesn't happen anymore if you look here you see we set his direction to left we set his speed to four and then on this one right here we told it to go to the left sprite and we told it to go to frame zero what frame zero is and you'll find this in programming a lot is they don't program is typically don't count when when they start counting don't start with one they start with zero so this is frame zero this is frame one this is frame two and frame three so we're telling it every step right here we're telling it go to frame zero so that's what it's doing every step that that one that whenever that Keys held down it is going to step two frame zero it's going to this guy right here we don't want it to do that we want it to just whatever frame it's happens to be on that's what we're gonna do so the simple trick to fix that is click this relative button right here this check box click that what that does instead of setting it to zero it adds zero to whatever the current sub-images so if I go to the left sub in or if I go to left sprite I'll see that this is zero right here if I'm adds 0 to 0 I get 0 so if he's on 1 and I add 0 he's still on 1 if I add if he's on 2 and I add 0 he's still on 2 so now his sprite should now work correctly so let's go back to the object and okay so now again up-down doesn't work right doesn't work but left works and even whether I'm pressing it or not he walks to the to the left yeah but he doesn't stop so that's our next step we'll get him to now stop so I'm gonna go so when I let go of the key or when the key comes up when the left key comes up we will add that so they're here we have at event key up and again left this time remember going to do the left direction completely them and then we'll duplicate everything so what we're gonna have happen is we are going to make it so that we are going to make him stop moving we're gonna stop his animation and then we're gonna then we're gonna well I'll show you in a second so we're gonna stop his speed and again just like on to start him up here's our set speed right here and we drag that over and we want his speed to be zero so now if I play and I go to the left and let go he stops he stops moving but his little legs are keep going okay so the next thing we're gonna do is we got to get to stop animating so that's the next step again in the instances section here's the animation speed set animation speed drag that into place we want his animation speed to be zero we want him to stop just like he did when the game was first started we do not click relative this time this time we want him to go or we want him to I'm sorry that was on another part we want him to his speed to be zero so he's not going to cycle through anymore so now we play the game and we find that we have another problem as we must walk to the left here you'll notice that sometimes when he stops his legs are apart from each other now most people when they walk when they stop they put their legs together so we want to ceiling like this we want his legs together when he stops okay so the way we're gonna do that is we are going to set his sprite again we're gonna use the same kind of set animation we're gonna set his sprite drag that in okay now he is going to the left so we have to set his sprite to the left this time okay so again I click there and then pick that guy right there and then this time we do want him to go to frame zero so if we go back to the the sprite here you'll see there is frame zero that's him standing still frame two also does the same thing so we could pick we could put a zero or two there it doesn't matter but for simplicity sake I'm just gonna leave it at zero so now when the gameplay is he's when you let go of the left key he stops moving his animation stops and he's he goes back to what he's still on the Left sprite but this just gets it to go to the frame zero okay so now if I play you will see that that works famously so now he goes the left stops and you see whenever he stops his legs are always together okay so now we've got the left working just as we wanted to so now we can we we want to change one thing a little bit when this is to make our lives easier if we decide to change something later on notice we use the speed here and for the directions speed or for the the speed that he's moving we put the four right here just tell us how fast we want it to move and then down here for animation speed we set this one now if we go and duplicate all these directions and they'll all say four a four for speed and one for animation speed but what happens if we decide you know what he's not moving fast enough we want him to go faster or if you get some kind of powerup and we want him to go faster when he hits that powerup we want to be able to change the speed quickly without having to go through and reprogram everything so what we're going to do is in the create event we'll go to the create event right now we're gonna set what are called variables variables all the variables are just words or a letter that represent a number or some other value so right here I'm gonna drag this guy right here a signed variable pull that in and the variable name I'm going to use here is called SPD this is gonna this is going to be what we use to to control how fast he moves okay so I'm the reason I'm using SPD is the word speed is an actual variable used in game maker that will get him to move so we could change his speed using that so we can't use it to set his speed it just tells whatever his current speed is so that's why we can't use it so I'm just using SPD and you can spell it how you want you know all capital letters like this or SPD like you know first lower K first letter uppercase the rest lowercase but you have to remember how you spell it because spelling counts in game maker it it doesn't it thinks there's a huge difference between this capital SPD and this so it does not understand it so you got to make sure that you spell it correctly I'm just gonna use lowercase SPD I'm gonna set this value to 4 remember this is this value we set for speed so I'm gonna set SPD 4 to 4 to represent the same thing I'm gonna also set another variable this one minute called an e speed Ani SPD again you can choose whatever name you want here as long as it's not already a built-in variable in game maker but you have to remember how you spelled it otherwise it will not work correctly so now I'm gonna go to value here and I'm just gonna put 1 this is gonna represent our animation speed how fast he cycles through his sub images and this is what we have it set to in the programming already so now I'm gonna go to my key down left and I'm gonna start replacing this so where I set my speed here I'm gonna change this from speed from four I'm gonna just change it to SPD which right now when the game runs will equal four that's what it stands for okay and then down here for set animation speed I'm going to put a ni SPD and remember you must spell it exactly the same as you did in the create event so now if I play this game even though this is a word here this this means four and then here it means one the ante speed here means one so again I just play the game and you'll see that it works just as it did before no changes so if I go to the left he still works exactly the same but I can quickly change if I go to the create event and I change and I changes his SPD value to eight now which is double what it was and hit play you'll see he now moves twice as fast okay and same with an e speed I can slow him down I can go make his animation speed say 0.25 which is quarter a quarter of the speed he normally goes so now watch his feet here and you'll see he will move his legs will move very slowly now it gives you a weird effect like he is sliding but you see that's that's what it can control so I'm gonna so I'm gonna set this to four and I'm gonna set his and this animation speed to 0.5 you might want to write it like this so it's a little bit clearer to see 0.5 and lose I'm in real quick so you can see so that's that's what you can do now now that we've got so now that we've got left set up we got left completely setup to for him to start and for him to stop we can start duplicating these and then just making a few changes here to make our to make it a little bit simpler so four key down left on over here just have this selected right click on it and go duplicate event right here you click that and you select what do you want to duplicate it to so we're gonna we're gonna go key down right do the same for key down duplicate key down up and duplicate event key down down now if you're using WASD left would be you would you would make it key down letters a up would be key down letters W right would be key down letters D and then down would be key down letters s okay all right so now I need to go through and just change some of the change some of the things in these other directions here so left you see I'm going to the left I'm picking the left sprite okay I need to do the same and also for up I had to change it to I'm you zoom out here there we go for left or for up I got to make sure that I'm clicking the up button again make sure up is selected over here click on the up button changing it to the up sprite which is this guy facing away from you for the right same thing click on the right over here and change it to the right sprite and then for down click down and change it to the down sprite like that okay now for the left for the key up left you're going to do the same thing here we're going to just duplicate these duplicate event key up this time right duplicate event key up up and hit event key up down now you'll notice this puts it in a different order than it shows on the menu you can't change these you can't change the orders of these it's there this is the order that they run them and this is the order that the program sets them to go so you can't like grab this and drag it down so just you know - just live with it alright so now now let's go through these and fix them here so key up left you see the only thing that really needs to change is setting the sprite right here okay I'm gonna go to up and I'm going to change that to up alright guy change right to the right right and change down to the down sprite okay yeah we're about done but let's just test it and make sure it's working okay don't here is so he stops when he moved when game starts no he moves to the left and he stops feet together moves to the right stops feet together there you go up down and everything is working swimmingly as it should okay all right there you go all right thank you and on we'll have other videos now how to make a how to make it work like a platformer game a simple platformer game and we'll do another one where he is steering like a car so stay tuned for those all right we will see you soon
Info
Channel: Eric Gallery
Views: 40,336
Rating: undefined out of 5
Keywords: GameMaker, Studio 2, Tutorial, Character movement
Id: nmRH7CbJljc
Channel Id: undefined
Length: 25min 35sec (1535 seconds)
Published: Mon Sep 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.