Scene with GestureDetector

  ... tutorials on how to use AndEngine.

Scene with GestureDetector

Postby rkpost » Mon Oct 11, 2010 1:27 am

Hi Nicolas!

Maybe this will be usefull in order to control the game with all the screen.

:exclamation: :lightning: This became a part of AndEngine. The class to use is: SurfaceGestureDetector :lightning: :exclamation:


Only it's necesary set this scene with "setChildScene" and overwrite methods like onDown, onUp, onLeft, onRight, onSingleTap or onDoubleTap.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.rkpost.games.tam;
  2.  
  3. import org.anddev.andengine.entity.scene.Scene;
  4. import org.anddev.andengine.input.touch.TouchEvent;
  5.  
  6. import android.view.GestureDetector;
  7. import android.view.MotionEvent;
  8. import android.view.GestureDetector.SimpleOnGestureListener;
  9.  
  10. public class TouchControlScene extends Scene {
  11.  
  12.         private GestureDetector mDetector;
  13.        
  14.         public void onSingleTap() {}
  15.         public void onDoubleTap() {}
  16.         public void onUp() {}
  17.         public void onDown() {}
  18.         public void onLeft() {}
  19.         public void onRight() {}
  20.        
  21.         public TouchControlScene() {
  22.                 super(0);
  23.                 this.mDetector = new GestureDetector(new MyGestureDetector());
  24.                 this.setBackgroundEnabled(false);
  25.         }
  26.        
  27.         @Override
  28.         public boolean onSceneTouchEvent(TouchEvent pSceneTouchEvent) {
  29.                 mDetector.onTouchEvent(pSceneTouchEvent.getMotionEvent());
  30.                 return true;
  31.         }
  32.        
  33.         public class MyGestureDetector extends SimpleOnGestureListener {
  34.  
  35.                 private static final int SWIPE_MIN_DISTANCE = 120;
  36.  
  37.                 @Override
  38.                 public boolean onSingleTapConfirmed(MotionEvent e) {
  39.                         TouchControlScene.this.onSingleTap();
  40.                         return true;
  41.                 }
  42.  
  43.                 @Override
  44.                 public boolean onDoubleTap(MotionEvent e) {
  45.                         TouchControlScene.this.onDoubleTap();
  46.                         return true;
  47.                 }
  48.  
  49.                 @Override
  50.                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  51.                                 float velocityY) {
  52.  
  53.                         // Horizontal
  54.                         if (Math.abs(velocityX) > Math.abs(velocityY)) {
  55.                                 if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
  56.                                         TouchControlScene.this.onLeft();
  57.                                         return true;
  58.                                 } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
  59.                                         TouchControlScene.this.onRight();
  60.                                         return true;
  61.                                 }
  62.                         // Vertical
  63.                         } else {
  64.                                 if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
  65.                                         TouchControlScene.this.onUp();
  66.                                         return true;
  67.                                 } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
  68.                                         TouchControlScene.this.onDown();
  69.                                         return true;
  70.                                 }
  71.                         }
  72.  
  73.                         return false;
  74.                 }
  75.  
  76.         }
  77.  
  78. }
  79.  
Parsed in 0.016 seconds, using GeSHi 1.0.8.4
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: Touch Control Scene

Postby cschua » Mon Oct 11, 2010 1:44 am

just commenting cuz I want to try this out when I have time.
I like what you're doing here because we can specify which on method we want to override. but i think nicolas did it his way to make it easier for most ppl that will use the engine. anyways thanks for sharing.
Published: Another Strobe Light, Nitey Night Light, Live Wallpaper ASL, Score Keeper, etc.

Future: Happy Halloween Game (using AndEngine)
Castle Game (using AndEngine)
Another Live Wallpaper (using AndEngine)
Etc.
cschua
 
Posts: 48
Joined: Sat Sep 11, 2010 11:47 pm

Re: Scene with GestureDetector

Postby Nicolas Gramlich » Mon Oct 11, 2010 11:47 am

Hi rkpost,

thanks, I added this in a derived form to the AndEngine codebase.

Changeset: http://code.google.com/p/andengine/sour ... 823ea38092

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Scene with GestureDetector

Postby Muddasir » Mon Dec 26, 2011 1:56 pm

@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());

// Creates the Scene and sets the background
this.mMainScene = new Scene();
this.mMainScene.setBackground(new ColorBackground(1f, 1f, 1f));
Sprite back = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT,this.mBackTextureRegion);

mMainScene.setBackground(new SpriteBackground(back));



final int centerX = (CAMERA_WIDTH - this.mTexturebottleRegion.getWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mTexturebottleRegion.getHeight()) / 2;

/* Create the face and add it to the scene. */
bottle = new Sprite(centerX - 180, centerY - 145,this.mTexturebottleRegion);
mMainScene.attachChild(bottle);

final Sprite bottle2 = new Sprite(centerX - 60, centerY - 145,this.mTexturebottleRegion2);
mMainScene.attachChild(bottle2);

final Sprite bottle3 = new Sprite(centerX + 60, centerY - 145,this.mTexturebottleRegion3);
mMainScene.attachChild(bottle3);

final Sprite bottle4 = new Sprite(centerX + 180, centerY - 145,this.mTexturebottleRegion4);
mMainScene.attachChild(bottle4);

stone = new Sprite(centerX - 20, centerY + 350, mTexturestoneRegion);
mMainScene.setOnSceneTouchListener(new IOnSceneTouchListener()
{
// @Override
// public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float newX, final float newY) {
//
//
// float X1 = pSceneTouchEvent.getX();
// float Y1 = centerX - 20; // Set start point (X1,Y1)
// float X2 = pSceneTouchEvent.getX();
// float Y2 = centerY + 350; // Set end point (X2,Y2)
//
// float slope = (Y2 - Y1) / (X2 - X1);
// float angle = (float) Math.atan(slope);
// float angleInDegree = (float) Math.toDegrees(angle);
//
// float C = Y1 - (slope * X1);
//
// Log.i("Angle",""+angle );
// Log.i("Angle In Degree",""+angleInDegree );
//
//
// Line line = new Line(X1, Y1, X2, Y2, 15.0f); // Draw the line initially, and specify the weight of the line
// line.setLineWidth(10f);
// mMainScene.attachChild(line);// Add the line to the scene on top of everything else
//
// Log.i("TEST_ACT", "Start point: (" + X1 + ", " + Y1 + ")");
//
//
//
// return true;
// }
float X1,Y1,X2,Y2;
@Override
public boolean onSceneTouchEvent(Scene pScene,TouchEvent pSceneTouchEvent) {
if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
{
X1 = centerX+10 ;
Y1 = centerY + 360; // Set start point (X1,Y1)
X2 = pSceneTouchEvent.getX();
Y2 = pSceneTouchEvent.getY(); // Set end point (X2,Y2)

if(Y2>580&&Y2<650){
Line line = new Line(X1, Y1, X2, Y2,15.0f);// Draw the line initially, and specify the weight of the line
line.setLineWidth(100f);
mMainScene.attachChild(line); // Add the line to the scene on top of everything else
}
Log.i("TEST_ACT", "Start point: (" + X2 + ", " + Y2 + ")");
}
return false;
}
});




// {
// @Override
// public boolean onAreaTouched(MotionEvent event, MapView mapView) {
// if(event.getAction() == MotionEvent.ACTION_DOWN) {
//
// startX = event.getX();
// startY = event.getY();
// //start the timer, though you may want to periodically reset this as it's not uncommon for the user to hold onto the sprite for a few seconds before deciding where to fling it
//
// } else if (event.getAction() == MotionEvent.ACTION_UP) {
//
// long timerTime = getTime between two event down to Up
// newX = event.getX();
// newY = event.getY();
//
// float distance = Math.sqrt((newX-oldX) * (newX-oldX) + (newY-oldY) * (newY-oldY));
// float speed = distance / timerTime;
// float trajectoryX = startX - newX;
// float trajectoryY = startY - newY;
// //then, send the sprite flying in the direction of trajectoryX, trajectoryY with the momentum speed.
// }
// };
//mMainScene.registerTouchArea(stone);

mMainScene.attachChild(stone);


return this.mMainScene;
}

i am new in andengine and want to implement onfling ... got confused... want the stone to move... where i drag... Tips would be helpfull thankx in advance..
Muddasir
 
Posts: 9
Joined: Tue Dec 13, 2011 8:08 am


Return to Tutorials

Who is online

Users browsing this forum: Google Adsense [Bot] and 18 guests