2.5 Introducing Multiple Bodies
In this scene we are going to walk through the process of adding additional bodies to the scene. By doing this, you can begin to create interesting landscapes or levels to present to the user. This will become very important over time if you want to keep the user engaged.
1.Introduction3 lessons, 07:37
2.Introducing SpriteKit7 lessons, 54:48
3.Building a Game11 lessons, 1:30:25
4.Conclusion1 lesson, 01:49
2.5 Introducing Multiple Bodies
If you've been following along to this point, you've gotten to the point here where we've now created a Sprite Kit game that has a ball, or a sprite, that just kind of bounces around the screen in a diagonal fashion. Which is pretty good, I think that's a good starting point, but the next thing I wanna do is I want to reintroduce another body into our world that is going to have interaction with another body, so, that's gonna be in the form of a paddle. So I want to introduce a paddle that is going to have an effect on the trajectory and the direction of the ball. So let's come back into our project, and here we are in our game scene designer. So what I'd like to do is drag another color sprite out here just like we did before. And just kind of pick a spot somewhere it's not a big deal. And we're just gonna stretch this out a little bit to have a particular size. And then we'll change the color to blue. All right, so we'll go ahead and save that. Now, in order for this to have an effect on the ball, we're going to have to make sure that we set the physics definition in the body type here, similarly to how we did before. Now just a little bit of clarification the, you have three options here. You can select bounding rectangle, which kind of makes sense for these two shapes because they are kind of rectangular, so that just means the boundaries of these nodes or these sprites are going to be rectangular in fashion. So if I were to put a ball or something out here, something circular, then I would probably pick a circle which doesn't make much sense for this particular application since that would really cause some strange things to happen when this would actually run. And then thirdly you can select alpha mask which basically is going to be the shape of the object that we're using. So if you have a very complicated shape it would follow those particular boundaries. But like I said before since this is a rectangular shape, that just makes sense. So we're just going to, maybe set these things similarly to how we did before and let's see what happens. We'll set friction equal to zero. Restitution will be, once again, one. And then the linear dampening and angular dampening we're going to set to zero. So let's go ahead and save that. And actually, because the ball is moving in a direction that, kind of comes over here to the right, I'm gonna bring this over here just a little bit so we can see the, the ball actually hit it, and we'll save that and run. Now, I think what you're gonna see here is something that is not quite what you were expecting. So it's going to hit and the, the paddle is gonna move, and it is going to come down to the bottom, and it's going to mess with the, the direction of this ball, so this is not the type of functionality that we want, and why did this paddle move when the ball hit it? Well let's stop and come back and take a look. So the reason that, that happened is that when the ball, which is dynamic in nature, which means it has outside influences affect it, when it hits something like the boundaries of the screen, it's going to move around and bounce accordingly. But we don't want that to happen with our paddle. We want our paddle to be stationary or static. So in order to have that happen we need to uncheck the dynamic box, which is going to make this a static object. So, let's go ahead and run this again. So now just by making that one small change, now the paddle stays still and the ball will hit it and continue to bounce on its path. So that's pretty nice. We've now been able to see this at work, but this is a little bit boring. I can't actually do anything with this paddle, but at least I've been able to add it in there and have it notice when things hit it, and affect the trajectory of the dynamic bodies that are also in the world. So, in the next lesson, we're gonna introduce the way to actually notice when we have touched this paddle and then allow us to move it back and forth across the screen.