Java 在android应用程序中让精灵从墙壁/边缘反弹

Java 在android应用程序中让精灵从墙壁/边缘反弹,java,android,coordinates,sprite,formula,Java,Android,Coordinates,Sprite,Formula,最博学的朋友 我有一个精灵在屏幕上四处移动,但现在它只是从左到右成对角线移动,离开屏幕,然后从另一边回来 我希望它能以一种随机的方式从屏幕边缘弹起,但是,我并没有完全掌握数学知识,我正在努力找出实现这一点的坐标 以下是我到目前为止所掌握的内容:(这是Sprite类的更新代码: public class Sprite { //x,y position of sprite - initial position (0,50) // int [] DIRECTION_TO_ANIM

最博学的朋友

我有一个精灵在屏幕上四处移动,但现在它只是从左到右成对角线移动,离开屏幕,然后从另一边回来

我希望它能以一种随机的方式从屏幕边缘弹起,但是,我并没有完全掌握数学知识,我正在努力找出实现这一点的坐标

以下是我到目前为止所掌握的内容:(这是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;
    }
    }
公共类精灵{
//x、 精灵的y位置-初始位置(0,50)
//int[]DIRECTION_TO_ANIMATION_MAP={3,1,0,2};
私有静态final int BMP_ROWS=3;
私有静态final int BMP_列=4;
私有整数x=0;
私有整数y=0;
private int xSpeed=5;//位置的水平增量(速度)
private int ySpeed;//位置的垂直增量(速度)
私有int currentFrame=0;
私人游戏视图游戏视图;
私有位图spritebmp;
//精灵图像的宽度和高度
私有整数bmp_宽度;
私家车高度;
//需要新的随机坐标。
//私有随机=新随机();
公共精灵(GameView GameView){
this.gameView=gameView;
spritebmp=BitmapFactory.decodeResource(gameView.getResources(),
R.可牵引。奔跑(忍者精灵);
this.bmp\u width=spritebmp.getWidth()/bmp\u列;
this.bmp\u height=spritebmp.getHeight()/bmp\u行;
/*Random rnd=新的Random(System.currentTimeMillis());
xSpeed=rnd.nextInt(45)-5;
ySpeed=rnd.nextInt(25)-5*/
}
//更新精灵的位置
公共无效更新(){
//如果(x<0 | | x>gameView.getWidth()){xSpeed=xSpeed*-1;}
//如果(y<0 | | y>gameView.getHeight()){ySpeed=ySpeed*-1;}
如果(x>gameView.getWidth()-bmp_width-xSpeed | | x+xSpeed<0){
xSpeed=-xSpeed;
}
x=x+x速度;
如果(y>gameView.getHeight()-bmp_height-ySpeed | | y+ySpeed<0){
ySpeed=-ySpeed;
}
y=y+x速度;
currentFrame=++currentFrame%BMP_列;
//y=random.nextInt(gameView.getWidth());
//环绕();//调整精灵的运动。
}
公共空白绘制(画布){
更新();
int srcX=当前帧*bmp\u宽度;
int srcY=1*bmp_height;//getAnimationRow()
Rect src=new Rect(srcX,srcY,srcX+bmp\u宽度,srcY+bmp\u高度);
Rect dst=新的Rect(x,y,x+bmp_宽度,y+bmp_高度);
//绘制精灵图像
drawBitmap(spritebmp,x,y,null);
}
/*私有int getAnimationRow(){
double dirDouble=(Math.atan2(xSpeed,ySpeed)/(Math.PI/2)+2);
int方向=(int)数学圆整(dirDouble)%BMP\u行;
返回方向_到动画_贴图[方向];
}*/
公共无效环绕(){
//环绕的代码
如果(x<0)x=x+gameView.getWidth();//在不离开屏幕时增加x
如果(x>=gameView.getWidth()){//if离开屏幕右侧
x=x-gameView.getWidth();//重置x
}
如果(y<0)y=y+gameView.getHeight();//在不离开屏幕时增加y
如果(y>=gameView.getHeight()){//if离开屏幕底部
y-=gameView.getHeight();//重置y
}
}
//检查精灵是否被触摸过
公共布尔值wasItTouched(float-ex,float-ey){
布尔值=假;

如果((x只要在每次撞墙时否定你的速度就行了

if (x < 0 || x > gameView.getWidth() ){ xSpeed = xSpeed * -1;}
if (y < 0 || y > gameView.getHeight() ){ ySpeed = ySpeed * -1;}
if(x<0 | | x>gameView.getWidth()){xSpeed=xSpeed*-1;}
如果(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;}

}
//更新精灵的位置
公共无效更新(){
x=x+x速度;
y=y+x速度;
反弹();
}
私有无效反弹(){
如果(x=gameView.getWidth()){xSpeed=xSpeed*-1;}
如果(y=gameView.getHeight()){ySpeed=ySpeed*-1;}
}

我已经用我尝试过的方法更新了我的问题。也许你可以建议下一步采取什么措施?@PhilAdams编辑。这会让你达到90%的目标。仍然有一些工作要做,以确保它在边界处完美反弹,但我会让你自己去弄清楚。呃,谢谢,但这就是我来这里的原因,因为我不能你明白了。就像我说的,我的数学不是我的强项,所以像这样的东西让我有点迷惑,所以我不认为我能弄明白。但是无论如何,谢谢你,来这里理解没有什么错。不过我们不是来为你做这项工作的。我已经更新了我的问题,修改了Sprite类。更多的代码,但都是一样的结果。它只是从左上角到右下角成对角线,然后朝着同一方向后退,等等