开发者社区> 问答> 正文

我如何在getArrow()的if(ab)中本地更改x和y的值

public class Arrow {
protected  static int x;
protected  static int y;

public void setA(boolean a) {
    this.a = a;
}

public void setB(boolean b) {
    this.b = b;
}

public void setAb(boolean ab) {
    this.ab = ab;
}

public Arrow( int x1, int y1) {
   this.x=x1;
    this.y=y1;
}
public double getySpeed(){

    return (-ySpeed*Time+Time*Time/10);
}
public boolean getX(){
    return x +Math.abs(xSpeed * Time)<canvasWidth-90;
}
public boolean getY(){
    return y+getySpeed()<canvasHeight-110;
}

public Matrix getArrow(){
    Matrix matrix = new Matrix();//1140,540

    matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
    if (a&&!ab) {
       // here if i do sout(x) it will show 75 which is the value i gave it in the constructor
        return a(x);
    }
   if(b&&!ab){
        // here if i do sout(y) it will show 125 which is the value i gave it in the constructor
       return (b(y));
    }
    if(ab){
            x =x+(int) Math.abs(xSpeed * Time);
            y = y+(int)getySpeed();
            matrix.postTranslate(x,y );
        }
    return matrix;
}
public Matrix b(int yy ){
    Matrix matrix = new Matrix();
    matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
    matrix.postTranslate(canvasWidth-90,yy );

    return matrix;
}
public Matrix a(int xx ){
    Matrix matrix = new Matrix();
    matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
    matrix.postTranslate(xx,canvasHeight-100 );

    return matrix;
}

我试图使“位图”箭头停止离开屏幕,以便找出最大的x和y坐标,然后箭头移动直至到达这些坐标。

在getArrow(),在if(ab)块,我认为我做的是改变x和y,但在现实中,他们没有改变。-它们保持与构造函数中给定的相同值。

如何将x和y中的值更改为我在if(ab)中给他们的值 getArrow()

谢谢 :*)

问题来源:Stack Overflow

展开
收起
montos 2020-03-24 14:20:26 468 0
1 条回答
写回答
取消 提交回答
  • 之所以你所面临的问题,是因为你已经声明x,并y为static使它们类变量,这意味着它们的值将是所有的对象是相同的。

    protected  static int x;
    protected  static int y;
    

    只需static从其声明中删除该术语即可。

    如有任何疑问/问题,请随时发表评论。

    回答来源:Stack Overflow

    2020-03-24 14:20:49
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载