I am using this method in AndEngine to determine the scene being touched by the user.
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(pSceneTouchEvent.isActionDown()) {
Log.e("Arcade", "Scene Tapped");
//Simulate player jumping
}
return false;
}
What i want to do is when the scene is tapped, i want to allow the player to jump.
Now two things for this would it be better to use PathModifier, or MoveYModifier considering it is landscape mode?
If either please provide an example of such.
Thanks
EDIT:
ive managed to use Physics to simulate a jump using this..
@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;
}
As you said in the answer by changing the gravity. The only issue is, when the user keeps touching the screen the sprites keep going up and up and up. How can i set it where the user can only click once and cant make him jump again until the sprite hits the ground, which is a rectangle?
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:
EDIT:
For your second question:
boolean mIsJumpingin your scene; When the jump starts – set it totrue. If the user taps the screen andmIsJumping == true, don’t jump.PhysicsWorld. Whenever there is contact between the player and the ground, setmIsJumpingtofalse.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:
ContactListenersample:private static final String PLAYER_ID = "player", GROUND_ID = "ground";playerBody.setUserData(PLAYER_ID);andgroundBody.setUserData(GROUND_ID);Create a
ContactListeneras a field in your scene: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.applyForcemethod, or apply an impulse usingBody.applyLinearImpulsemethod. 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());.