Reputation: 26971
I am using andengine to implement a sprite being able to be dragged across the screen using this.
So what i want to do is when the user taps any where on the screen make the sprite jump.
or move up and then move down.
What is the best way to go bout doing this with andengine?
Upvotes: 4
Views: 2144
Reputation: 9115
This should work:
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(pSceneTouchEvent.isActionDown()) { //Jump only if the user tapped, not moved his finger or something
final Entity playerEntity = ...;//Get player entity here.
final float jumpDuration = 2;
final float startX = playerEntity.getX();
final float jumpHeight = 100;
final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration / 2, startX, startX - jumpHeight); // - since we want the sprite to go up.
final MoveYModifier moveDownModifier = new MoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);
playerEntity.registerEntityModifier(modifier);
return true;
}
return false;
}
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.
Upvotes: 3
Reputation: 625
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
Upvotes: 1