Using Pathmodifier Or Moveymodifier To Simulate Sprite Jumping
Solution 1:
Use the MoveYModifier
. remember, you can register as many modifiers as you want. So if, for example, the game a platform game and the character always moves on the X axis, and he can jumpt if he wants (Like Gravity Guy or Yoo Ninja, although these games change the gravity which is something else).
You could do like:
EntityplayerEntity= ..//It doesn't matter if the player is a sprite, animated sprite, or anything else. So I'll just use Entity here, but you can declare your player as you wish.finalfloatjumpDuration=2;
finalfloatstartX= playerEntity.getX();
finalfloatjumpHeight=100;
finalMoveYModifiermoveUpModifier=newMoveYModifier(jumpDuration / 2, startX, startX + jumpHeight);
finalMoveYModifiermoveDownModifier=newMoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
finalSequenceEntityModifiermodifier=newSequenceEntityModifier(moveUpModifier, moveDownModifier);
playerEntity.registerEntityModifier(modifier);
EDIT:
For your second question:
- Create a variable
boolean mIsJumping
in your scene; When the jump starts - set it totrue
. If the user taps the screen andmIsJumping == true
, don't jump. - Now, register a ContactListener to your
PhysicsWorld
. Whenever there is contact between the player and the ground, setmIsJumping
tofalse
.
There are many samples of using ContactListener
s in AndEngine forums, a quick search yields some. If you need an example, you can ask for one :)
EDIT 2:ContactListener
sample:
- Have 2 variables to hold IDs for the player and the ground:
private static final String PLAYER_ID = "player", GROUND_ID = "ground";
- When you create the ground body and the player body, set their IDs as the user data:
playerBody.setUserData(PLAYER_ID);
andgroundBody.setUserData(GROUND_ID);
Create a
ContactListener
as a field in your scene:private ContactListener mContactListener = new ContactListener() { /** * Called when two fixtures begin to touch. */publicvoidbeginContact (Contact contact) { final Body bodyA = contact.getFixtureA().getBody(); final Body bodyB = contact.getFixtureB().getBody(); if(bodyA.getUserData().equals(PLAYER_ID)) { if(bodyB.getUserData().equals(GROUND_ID)) mIsJumping = false; } elseif(bodyA.getUserData().equals(GROUND_ID)) { if(bodyB.getUserData().equals(PLAYER_ID)) mIsJumping = false; } } /** * Called when two fixtures cease to touch. */publicvoidendContact (Contact contact) { } /** * This is called after a contact is updated. */publicvoidpreSolve(Contact pContact) { } /** * This lets you inspect a contact after the solver is finished. */publicvoidpostSolve(Contact pContact) { } };
Lastly, register that contact listener:
physicsWorld.setContactListener(mContactListener);
EDIT 3:
To move your sprite over the X axis, you can apply a force using Body.applyForce
method, or apply an impulse using Body.applyLinearImpulse
method. Play around with the arguments and find what works the next.
The vector should consist a X part only; Try Vector2 force = Vector2Pool.obtain(50, 0);
. Then apply the force this way: body.applyForce(force, body.getWorldCenter());
.
Post a Comment for "Using Pathmodifier Or Moveymodifier To Simulate Sprite Jumping"