Programming Games for Android

Programming Games for Android 31: Box 2D and 2D Scene Part 1

42 videos

238 minutes

Great, we determined Box2D screen, we also have Scene2D screen. Let now the real part, the code that you have in the description of the video, so what I'll put together the best of the best of Box2D Scene2D. We'll have to do the following: first create the Stage, and then create a Box2D simulation for the parameters. Everything you have seen Box2DDebugRenderer and OrthographicCamera, is an illusion:

Box2D knows only calculate parameters. We need to stop simulating the world, get properties, update, and let the actors move or updated as you have calculated Box2D. That is, with Box2D know how to calculate things, but Box2D does not draw anything, because for that we Scene2D. Let's start with Scene2D Box2D gathering, and I'm going to do in a new screen to not have it so dirty everything. I will move the screen Scene2D Actor package,

rename it in case you want to display your code. Rename the actor Scene2D package, but I will work from scratch. In fact I will create a class called GameScreen to indicate that is the main game screen. Of course how could it be otherwise is a BaseScreen, obvious. From here we can start creating things. What should we create? I need to create a Stage, to be part of Scene2D. I need to create a world, to be part of Box2D.

We do import, and already have. I define a Stage. I define one world. [Keyboard] Importo everything has to be imported, and forget not make a dispose (), for what we will see how our system with multiple screens, because by default does not do well. We will also see how we deal with do rendering, which will import directly from Scene2D, basically. I copy and paste without remorse, and with that I have my Stage drawing,

cleaning and all that. Now I make my MainGame create a new screen GameScreen type, and now should be able to display a blue screen in which no nothing happens. You See? Why nothing happens? Because no aggregates actors. Let's see how to add these players, so first I will create a package called Entity, and I will create my first actor to be ... in fact, to see you see how is this ... well, yes, I'll create it from scratch because

as things must be integrated Box2D I do not lose anything and so I explain it better. It will be a PlayerEntity, Actor type. The actor will need to know its texture, because somehow you have to be drawn in Scene2D, but also needs the world to associate and delete it, or do any kind of calculation. The Body's own player itself, the player's own Fixture itself. More variables that we have previously seen: when he's alive, when he is jumping.

With all this, we are ready to create the PlayerEntity. Le Mundo step as a parameter, then a texture, and an initial position, as it may be a Vector2 to indicate the starting position. And now this is simpler than it seems. It remains only to save the instance of the world, the Texture, but everything else is done so we will copy and paste as if there were no tomorrow. For example, creating the definition body,

it was easy. We saw that it worked. This is misspelled, now I see. What changes is that instead instantiated in a fixed position the volume parameter, but it is already. With this we have created the Body. And the same with the Fixture. The catch here, copy it, and hit. We're not programming in copy plan and paste, but as this is done and it works, all you have to do is adapt. If anything I rename this variable. Remember what size: 1 meter.

With all this we are ready to draw on screen, and so I tell a problem that we will find.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.