Game 3
  • Overview
  • Transcript

2.6 Add Bombs

It's pretty cool to see our spaceship flying through the sky, but to make this fun, we need some obstacles. We'll begin by creating a single bomb, and then we'll add some scrolling logic to move it towards our spaceship. We'll end the lesson by adding an entire barrage of bombs.

2.6 Add Bombs

Moving right along, the next thing that we would like to do now is place our bomb on the scene. And so we can see that show up on our scene. And then since we're already familiar with the concept of how we're gonna animate the background, let's go ahead and give the bomb some movement as well, all at the same time. So the first thing that we want to do is we want to actually add a bomb to the scene. So we're once again going to create another function here. And this is going to be add, and actually, I'm going to move this up a little bit just so we kind of keep things together a little bit, all of our add functions together. This is going to be addBomb. Now, we're only going to be worried about adding a single bomb at this point. And then as part of the movement, we'll start to add some additional ones on there. And you'll see how we'll do that in a minute. So similarly to how we have done all the other adds, let's go ahead and let bomb be equal to a new SKSpriteNode. And it's gonna be the image named bomb just like that. And then like we did with the spaceship, we're gonna go ahead and set its scale a little bit, so we can kinda scale it down just a little bit. So we're gonna say bomb.setScale. And we're going to set this down a little bit smaller than we did before. Let's try 0.15, and once again, these are things that you can play around with a little bit. Then we're also going to work with its physics body a little bit. We're going to set its physicsBody equal to another SKPhysicsBody. And we are going to give this once again a rectangleOf size bomb, and whatever its size is, like that. So go ahead and save that. And then we are gonna want to also make sure that the bomb.physicsBody isDynamic, so let's go ahead and set that equal to true. And the isDynamic stuff we're going to see come into play a little bit more as we start to get into the whole collision concept. But we'll get there. And then we're gonna say bomb.name, cuz we're gonna need this once again, just like we did for the background. We're going to call this bomb. And then we also, like when we did with the ship, we gave it a definite position. We said we want it to start at something like 120, 150, something like that. Or 120, 160 like we did up here. We don't wanna do that necessarily here. We want it to be a little bit more random than that. So what we're gonna do is we're gonna create a little bit of a random generator here. And then kind of generate where it's gonna start off to the right of the screen, because we want the bombs coming from the right to the left. So we're going to say, let random be equal to, or this is gonna be a CGFloat. We're gonna have a CGFloat here, and we're going to set this equal to a CGFloat. And then we're gonna use a bit of a random generator here. We're gonna use an arc4random_uniform, like that, and we're gonna give it an upper bound of 300, since that's kind of the width that we're dealing with here. We're only gonna deal with positions up and down from 0 to 300, because that's about as high as we want to be able to place this particular bomb. Once we have this random value here, we're now going to say bomb.position is going to be equal to CGPoint. And once again, we're going to give this an x and a y coordinate. And our x coordinate is going to be self.frame.size.width. And we're just gonna adjust this a little bit, say plus 20, cuz we don't want it to be too far onto the screen. We want to actually kind of shoot this off just a little bit, so you can just barely see the edge of it there. But you don't wanna see it kind of just appear on the scene, you want it to start off to the right a little bit. And then we're going to give it a y coordinate of random, actually, we're going to specify this is going to be the randomness where on the up and down it's going to be. And then once we've done that, we're going to go ahead and say self.addChild, and we're going to add our bomb. And we're going to save that. And just so we can kind of see it show up, the first time, after we add a ship, we're going to say self.add, not background, addBomb like that. So let's go ahead and save this. Let's execute our application. And now what we should hope to see is, well, nothing should change all that much. But as you can see here, off to the right just a little bit, we're going to see the tip of our bomb. So it's not moving yet, but we can kind of see it got placed there. So that's a good start. So now what we want to do is we want to introduce the same or similar type of animation or movement in the background. We want to add that same type of movement for our bomb. So let's go ahead and do that now. So once again, like we were doing with the background, we had the concept of a velocity here. Let's do another velocity for our bomb. So we're going to say our bombVelocity. But this time, well, let's make this a little bit more interesting. I want the bombs to be moving a little bit faster than the background. So let's say this is going to be moving at 5. And so, we're going to see the bombs actually kind of moving faster than the background, or ultimately moving faster than the clouds, which I think is kind of an interesting little animation there. So now we want to add another function down here. So we have our moveBackground, let's go ahead and add in a moveBomb. So we're gonna do a function, we're gonna call this moveBomb, like that. And we're gonna once again do our enumeration. And this time, we're going to be working with the ones that are called bomb. And we're going to have a similar situation here where we have two parameters, we have the node and then we also have the stop. There is no return value. So, you can say, Void in, and now we can give it our body. And this is gonna be pretty simple, we're just going to, once again, do this little cast again. We're gonna say let bomb = node, and then we wanna try to cast this as an SKSprite node. And if that works out, then we wanna go ahead and execute this little block here. And then we're gonna say bomb.position = CGPoint, where we set our x value equal to our bomb.position.x minus our bombVelocity. And then our y is simply going to be the bomb.position.y. Because we want to maintain the y that we set up randomly when we added our bomb up here. So we want to maintain that so it's coming across in a straight line, although you could kind of randomize it a little bit maybe to make it interesting. But then you might get this weird kind of jumping effect that might be a little strange for the user. Now, like we also did with the background, if we get too far off to the left, we don't necessarily want to move this over to the right, we can just kind of get rid of it altogether so we can not use up too much memory here. So we're simply gonna say if our bomb.position.x is, say, less than 0, if it's moved off to the left of the screen, then all we're going to do here is say bomb.removeFromParent. Which is going to remove it from the scene, and we're not going to have to worry about it any more. So then once again, like we did with our background, we were using our update here. I want to move background, but instead, or in addition to moving the background, I would also like to do self.moveBomb. So let's go ahead and save that. So let's go ahead and run our application and let's see where that is getting us now. So here we are running, and we can now see our bomb moving across the screen. That's pretty cool. So I can kind of move my ship up out of the way to miss the collision there, which is not set up yet. So don't worry about that too much. But now the problem is, well, there's no more bombs. We only set it up to handle that one. How can we introduce some more? We want them to kind of keep coming in a steady barrage of bombs, so we have to move our ship out of the way. So let's go ahead and see how we can do that. So really what I want to do at this point is I want to continue to add missiles, but I want to add them at a decent interval here. And the way that I'm going to do that is by utilizing my update function here. So what I want to do is I want to initially set up my moveBackground and moveBomb, because those are the first ones that I want to deal with. But I also kind of want to pay attention to this currentTime so I can see the current interval into my game and maybe adjust and see if there was such a distance between when I placed the last bomb, can I put another one in there? So the way that we're going to do that is we're actually going to come up here to the top and let's create another variable. And we're going to call this var lastBombAdded. And this is going to be a time interval, and we're going to set this equal to 0. So we haven't actually set anything yet, or haven't actually added any bombs yet. So then we'll come down here into our update. We'll do all of our initial placements, and then we'll say if currentTime minus our self.lastBombAdded, if that subtraction is greater than 1, which means we've had a little bit of time in between our last placement, then I'll do self.lastBombAdded = currentTime + 1. And then I will do self.addBomb and save that. So what this should allow us to do is see a couple of bombs show up at kind of a reasonably similar interval and maybe see a couple of them come across the screen. So we have two there starting out, there we go, and now we're kind of getting them randomly placed on the screen. So that's pretty cool. I can now set this up. And you kind of saw a little bit of a collision there. And we're gonna adjust that a little bit. But you can kind of see there they're bumping into each other a little bit, which is pretty cool. But ultimately what we're gonna be attempting to do in a little while is to detect a collision and then handle that in a certain fashion. So we're gonna start talking about that very soon.

Back to the top