Как я могу изменить значение х и у в if (ab) в getArrow () локально - PullRequest
0 голосов
/ 15 марта 2020
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()

Спасибо: *)

1 Ответ

0 голосов
/ 15 марта 2020

Причина проблемы, с которой вы сталкиваетесь, заключается в том, что вы объявили x и y как static, делая их переменными класса, что означает, что их значения будут одинаковыми для всех объектов.

protected  static int x;
protected  static int y;

Просто удалите термин static из их декларации.

Не стесняйтесь комментировать в случае каких-либо сомнений / проблем.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...