Guest User

Untitled

a guest
May 8th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. Using PathModifier or MoveYModifier to simulate sprite jumping
  2. @Override
  3. public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
  4. if(pSceneTouchEvent.isActionDown()) {
  5. Log.e("Arcade", "Scene Tapped");
  6. //Simulate player jumping
  7.  
  8. }
  9. return false;
  10. }
  11.  
  12. @Override
  13. public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
  14. if(pSceneTouchEvent.isActionDown()) {
  15. Log.e("Arcade", "Scene Tapped");
  16.  
  17. final Vector2 velocity = Vector2Pool.obtain(mPhysicsWorld.getGravity().x * -0.5f,mPhysicsWorld.getGravity().y * -0.5f);
  18. body.setLinearVelocity(velocity);
  19. Vector2Pool.recycle(velocity);
  20. return true;
  21. }
  22. return false;
  23. }
  24.  
  25. 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.
  26.  
  27. final float jumpDuration = 2;
  28. final float startX = playerEntity.getX();
  29. final float jumpHeight = 100;
  30.  
  31. final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration / 2, startX, startX + jumpHeight);
  32. final MoveYModifier moveDownModifier = new MoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
  33. final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);
  34.  
  35. playerEntity.registerEntityModifier(modifier);
  36.  
  37. private ContactListener mContactListener = new ContactListener() {
  38. /**
  39. * Called when two fixtures begin to touch.
  40. */
  41. public void beginContact (Contact contact) {
  42. final Body bodyA = contact.getFixtureA().getBody();
  43. final Body bodyB = contact.getFixtureB().getBody();
  44.  
  45. if(bodyA.getUserData().equals(PLAYER_ID)) {
  46. if(bodyB.getUserData().equals(GROUND_ID))
  47. mIsJumping = false;
  48. }
  49. else if(bodyA.getUserData().equals(GROUND_ID)) {
  50. if(bodyB.getUserData().equals(PLAYER_ID))
  51. mIsJumping = false;
  52. }
  53.  
  54. }
  55.  
  56. /**
  57. * Called when two fixtures cease to touch.
  58. */
  59. public void endContact (Contact contact) { }
  60.  
  61. /**
  62. * This is called after a contact is updated.
  63. */
  64. public void preSolve(Contact pContact) { }
  65.  
  66. /**
  67. * This lets you inspect a contact after the solver is finished.
  68. */
  69. public void postSolve(Contact pContact) { }
  70. };
Advertisement
Add Comment
Please, Sign In to add comment