让一个精灵从Android应用程序中的墙/边缘反弹

时间:2017-03-30 14:27:11

标签: java android coordinates sprite formula

最有学问的朋友

我有一个精灵在屏幕上移动,但此刻它只是从左向右倾斜移动而离开屏幕然后又回到另一边。

我希望它能以随机的方式从屏幕的边缘反弹,但是,并不是数学上的所有问题,我努力想出坐标来做到这一点。

以下是我到目前为止:(这是Sprite类的更新代码:

   public class Sprite {

    //x,y position of sprite - initial position (0,50)
   // int [] DIRECTION_TO_ANIMATION_MAP = {3, 1, 0, 2};
private static final int BMP_ROWS = 3;
private static final int BMP_COLUMNS = 4;
private int x = 0;
private int y = 0;
private int xSpeed = 5;//Horizontal increment of position (speed)
private int ySpeed;// Vertical increment of position (speed)
private int currentFrame = 0;
private GameView gameView;
private Bitmap spritebmp;
//Width and Height of the Sprite image
private int bmp_width;
private int bmp_height;
// Needed for new random coordinates.
//private Random random = new Random();


public Sprite(GameView gameView) {
    this.gameView = gameView;
    spritebmp = BitmapFactory.decodeResource(gameView.getResources(),
            R.drawable.running_ninja_sprite);
    this.bmp_width = spritebmp.getWidth() / BMP_COLUMNS;
    this.bmp_height = spritebmp.getHeight() / BMP_ROWS;
    /*Random rnd = new Random(System.currentTimeMillis());
    xSpeed = rnd.nextInt(45)-5;
    ySpeed = rnd.nextInt(25)-5;*/
}

//update the position of the sprite
public void update() {

    //if (x < 0 || x > gameView.getWidth() ){ xSpeed = xSpeed * -1;}
    //if (y < 0 || y > gameView.getHeight() ){ ySpeed = ySpeed * -1;}
    if (x > gameView.getWidth() - bmp_width - xSpeed || x + xSpeed < 0) {
        xSpeed = -xSpeed;
    }
    x = x + xSpeed;
    if (y > gameView.getHeight() - bmp_height - ySpeed || y + ySpeed < 0) {
        ySpeed = -ySpeed;
    }
    y = y + xSpeed;
    currentFrame = ++currentFrame % BMP_COLUMNS;
    //y = random.nextInt(gameView.getWidth());
    //wrapAround(); //Adjust motion of sprite.
}

public void draw(Canvas canvas) {
    update();
    int srcX = currentFrame * bmp_width;
    int srcY = 1 * bmp_height; //getAnimationRow()
    Rect src = new Rect(srcX, srcY, srcX + bmp_width, srcY + bmp_height);
    Rect dst = new Rect(x, y, x + bmp_width, y + bmp_height);
    //Draw sprite image
    canvas.drawBitmap(spritebmp, x, y, null);


}

/*private int getAnimationRow() {
    double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
    int direction = (int) Math.round(dirDouble) % BMP_ROWS;
    return DIRECTION_TO_ANIMATION_MAP[direction];
}*/

public void wrapAround() {
    //Code to wrap around
    if (x < 0) x = x + gameView.getWidth(); //increment x whilst not off screen
    if (x >= gameView.getWidth()) { //if gone of the right sides of screen
        x = x - gameView.getWidth(); //Reset x
    }
    if (y < 0) y = y + gameView.getHeight();//increment y whilst not off screen
    if (y >= gameView.getHeight()) {//if gone of the bottom of screen
        y -= gameView.getHeight();//Reset y
    }
}

    // Checks if the sprite was touched

public boolean wasItTouched(float ex, float ey) {
    boolean touched = false;
    if ((x <= ex) && (ex < x + bmp_width) &&
            (y <= ey) && (ey < y + bmp_height)) {
        touched = true;
    }
    return touched;
    }
    }

它现在会从边缘反弹,但从左上角,对角线向右,向后朝同一方向反复,并继续前后移动。在击中边缘后,我希望它在方向上是随机的。有什么建议?正如你从代码中看到的那样,我已经尝试了很多东西,但是精灵只是继续进行这种连续的来回对角运动,我不知道我能做些什么。

由于

1 个答案:

答案 0 :(得分:0)

每次撞墙时都会忽略你的速度

if (x < 0 || x > gameView.getWidth() ){ xSpeed = xSpeed * -1;}
if (y < 0 || y > gameView.getHeight() ){ ySpeed = ySpeed * -1;}

///////////////////////////////编辑

它会适合这样的东西。您也可以删除不再适用的换行功能

//update the position of the sprite
public void update() {

    x = x + xSpeed;
    y = y + xSpeed;
    bounce();

}

private void bounce(){
    if (x <= 0 || x >= gameView.getWidth() ){ xSpeed = xSpeed * -1;}
    if (y <= 0 || y >= gameView.getHeight() ){ ySpeed = ySpeed * -1;}

}