- Using PathModifier or MoveYModifier to simulate sprite jumping
- @Override
- public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
- if(pSceneTouchEvent.isActionDown()) {
- Log.e("Arcade", "Scene Tapped");
- //Simulate player jumping
- }
- return false;
- }
- @Override
- public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
- if(pSceneTouchEvent.isActionDown()) {
- Log.e("Arcade", "Scene Tapped");
- final Vector2 velocity = Vector2Pool.obtain(mPhysicsWorld.getGravity().x * -0.5f,mPhysicsWorld.getGravity().y * -0.5f);
- body.setLinearVelocity(velocity);
- Vector2Pool.recycle(velocity);
- return true;
- }
- return false;
- }
- Entity playerEntity = ..//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.
- final float jumpDuration = 2;
- final float startX = playerEntity.getX();
- final float jumpHeight = 100;
- final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration / 2, startX, startX + jumpHeight);
- final MoveYModifier moveDownModifier = new MoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
- final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);
- playerEntity.registerEntityModifier(modifier);
- private ContactListener mContactListener = new ContactListener() {
- /**
- * Called when two fixtures begin to touch.
- */
- public void beginContact (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;
- }
- else if(bodyA.getUserData().equals(GROUND_ID)) {
- if(bodyB.getUserData().equals(PLAYER_ID))
- mIsJumping = false;
- }
- }
- /**
- * Called when two fixtures cease to touch.
- */
- public void endContact (Contact contact) { }
- /**
- * This is called after a contact is updated.
- */
- public void preSolve(Contact pContact) { }
- /**
- * This lets you inspect a contact after the solver is finished.
- */
- public void postSolve(Contact pContact) { }
- };