7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 22Length: 2.6 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.4 Creating the Paddle

Now that we have our ball moving around freely on the screen, let's create our paddle. Now we're going to come back to our scene designer here and I'm going to once again drag out here a color sprite. And I'm going to kind of change the dimensions of this a little bit. And once again this can be anything that you want it to be. I am going to set the size of this to, let's see, 250 by 30, something like that. And I'm also going to change its color. To be this blue color again. And so now we have that, and we want this to have a name of paddle. So we'll go ahead and change that, make that saved. Now I'm also going to come down here and play with the physics of this guy just a little bit. I want this to once again be a bounding rectangle, but if you recall, I want to uncheck dynamic in this particular case, because I do not want it to be affected by collisions and have it move dynamically. It's a static body. That means it's just kind of going to be there. It's going to inflict changes on other dynamic objects, but it itself is not going to change, or the physics of itself is not going to change based on collisions with other dynamic objects. So we'll go ahead and save that. And once again, you can kind of move this around however you would like. I'm going to position it here because it's kind of in the way, at least originally, of where the ball is going to go. So I want to save that. We'll come back over to our GameScene.swift. And we wanna do a couple things. We want to make sure we add a category for this guy, so that we can detect collisions. Now we may or may not need that, but, at this point, you know, we're gonna create it in there anyway, just cuz I think it's gonna be good practice. So we're gonna create a PaddleCategory. And we're, this is, once again, going to be an Unsigned Int32. And it's going to be initialized to this value of one again. And it's going to be shifted to the left one. Which means it's going to be the same exact value. So we're going to copy this just so it's easy to see. [BLANK_AUDIO] And the only difference is that this is going to be shifted over one so it's going to look like that. So now we have a category for our paddle. Now let's also change this a little bit so that we can introduce the movement of the paddle so we can actually start to touch the ball. So this work is gonna be done in the touchesBegan function as you see down here. Now we're going to first get our touch. So we want to say touch is equal to, and remember, since we're only dealing with single touches in this game, we can actually just get any object from our touches collection. Now, do remember that this is just going to give us an optional any object. Which is not gonna help us very much in this case. We need to cast that to UI touch and we also need to make sure that we give it an exclamation point, simply because we want it to actually get the value and not the optional of this type. So now we want to get the location of where that touch is. So we're going to say the location is going to be equal to our touch.location in node. And we want to know where this touch occurred within this particular scene. So we'll get those relative coordinates. And then we want to see if we're actually touching something and if that something is our paddle. So, we'll say, If we let our body equal to self. We want to go into the physics world and we want to get a bodyAtPoint, and that point is going to be our location. And if that is satisfied, then we'll say if the body. We want to get its node has a name equal to paddle, since that's what we set it to. Then we're going to find a way to notify at least the rest of our code that we're truly are touching this paddle, and that we're, we should be registering and doing things with the movement. So, before the way that we handled that was by creating a variable or a property at the class level. So we'll say var is touching paddle, and it's gonna be initialized to false so that we can see that this is truly a boolean value. So then we'll come down here, and if we are touching it, then we will say is touching paddle equal to true, so we'll save that. Now we need to know when we're actually moving, cuz remember there are three parts to the touches world. There's the, well, actually four. There's touches began, canceled, ended, and moved, and we're gonna be concerned with began, ended, and moved in this particular case. So we'll do ended right now, and it's just because it's pretty easy. So we'll say, is touching padel equal to false. We'll save that. And now the final one is going to be the touches moved. So now when we moved, we wanna to calculate where it is we're moving to, at least with our finger. And then adjust the location of the paddle, at least the x location, not the y location, cause we're only moving horizontally, where it should go now. But this all starts based on the, are we truly touching the paddle? So, if we are, then we need to once again figure out where we are. So, we're going to say the var touch is going to be touches.any object casted to a UI touch. Then we'll get it's, now this is where we start to talk about the current location and the previous location. So, we get both of those things in this movement so we can get where we started from and where we are going to. So we're gonna say the current location is going to be equal to touch.locationInNode, and that's gonna be within self. And we're gonna say var previous location is going to be touch.previous location in node, and that's also going to be self. So now we have the current location and where it was. Then we want to get it a reference to our paddle. So we're gonna use our child with no name paddle. And we're once against going to cast that as an SK sprite node, so go ahead and save that. Now we need to do a little bit of math just to calculate where we are, and where we need to move this panel along the x access. We'll say our exposition is going to be equal to our paddle.position x plus the difference between where we are and where we were going. So we're gonna say the current location.x minus the previous location.x. So that's going to be where we want our panel to go but remember, we don't want the paddle or any part of the paddle to go off of the screen. Because if we let it go off of the screen, then we're gonna have problems, cuz we're not gonna be able to grab onto it again or it's just gonna look kind of weird. So, we just use a couple helper methods to check that make sure that the min and max values are being set appropriately based on the size of the paddle as well as the size of the actual scene itself. So we will say that we want to run the max between two values here. So we're going to say our x position. And the paddle size width property divided by two. And then we are also going to do the x position equal to the min, between our x position, as well as our size, which is the size of the scene, it's width. Minus our paddle size that we just used before. So paddle size width divided by two. So now that we have those set up we can go ahead and save that. And then we'll go ahead and set our paddle position. So paddle position is going to be equal to CG point make. And its x position is going to be x position. And our Y, because we're not changing that, it's going to contin, it's gonna continue to be the same thing it always has been. We're simply going to set it to its current position.Y. So we'll go ahead and save that. So once we've done all of those things, if we've done them correctly and we run our application we should now see our game start, and there we have the ball kind of bounced off our paddle. And I can also touch and grab this, and start to move this around, so I can move it left and right. the ball can currently bounce underneath, which we don't want, but we're gonna make adjustments to that later. But I can now move my paddle around. And I can hit the ball and have it move, and hopefully, eventually run into some blocks. So now we have our two sprites set up. We have a dynamic ball. And we have a static paddle that are going to create the basis of our game.

Back to the top