Programming Games for Android

Programming Games for Android 34: Box 2D and 2D Scene Part 4

42 videos

238 minutes

As I said, I have created the actor to represent the ground and actor to represent the skewer. In both cases it is simple, it is copied and Box2DScreen paste the code and put it in nice shape, always creating, first defining the body in the position it deserves, and then shaping it into a box or pinchos as you play. Here you see that the ground is box-shaped, but the spikes used vertices

I had created earlier. In the case of ground to instantiate it first tell where the edge of the floor. For example, at position 1. Then I tell the width in meters, so if I say 1000 will have a floor 1000 meters wide. And the Y is the upper edge, which makes the top edge and where they will rest other objects is what I say. I say to the skewers on skewers where X is horizontally,

and the rest And where skewers, which should be the same value and has the floor. With this I could already go instantiating my actors. For example, first getting the textures I'll need, as may be the soil. FloorTexture, OverFloorTexture and SpikeTexture. On the one hand floor.png charge, overflow.png, and spikes, which could rename it by spike.png, will be the skewers, and spikes. Now I can do is to create,

on the one hand FloorEntity list to represent all soils that I create. Why do you want a list ?, you say. Imagine you want to put steps, I have to keep all my soil to know when jump from one floor to another. I also need a list with skewers to have several skewers on my level. As we are in Java 6, because we must respect the compatibility with Android, we always pass the type

data in the ArrayList, not just use a diamond, because that is Java 7. I must say it is an ArrayList of FloorEntity and SpikeEntity. And finally, I add the first instance that will create Floor, associated with this world, with this ground. And I'll create at X = 0 and I'll give a width of 1 km, and I'll put base 1 meter high. Similarly, now I'll create a skewer, in the same world using the same texture, and I will place a position

6 meters or less. With a base that is placed within 1 m. Finally, I itero on my floor, and each of them added to the Stage, and the same with Spikes, right. Finally I have to do something to eliminate all the Stage. On the one hand, I owe decouple the ground, and eventually eliminate. And like, I have to remove all the spikes, in both directions. Perfect. If I now wanted to test,

putting values ​​such as 1.5, we would see all the objects that I created. Or not, who knows. I do not know why not see ... Ah, okay, that ... Maybe ... I forgot to rename ... sorry. Of course I should rename the Manager, the file. And now, we should be able to visualize everything that we have created. Both the ground, as the player, as skewers. It remains only provide the behavior so that the actor can jump when we click

or we touch the screen with your finger, and obviously you can move while alive.

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.