Making A Sprite Jump When User Taps On The Screen?
Solution 1:
This should work:
@OverridepublicbooleanonSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(pSceneTouchEvent.isActionDown()) { //Jump only if the user tapped, not moved his finger or somethingfinalEntityplayerEntity= ...;//Get player entity here.finalfloatjumpDuration=2;
finalfloatstartX= playerEntity.getX();
finalfloatjumpHeight=100;
finalMoveYModifiermoveUpModifier=newMoveYModifier(jumpDuration / 2, startX, startX - jumpHeight); // - since we want the sprite to go up.finalMoveYModifiermoveDownModifier=newMoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
finalSequenceEntityModifiermodifier=newSequenceEntityModifier(moveUpModifier, moveDownModifier);
playerEntity.registerEntityModifier(modifier);
returntrue;
}
returnfalse;
}
Keep in mind that, if you will use this, whenever there is an ACTION_DOWN
event, you might miss some other event handlers for this (For example, if you have on-screen buttons, they will never handle the event).
The way to handle it is to use add any other entities you want to receive touch events as touch areas for your scene, Scene.registerTouchArea(ITouchArea)
does so. Sprite
s and AnimatedSprite
s implement ITouchArea
, so you can use these.
When a Scene
receives a TouchEvent
to handle, it will first let all the ITouchArea
s you registered handle it, then if non of them consumed it, it will try the onSceneTouchEvenet
method.
Solution 2:
It looks like you would have to set up a listener shown how in this tutorial: http://www.andengine.org/forums/tutorials/updating-sprites-objects-listeners-t386.html then perhaps use a move modifier as shown in this post: how to move sprite object using AndEngine (Android) I hope that you will figure out your problem. ;)
Good Luck- Lijap
Post a Comment for "Making A Sprite Jump When User Taps On The Screen?"