如何在`getArrow()`函数中局部地更改条件`if(ab)`中的`x`和`y`的值。

huangapple go评论85阅读模式
英文:

how can i change the value of the x and y in if(ab) at getArrow() locally

问题

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();

        matrix.postRotate((int) getAngle(), arrowWidth / 2, arrowHeight / 2);
        if (a && !ab) {
            return a(x);
        }
        if (b && !ab) {
            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;
    }
}

你好,这是你提供的代码的翻译部分。如果你有任何关于代码的问题或需要进一步解释,请随时提问。

英文:
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)&lt;canvasWidth-90;
}
public boolean getY(){
return y+getySpeed()&lt;canvasHeight-110;
}
public Matrix getArrow(){
Matrix matrix = new Matrix();//1140,540
matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
if (a&amp;&amp;!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&amp;&amp;!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;
}

I am trying to make a Bitmap arrow stop from leaving the screen so I figured out the maximum x and y coordinates and the arrow moves until it reaches these coordinates.

In getArrow(), at the if(ab) block, what I think I am doing is changing x and y, but in reality, they're not changing.

  • They stay the same value I gave them in the constructor.

How can I change the x and y in the class to the value I gave them at
if(ab) in getArrow()

Thank you :*)

答案1

得分: 0

你面临的问题的原因是因为你将 xy 声明为 static,使它们成为类变量,这意味着它们的值对于所有对象都是相同的。

protected int x;
protected int y;

只需从它们的声明中移除 static 这个修饰符。

如果有任何疑问或问题,请随意进行评论。

英文:

The reason for the issue you are facing is because you have declared x and y as static making them class variables which means their values will be the same for all objects.

protected  static int x;
protected  static int y;

Just remove the term, static from their declaration.

Feel free to comment in case of any doubt/issue.

huangapple
  • 本文由 发表于 2020年3月15日 21:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/60693176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定