42 videos
238 minutes
Good news for purists: this is the last video in which the code will be this dirty. But first there is a matter to review. Voucher, now I have the game well, I can jump and all that. But most boring go Geometry Dash, the character does not move. Let's see how we can move the character on screen. To do this before anything else will move the camera a bit, so you can see that if I change its properties by 16: 9, have separated more
skewers, because I want to give some more space for what I do now. I'll show you how we can do to make the character move. We here skewers and we are interested in the character always move towards the right. The idea, as in the Geometry Dash is the character jump to avoid taking the piñazo. If you touch the spikes we must consider that the character is dead and the game is over. But we must find a way
make it move. To do this, Box2d in the Body class offers a method called setLinearVelocity, by which we can change the linear velocity of a Body. We can pass it as a vector or as components X, Y, but somehow we can play with the instantaneous velocity Player or any other body at a given time. In my case I want to adjust the speed or X X velocity component to always
to move to the right. And speed does not interest me touch it because when we do is jump impulses or forces that cause change vertical speed, because it is the only one I play, so it should stay as it is the vertical component. I can only change the horizontal. This is not difficult to get, I get the value of the speed and getLinearVelocity, and then use this parameter to modify the horizontal speed
and put 8 m / s, maintaining the vertical speed to remain seized that moment, because as I say I do not care. Among the jump and updating the world this will be changing all the time and do not want to touch. But with this change, the character and move. You See? The only problem is that it goes very fast. I treat this collision to hit when you have to stop moving because it has no grace. We have already lost, no sense. So taking advantage
This is the last video before cleaning'll mess even more and will introduce a variable called joeVivo to tell us whether he is alive or not. While still alive, we can keep moving. So now I can put this code in a check, so that now only move if alive, and what I can do is another contact to assure that somehow collided with a skewer, and if you hit a skewer stop the animation
Joe and make sure to be alive. Again, I could do with variables of this style but cleaner to check this is by a UserData forms. With them we can associate any object to anything Box2D, is a body, a fixture ... for it, the bodies always have a field called userData. We can get it with getUserData, or modify it setUserData. And as you see, you can spend an Object, anything. Thanks to this,
we can associate any object, whatever it is, and get what we can use to obtain and manage collisions. In my case I will make him pass strings to identify what type or category is everything. For example, minijoeBody going to be a Player, sueloBody will be a Floor or pinchoBody one Spike. Now that I think, as I make collisions with Fixtures, have to change in place,
the fixtures, not the Body. So let this change Fixture ... And now the Fixture minijoe is a player, the ground floor and one skewer one spike. And now I can see more elegantly, to update this code: if fixtureA.getUserData () equals ( 'Player') && fixtureB.getUserData () equals ( 'Floor'),.. or vice versa, you know. the Fixture A, its userData is Floor and Fixture B, your userData is Player. I leave this as it was, make a run, and I show how
stop when you crash. ... Okay, sorry, if I do not jump obvious we can not check. Look what has jumped well. Okay, so we will do the same but now changing floor instances by spike. And when the player causing collision with a skewer, joeVivo is set to false. That is, it stops moving. The results are instant, the instant in which I put a fake and not the speed is updated and stays stuck. This is a good time to show
a screen set 'you lost' or show something that says you've lost, sounding a noise. This is a way to teach you how to play with speed. You see that is instantaneous. So does a little jump but it does not move. This is how we can make the collisions between spikes to stop moving the player. Begin to organize this a little because I will integrate Scene2D.
About the Author
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.