0
private void gotoPos()
{
    spaceX = x2 - x;
    spaceY = y2 - y;
    if (Math.abs(spaceX) >= Math.abs(spaceY)) {
        xSpeed = Math.round(spaceX * (3/Math.abs(spaceX)));
        ySpeed = Math.round(spaceY * (3/Math.abs(spaceX)));
    }

使用此代码,我想将对象移动到位置 x2 和 y2。x 和 y 是对象的当前位置。spaceX 是对象和它应该去的 x 位置之间的空间。spaceY 也是如此。

但我不希望对象每次绘制移动超过 3 个像素。

示例:对象位置:x = 35,y = 22

指向它应该去:x2 = 79,y2 = 46

它们之间的空间:spaceX = 79-35 = 44,spaceY = 46-22 = 24

spaceX 比 spaceY 大,所以:

xSpeed = 44 * (3/44) = 3,ySpeed = 24 * (3/44) = 1.63 = 2

但它不是这样工作的。当我启动应用程序时,对象不会转到 x2 和 y2。如果我改变 xSpeed = spaceX; y速度=空间Y;

物体移动到该位置,但我不希望它立即去那里

完整代码:

public class Sprite
{
private boolean walking = true;
private int actionWalk = 0;
private Random rnd;
private int checkIfAction;
private int nextAction = 0;
static final private int BMP_COLUMNS = 4;
static final private int BMP_ROWS = 4;
private int[] DIRECTION_TO_SPRITE_SHEET = { 1, 0, 3, 2 };
public int x=-1;
private int y=-1;
public int xSpeed;
private int ySpeed;
private int width;
private int height;
private int bottomSpace;
private Bitmap bmp;
private GameView theGameView;
private int currentFrame=0;
private int x2, y2;
private boolean isTouched;
private int spaceX, spaceY;

D

public Sprite(GameView theGameView, Bitmap bmp)
{
    this.theGameView = theGameView;
    this.bmp = bmp;
    this.width = bmp.getWidth() / BMP_COLUMNS;
    this.height = bmp.getHeight() / BMP_ROWS;
    rnd = new Random();
    xSpeed = 0;
    ySpeed = 0;
}

public void shareTouch(float xTouch, float yTouch)
{
    x2 = (int) xTouch;
    y2 = (int) yTouch;
    isTouched = true;
}
    private void gotoPos()
{
    spaceX = x2 - x;
    spaceY = y2 - y;
    if (Math.abs(spaceX) >= Math.abs(spaceY)) {
        xSpeed = Math.round(spaceX * (3/Math.abs(spaceX)));
        ySpeed = Math.round(spaceY * (3/Math.abs(spaceX)));
    }
    else {
        xSpeed = spaceX;
        ySpeed = spaceY;
    }
}

D

private void bounceOff()
{
    bottomSpace = theGameView.getHeight() - y;
    if (x > theGameView.getWidth() - (width * theGameView.getDensity()) - xSpeed - bottomSpace / 2 || x + xSpeed < bottomSpace / 2)
    {
        xSpeed = -xSpeed;
    }
    x = x + xSpeed;
    if (y > theGameView.getHeight() - (height * theGameView.getDensity()) - ySpeed || y + ySpeed < theGameView.getHeight() / 2)
    {
        ySpeed = -ySpeed;
    }
    y = y + ySpeed;
    currentFrame = ++currentFrame % BMP_COLUMNS;
}

d

public void onDraw(Canvas canvas)
{
    if (x == -1)
    {
        x = (theGameView.getWidth() / 2);
        y = (theGameView.getHeight() / 2 + theGameView.getHeight() / 4);
    }
    if (isTouched == true)
    {
        gotoPos();
    }
    /*      if (nextAction == 100)
     {
     action();
     nextAction = 0;
     }
     nextAction += 1;*/
    bounceOff();
    int sourceX, sourceY;
    if (walking == true)
    {
        sourceX = currentFrame * width; 
    }
    else
    {
        sourceX = 0;
    }
    sourceY = getAnimationRow() * height;
    Rect source = new Rect(sourceX, sourceY, sourceX + width, sourceY + height);
    Rect destine = new Rect(x, y, (int) (x + (width * theGameView.getDensity())), (int) (y + (height * theGameView.getDensity())));
    canvas.drawBitmap(bmp, source, destine, null);
}

d

private int getAnimationRow()
{
    double directionDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
    int spriteDir = (int) Math.round(directionDouble) % BMP_ROWS;
    return DIRECTION_TO_SPRITE_SHEET[spriteDir];
}

}

4

3 回答 3

1

简单的问题:您使用整数算术并且不明白这一点:

spaceX * (3/Math.abs(spaceX))

结果0几乎在所有情况下3/x都是x > 3如此0

要使您的程序正常工作,请使用浮点运算或重写您的公式以按预期工作。

要使用浮点算术,您必须更改为

spaceX * (3.0/Math.abs(spaceX))

假设您的变量spaceX也是浮点数。

你也可以使用

(spaceX * 3) / Math.abs(spaceX)

如果您想保留整数(我想是这样)。

于 2013-07-29T10:03:10.770 回答
0

给定点a Vector2d(x, y) 和b Vector2d(x2, y2)-

通过像你一样从a中减去b来创建从ab的向量V。将向量V归一化为单位向量并将其与所需的距离相乘。然后将结果向量添加到点a

更新时:

a.add(b.subtract(a).norm().multiply(d));

根据您的向量实现,正​​确修改上述伪代码。

于 2013-07-29T10:11:44.247 回答
0

您的代码中有一个逻辑谬误:如果Math.abs(spaceX) < Math.abs(spaceY))?然后你的对象根本不会移动。

您计算的“x 距离/y 距离”通常被认为是角度,而不是速度。速度是“距离/时间”。您可以计算距离,并且应该为您的对象确定一个合理的速度。因为你知道你的 fps - 20 -,所以你可以计算你的对象需要在每一帧中移动多少像素。

于 2013-07-29T10:15:29.943 回答