Skip to content Skip to sidebar Skip to footer

Using Pathmodifier Or Moveymodifier To Simulate Sprite Jumping

I am using this method in AndEngine to determine the scene being touched by the user. @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { i

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:

  1. Create a variable boolean mIsJumping in your scene; When the jump starts - set it to true. If the user taps the screen and mIsJumping == true, don't jump.
  2. Now, register a ContactListener to your PhysicsWorld. Whenever there is contact between the player and the ground, set mIsJumping to false.

There are many samples of using ContactListeners in AndEngine forums, a quick search yields some. If you need an example, you can ask for one :)

EDIT 2:ContactListener sample:

  1. Have 2 variables to hold IDs for the player and the ground: private static final String PLAYER_ID = "player", GROUND_ID = "ground";
  2. When you create the ground body and the player body, set their IDs as the user data: playerBody.setUserData(PLAYER_ID); and groundBody.setUserData(GROUND_ID);
  3. 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) { }
    };
    
  4. 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"