gpt4 book ai didi

android - 如何使用 AndEngine 在 Fling/Swipe 方向(对角线)移动 Sprite

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:49:27 27 4
gpt4 key购买 nike

我正在 AndEngine 中开发游戏。因为我能够在从右到左从上到下 移动对象,反之亦然。但是,我的问题是如何在 Direction of Fling 中移动 Sprite 对象?这意味着如果用户向任何方向 Fling,Sprite 对象应该在 Fling 的坐标上移动,并且应该继续移动。

如果有人可以建议如何获得精确的 X 和 Y 坐标,我可以自己在坐标上移动 Sprite 对象。

您还可以看到视频 - Pirates Subs

在视频中,FLING 上出现的 Launcher 是我从任何方向寻找的东西。

提前致谢。苏瑞·萨哈尼。

最佳答案

好吧,你可以试试这段代码......

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

关于android - 如何使用 AndEngine 在 Fling/Swipe 方向(对角线)移动 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7320441/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com