42 videos
238 minutes
And for all this, what we will do is come to us to PlayerEntity, and taking advantage of is an actor, we override the act () method. Here we have to do two things: first, a jump start if we touched the screen. And then, to advance the player if he is alive. As for jumps, we saw that this could be done in a very convenient way: by checking if just touching the screen, and if so, calling jump (method),
which can take advantage here, as public void jump (). We had also seen that jump () as I did before, so I will copy the code casually and I have selected, it will not be because I have repeated this jack. And I'm going to hit the jump () method. But remember that we do double jump, so first of all we see that we are not jumping. And if we're not jumping, then we began putting the variable jump to true to avoid
you can re-jump. Now what remains is to manage the collision with the ground to ensure that it can undo the jump and can put false to re-jump. And this is easy: first we associate a userData entities, and then build the ContactListener as previously did. What I do is come to my World, and add a new ContactListener. Here comes the boring part, because you know that this part is always complex to write.
So I'll try to write a method of support to reduce logic to write. I'll write a method to check if they have collided in a contact an actor with a particular UserData, a; and an actor with a particular UserData, b. With this, it is easier to write everything then. There will be shocked if, or, A fixture has a UserData UserData is equal to A and B has a UserData Fixture which is equal to UserData B; or if the Fixture of A has a UserData
Like B, and B is UserData A. This is complicated and see I'll put parentheses. We make it easy to manage the collision, because if they hit my player in my contact with my ground, I'm going to do is tell the Player to initiate a jump. Or better, rather than a jump start, I say that the jump is no longer active. True, I need to create a getter and a setter, so let's create a second that. Yes now,
what should happen is that when you collide with the Player floor, finish the jump. With this you can jump but can not do double or triple jump in the air. It only remains as it can jump more times if the mouse is clicked, so what I do is that if the finger is still on the screen, will tell the player must jump again. Recall, as we saw above, we can not put a force in the middle of Contact for the world
It is processed. I create a variable called mustJump, and make jump out, creating a getter and a setter and causing the collision occurs, You put the mustJump to false, and keeps jumping. Thus, now we can do that when you hold your finger, you can keep jumping. All that remains is to adjust the speed to Joe, now that he is alive. So what I do is that finally, if alive, a new leap applies.
I will rename the variable as SPEEDY, to make it more English all. And now what remains is to make sure to be alive when shocks. I will use a new collision, which detects if one player has collided with Spike so that the player is no longer alive. And now should move while alive, and when you crash, you should stop moving. This inertia can use to display a message on the screen that says 'you lost, ha!'.
With this it is ported everything we had previously done with Box2D to what we had done before in Scene2D. Can you keep jumping being dead? That does not interest me, really. I will make only jump if you're not dead, because if not as no. With this we have finished porting the game Box2D to Scene2D so we can do it. We will improve the game now that we have finished.
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.