2011-09-06 14 views
5

私はAndEngineでゲームを進めています。その中でオブジェクトをRight to LeftTop to Bottomに移動することができ、その逆も可能です。しかし、私の問題は、Direction of FlingでSpriteオブジェクトを移動するにはどうすればいいですか?これは、フレイルユーザーがフライトの座標上を移動して移動する必要がある任意の方向のスプライトオブジェクトを意味します。AndEngineを使用して、スプライト/スワイプ方向(斜め)にスプライトを移動する方法

誰かがお勧めできるのであれば、正確なX and Y co-ordinatesを取得する方法もありますが、私は自分自身を座標軸上で動かすことができます。あなたはまた、VIDEO見ることができます

からLauncherFLINGに来てビデオでPirates Subs

は、私がどの方向から、探していますものです。

ありがとうございました。 Suri Sahani。

+0

を正確に知っているが、キーダウン座標はありませんし、キーアップの座標は、それらの座標に何かが飛んでいるかドラッグしているかを判断することができます.......... – viv

+0

これは数学についてです。 dスロープとシータ。 – Siten

答えて

4

さて、あなたはこのコードを試すことができます.....

float slope = (y2 - y1)/(x2 - x1); 
float angle = (float) Math.atan(slope); 
float angleInDegree = (float) Math.toDegrees(angle); 

c = y1 - (slope * x1); 

onFling()メソッドは、すべての方向のために、このようなのを見て。

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { 

float c; 
// Checks if LifeLine is there or not and enable or disable Fling 
// Operation. 
float sx = 0, sy = 0; 
float x1 = e1.getX(); 
float y1 = e1.getY(); 

float x2 = e2.getX(); 
float y2 = e2.getY(); 

float slope = (y2 - y1)/(x2 - x1); 
float angle = (float) Math.atan(slope); 
float angleInDegree = (float) Math.toDegrees(angle); 

c = y1 - (slope * x1); 

/** 
* bottom right to left top 
*/ 
if (x1 > x2 && y1 > y2) { 
sx = CAMERA_WIDTH; 
sy = (slope * sx) + c; 

missile = new Missile(sx, sy, 
this.mFaceTextureRegionMissileLeftToRight); 
missile.setVelocity(-(float) (600 * (Math.cos(angle))), 
-(float) (600 * (Math.sin(angle)))); 

scene.getTopLayer().addEntity(missile); 

missile.setRotation(angleInDegree + 180); 

} 
/** 
* left top corner to right 
*/ 
else if (x2 > x1 && y2 > y1) { 
sx = -100; 
sy = (slope * sx) + c; 
missile = new Missile(sx, sy, 
this.mFaceTextureRegionMissileLeftToRight); 
missile.setVelocity((float) (300 * (Math.cos(angle))), 
(float) (300 * (Math.sin(angle)))); 
scene.getTopLayer().addEntity(missile); 
missile.setRotation(angleInDegree); 
} 
/** 
* left bottom corner to right up 
*/ 
else if (x2 > x1 && y1 > y2) { 
sx = -100; 
sy = (slope * sx) + c; 
missile = new Missile(sx, sy, 
this.mFaceTextureRegionMissileLeftToRight); 
missile.setVelocity((float) (300 * (Math.cos(angle))), 
(float) (300 * (Math.sin(angle)))); 
scene.getTopLayer().addEntity(missile); 
missile.setRotation(angleInDegree); 
} 

/** 
* Right corner to left bottom down 
*/ 
else if (x1 > x2 && y2 > y1) { 
sx = CAMERA_WIDTH; 
sy = (slope * sx) + c; 
missile = new Missile(sx, sy, 
this.mFaceTextureRegionMissileLeftToRight); 
missile.setVelocity((float) (-600 * (Math.cos(angle))), 
-(float) (600 * (Math.sin(angle)))); 
scene.getTopLayer().addEntity(missile); 
missile.setRotation(angleInDegree + 180); 
} 
return false; 
} 
+0

この質問のお手伝いができますか? http://stackoverflow.com/questions/20540858/how-to-throw-fling-the-ball-by-swiping-on-it-using-andengine/20549429?noredirect=1#20549429 – Uday

0

私は上記の例に基づいて本を作成しました:ublic抽象クラスOnUpDownGestureListenerが実装

OnTouchListener {

private final GestureDetector gdt = new GestureDetector(new GestureListener()); 

@Override 
public boolean onTouch(final View v, final MotionEvent event) { 
    gdt.onTouchEvent(event); 
    return true; 
} 

private final class GestureListener extends SimpleOnGestureListener { 


    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     click(); 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

     float x1 = e1.getX(); 
     float y1 = e1.getY(); 

     float x2 = e2.getX(); 
     float y2 = e2.getY(); 


     /** 
     * bottom right to left top 
     */ 
     if (x1 > x2 && y1 > y2) { 
      onBottomToTop(); 

     } 
     /** 
     * left top corner to right 
     */ 
     else if (x2 > x1 && y2 > y1) { 
      onTopToBottom(); 
     } 
     /** 
     * left bottom corner to right up 
     */ 
     else if (x2 > x1 && y1 > y2) { 
      onBottomToTop(); 
     } 

     /** 
     * Right corner to left bottom down 
     */ 
     else if (x1 > x2 && y2 > y1) { 
      onTopToBottom(); 
     } 
     return false; 
    } 
} 

public abstract void onRightToLeft(); 

public abstract void click(); 

public abstract void onLeftToRight(); 

public abstract void onBottomToTop(); 

public abstract void onTopToBottom(); 

}

関連する問題