Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Handle Touches

While it's nice to have our spaceship on the screen, it's rather boring at the moment. Let's spice things up a bit by having the spaceship move up and down on the screen when the user taps. This is where we'll start to introduce some of the basic math that will become important in upcoming lessons.

2.3 Handle Touches

Okay, so what we have right now is pretty good. We can put the ship on the screen but the user can't really do anything with it. So we wanna be able to add a little bit of interaction with the ship. So now I wanna be able to tap above the ship and have the ship move up. And if I tap below it, it should move down, so that I can start avoiding these obstacles that are going to be coming my way fairly shortly. So we're going to do a couple things to enable that. We want to create a couple of actions, a move up action and a move down action, and then we want to integrate that into our workflow. So the first thing that we're gonna do is create two actions here. So we're gonna create a shipMoveUp action which is going to be an SKAction, and we also want to create a shipMoveDown SKAction, just like that. So that's going to be the beginnings of our actions, and we're going to start to add a little bit or define what these actions are actually going to do. So the way that we're going to do that is within our addShip function here towards the very bottom. I think just before we actually add it to the scene, let's go ahead and play around with those a little bit. So we're going to say that the shipMoveUp is going to be equal to a new SKAction. And in this case we want to moveBy, and so we're going to move this in a direction. So when I tap above the ship I want it to move up, say, 30 units in the y direction. So you can play around with how far you want it to move. That should be fine, but I'm going to keep it fairly simple for now. So it's not going to move in the x direction, it's going to move up 30 units in the y direction and the interval Is gonna be 0.2. I think that should be enough reaction time for it to move up and down. Once again, you can tweak these values to your liking. So we're gonna say shipMoveDown is gonna be equal to an SKAction as well, and this is going to be a moveBy, once again, 0 in the x direction. And this time we're going to move it down 30 units in the y direction, and we're gonna use the same duration. So that's going to be the actions that we can take. Now how do we actually tie these things up to our ship? Well, in order to do that we're actually going to override an existing function and we're gonna start to play around with the touchesBegan function. So how we do that is we go in and we've said we want to override a function, and the overridden function is gonna be touchesBegan. So within here, we can start to determine when touches happened and then we can do things in response to them. So when you do start to work with these things, you get a list or a collection of touches. So what we're gonna wind up doing is we're going to say for all of those touches, we're gonna go and look at all of those touches that are happening. And start to react to pretty much each and every one of them, or any of them that kind of matter to us in this case. So we're gonna get the location of the touch, so we're gonna say let the location equal to touch.location. And we want this to be the location in ourself. So within our scene I wanna get the location. Then I want to check to see if the location.y, cuz remember if it's the touches above where my ship is, then I wanna move it up. So if that touch.y is greater than the ship.position.y. And I want to make sure that we're kind of within the bounds here of my scene. So I'm going to make a little bit of a hard coded choice here. And once again this can be kind of a calculated value here. So I wanna also check to see if myShip.position.y is less than 300, so I'm only going to let it go so high, so that it doesn't go out of the screen. Then if those things happen, then I can say, and I can tell it to run a particular action in this case. I wanted to be the shipMoveUp action. Then if I check to see if the location is not greater than, the location of my touch is not greater than the location of the ship, then I want to move it down. So in this case I'm gonna say else if the ship.position.y is greater than 50. So as long as I don't go too low, then I want to say and I want this case to run the shipMoveDown action, just like that. So let's go ahead and save these changes. Let's go ahead and stop this and restart it. And if everything has worked correctly now, once the simulator starts up, I should be able to see my ship just as I did before, and I do right here. And if I were to now start to tap above it. I see it move up 30 units in the y direction, and I can keep going up all the way to the top. So I get a little bit more fine-grained control over where it's at, and how far down it's going to go. So I think this is going to be a good start, so that I can begin to avoid the obstacles that are coming at me. So now that I have handled or are able to handle the touches and the location of my ship on the screen. It's time to start presenting the user with a little bit of a background instead of this stark white color.

Back to the top