Frage

Ich arbeite an einem Spiel in AndEngine. In diesem Fall kann ich ein Objekt hinein bewegen Right to Left, Top to Bottom und umgekehrt. Aber mein Problem ist, wie kann ich das Sprite -Objekt in das bewegen? Direction of Fling? Dies bedeutet, wenn Benutzer in eine Richtung in eine Richtung flingen, die sich das Sprite-Objekt auf den Koordinaten des Flings bewegen und sich weiterentwickeln sollte.

Wenn jemand darüber vorschlagen kann, wie man genau das bekommt X and Y co-ordinates Ich kann es auch tun, ich kann es schaffen, das Sprite-Objekt selbst auf die Koordinaten zu bewegen.

Sie können auch das sehen VIDEO - Piraten Subs

Im Video die Launcher auf die FLING ist das, wonach ich suche, aus jeder Richtung.

Vielen Dank im Voraus. Suri Sahani.

War es hilfreich?

Lösung

Nun, Sie können diesen Code ausprobieren .....

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

c = y1 - (slope * x1);

Onfling () -Methode sieht so aus, für alle Richtung.

@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;
}

Andere Tipps

Ich habe dies basierend auf dem obigen Beispiel erstellt:

UBLIC abstrakte Klasse OnupdowngestaRelistener implementiert 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();

}

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top