 This video is part of a series. Be sure to check out the playlist in the description of this video for the full playlist. Definitely recommend watching the previous videos because we're adding on to what we did in previous videos. And last time we created two players that fall and bounce when they hit the ground. Should be having actually really added a ground. They bounce when they hit the world bounds. They collide with the world bounds. Okay, so today we're going to be moving our players around. So you want to use controls. We're going to use basic key presses in this example here to move our players around. And right now we have two players on the screen. We're going to control them both with the same set of keys in this instance. So let's go ahead and start working on our code. So this is what our update functions for. The game is going to constantly be looping through this update and it's going to run certain commands. And you'll have different updates for different characters for all your enemies, your objects, your power ups, and of course your players. So instead of typing all that code in that function, I'm going to create a function called player update. And that will update all of our player settings. So now that that's in there that will loop and down here. I'm going to type function player update. So whatever we put in here will happen continuously as the game moves on. So what we need to do first though is get key presses. So before we move in here, let's go ahead and go all the way to the top of our code here outside of any functions. I'm going to create a global variable called cursors. And this will be any inputs that the key presses from your keyboard. And now that we have that, let's go into our create function here. And what we're going to do in here is we're going to tell we're going to really, I don't think I have to put this in two different places, but this is how I do it. Cursors equals and we're going to look at our game, which we created on the first line of this code input. So there's different type of inputs, what type of input keyboard. So we're looking for keyboard inputs. And we're going to create a cursor keys curse. Let me spell things right. Cursor keys. So now the game's constantly watching what keys are being pressed and they're putting what's being pressed into this variable called cursors. And yes, I do have to do this way now that I'm thinking about it. So I created a global variable so we can look at what keys are being pressed anywhere in our game. But I didn't, I can't. No, I could do it out there. Anyway, this is how I'm doing it. So now into our player update. So again, this is a constant loop. It's constantly checking for stuff. So what I'm going to do in here is I'm going to say, and again, I'm setting this up so that you could theoretically make multiple multiple players. In your game, I'm going to say players. So our group of players, I'm going to say for each. So this is going to loop through each of our players. And I'm going to say for each of our players, I'm going to label our players P for short type players each time. And what I'm going to do in here as we're looping through this, just like to keep things nice and tight. And at this point someone's going to say, oh, you can add a function into Vim that will auto close things. Yeah, I don't like that. It bothers me. But yes, you are correct. So each time it loops, we're going to take our P, our player in this case, their body. And we're going to look at their velocity. And on the X, so that's left and right, and we're going to set it to zero. And then we're going to say if our cursor, and that's just going to mean they're going to stop. What this is doing is whenever this loops through, it's going to stop the player wherever they are. You can finagle it to where they slow to a stop. But here we're just keeping it simple, stopping the player when this loops through. Then after it stops them, and it's going to be so quick that you won't even notice it if you're actually still pressing, the left is down. So here cursor, which is, sorry, that should be cursors, because that's what we created up there. So cursors, it's looking at what key is being pressed. So we're saying if a key is being pressed. So if a key is being pressed, and that key is left, and it's saying it is down. So what are we going to do next? We are going to take our player, take their body, and set their velocity on the X axis. And now left is moving negative. So we're going to say equals 150. And of course you can tweak the speed to get whatever speed you like to add to the character. Else if, so if that's not true, if the left key is not being pressed, we're going to check our cursors again. And if it's the right key that is down, and remember this is case sensitive, we're going to take the player, P, body, and set their velocity on the X axis to 150. Oh, I forgot I said negative, but I didn't type it. This should be negative, and this should be positive here. Negative moving left, positive moving right. So now I can save this. And if I typed everything right, we can refresh our game over here. And obviously I typed something wrong. Let's open up our handy data console in Chrome. That's F12. Go to console and it says that I must have typed something wrong here. Create cursor keys is not a function. So let me look at my notes real quick. Keyboard, input, game, create, cursor keys. Okay, let's look at our code here. At least sure what I've done wrong at this point. Game, input, keyboard, create. Oh, it is a typo. I put an O in there and then cursor C. And it's okay when I mess up because then you might know what you're looking for. So that's how you check for errors. Now, sometimes people have problems where they say to me, oh, I checked the console on those note errors. That's because there's not an error necessarily. You may have just forgot to call a function that you thought you were calling. So let's go ahead and F5 now. There are players and I can use my right arrow key and they both move to the right. I can move my left arrow key and both move left. And because we have world bounds collision set on, you can see that they both collide when they hit this edge of the world. Now, if we didn't have collide with the world set, they would have went off the screen there. But now they're moving the same speed, and of course you can give different players different speeds if you'd like, and they're kind of overlapping each other. So what we should do is make them so that they collide with each other so that they don't overlap unless you want them to be able to pass each other. So if you have two players, if you want them to be able to pass each other, you leave them how it is now and give them different controls. But since we want our characters to be able to collide with each other, let's go ahead and quickly add that into our functions here. So we're going to go back into our player update, and here we're just going to say game.physics.archade.collide, and then we're going to pass two variables to this function. We're going to pass our players group and our players group. So that means that our players are going to collide with other players. We're going to have other groups later on, platforms, bricks that you can walk on, and so we'll say players can collide with those platforms or bricks, but here we're saying that each time it loops, basically it's going to check, is a player colliding with another player? If so, don't let them do that. So let's go ahead and I save that, refresh that. Now I can move them back and forth, and you can see they stop, they don't overlap. So I can get to the edge of a screen, and you can see even though I'm pressing to the right, they don't overlap each other anymore, because they're colliding with each other. The same would happen. Let's go ahead and do this real quick, just as another example. Let's add a third player, because our screen doesn't have enough players on it already. So in my create function here, I'm going to create another player, and I'm going to create them on the same X of 200 as the player before them, but I'm going to move them down the screen a little bit, so the top of the screen is zero. Let's go ahead and move them down 100, 100. So since the other one's a 10, he's 90 pixels below the other player. So I save that, come over here, refresh my game, and they were overlapping because of the size of the image already, so let me move him down 150 instead of 100, and we will now do this, and boom, he's standing on his head. So our players, we now have controls that we can move our players around with, and our players can collide with each other. Instead of overlapping. I hope you're enjoying this series. If so, be sure to check out the rest of the playlist. There should be a link in the description to the full playlist. New video each week, so if you can't access next week's video yet, it's because it's not next week yet. If you would like to have early access to all of my videos, think about becoming a supporter over at patreon.com, or if you just want to support me because you like my videos. A little as a dollar a month, you can support me at patreon.com. That's 1,000. There should be a link in the description of this video to that. If you can't support me financially, that's fine. I appreciate you watching, but you can support me by liking, sharing, subscribing, and commenting. It really does help, supposedly. Anyway, so go ahead and do that, and as always, I thank you for watching, and I hope that you have a great day.