5

我正在开发一个游戏AndEngine。这样我就可以在 中移动对象Right to LeftTop to Bottom反之亦然。但是,我的问题是如何将 Sprite 对象移动到Direction of Fling? 这意味着如果用户 Fling 在任何方向上,Sprite 对象都应该在 fling 的坐标上移动并且应该继续移动。

如果有人可以建议,如何获得精确的X and Y co-ordinates也可以,我可以设法自己在坐标上移动 Sprite 对象。

您还可以查看视频- Pirates Subs

在视频中,我正在寻找的东西,从任何方向LauncherFLING

提前致谢。苏里萨哈尼。

4

2 回答 2

4

Well, you can try this code.....

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

c = y1 - (slope * x1);

onFling() method look's like this, for all direction.

@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;
}
于 2011-09-16T10:41:58.347 回答
0

我根据上面的示例创建了这个:

公共抽象类 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();

}

于 2013-12-19T20:25:39.667 回答