How To Throw/fling The Ball By Swiping On It Using Andengine?
Solution 1:
You need to override the onAreaTouched method of Sprite as shown below. You can get the information on the touch event from your pSceneTouchEvent, pTouchAreasLocalX, and pTouchAreaLocalY variables and use them to determine which way to move the ball.
You don't want to apply any forces to the physics body inside of the OnAreaTouched method though because changes to physics bodies should be made using an update handler. I would suggest having the onAreaTouched method set a flag and some other variables so that the next time the update handler runs it can use those values.
Update: I added some code to help you figure out the direction. The comments inside the if statements should explain when they are called. Basically, you get the initial touch position (action down), calculate where you moved to (action move) and use the direction to apply the force in the update handler (action up).
mSprite = newSprite(x, y ,mRegion, mEngine.getVertexBufferObjectManager()){
@OverridepublicbooleanonAreaTouched(final TouchEvent pSceneTouchEvent, finalfloat pTouchAreaLocalX, finalfloat pTouchAreaLocalY)
{
//set flag member variable that sprite has been touchedif (pSceneTouchEvent.isActionDown())
{
//here is where the touch was initiated so you //can store the x,y location. You obtain it by using pSceneTouchEvent.getX()// and pSceneTouchEvent.getY()
}
if (pSceneTouchEvent.isActionMove())
{
//This will be called when you slide your finger, so you//can get the new coordinates by again using pSceneTouchEvent.getX()// and pSceneTouchEvent.getY()
}
if (sSceneTouchEvent.isActionUp())
{
//this will be called when you release the sprite// and tell the update handler to apply the force
}
}
};
this.registerUpdateHandler(newIUpdateHandler(){
@OverridepublicvoidonUpdate(float pSecondsElapsed) {
//if flag is set apply a force to the physics body//set flag to false to wait for next touch event
}
@Overridepublicvoidreset() {
}
Solution 2:
jteezy's answer isn't enough because you need to handle touchevents out of the Sprite too. To handle all touch events on scene first your class must implement IOnSceneTouchListener. Then you should override public boolean onSceneTouchEvent(Scene pScene, final TouchEvent pSceneTouchEvent). Then call and call that method on your scene yourscene.setOnSceneTouchListener( this );
Here is the code:
publicclassGameManagerimplementsIOnSceneTouchListener{
privatebooleanmSelected=false;
privatebooleanmThrown=false;
privatefloat mX;
privatefloat mY;
privatefloat velX;
privatefloat velY;
private TouchEvent mTouchEvent[] = {null, null, null, null, null};
callThatMethodSomeWhere(){
mScene.setOnSceneTouchListener( this );
mİşaretçiSprite = newSprite( 0, 0, mİşaretçiTextureRegion, mEngine.getVertexBufferObjectManager() );
}
@OverridepublicbooleanonSceneTouchEvent(Scene pScene, final TouchEvent pSceneTouchEvent)
{
if( pSceneTouchEvent.isActionDown() && mİşaretçiSprite.contains( pSceneTouchEvent.getX() , pSceneTouchEvent.getY())){ //This part is so important that you check if the touch w
mX = pSceneTouchEvent.getX();
mY = pSceneTouchEvent.getY();
mSelected = true;
}
elseif( pSceneTouchEvent.isActionMove() && mSelected ){
if( mTouchEvent[0] == null ){
mTouchEvent[0] = pSceneTouchEvent;
}elseif( mTouchEvent[1] == null ){
mTouchEvent[1] = pSceneTouchEvent;
}elseif( mTouchEvent[2] == null ){
mTouchEvent[2] = pSceneTouchEvent;
}elseif( mTouchEvent[3] == null ){
mTouchEvent[3] = pSceneTouchEvent;
}elseif( mTouchEvent[4] == null ){
mTouchEvent[4] = pSceneTouchEvent;
}else{
velX = mTouchEvent[4].getX() - mX;
velY = mTouchEvent[4].getY() - mY;
mThrown = true;
mSelected = false;
Body.applyForceToCenter( velX*10, velY*10);
}
}
elseif ( pSceneTouchEvent.isActionUp() && mSelected ){
velX = pSceneTouchEvent.getX() - mX;
velY = pSceneTouchEvent.getY() - mY;
mBody.applyForceToCenter( velX*10, velY*10);
mThrown = true;
}
returnfalse;
}
Edit: I forgot to say that you can increase the number of touchevents you store but it is better to do it like that to give the player to swipe in a limited time. also you can multiply velX and velY with higher values to throw it faster.
Post a Comment for "How To Throw/fling The Ball By Swiping On It Using Andengine?"