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

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

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

问题

  1. public class Arrow {
  2. protected static int x;
  3. protected static int y;
  4. public void setA(boolean a) {
  5. this.a = a;
  6. }
  7. public void setB(boolean b) {
  8. this.b = b;
  9. }
  10. public void setAb(boolean ab) {
  11. this.ab = ab;
  12. }
  13. public Arrow(int x1, int y1) {
  14. this.x = x1;
  15. this.y = y1;
  16. }
  17. public double getySpeed() {
  18. return (-ySpeed * Time + Time * Time / 10);
  19. }
  20. public boolean getX() {
  21. return x + Math.abs(xSpeed * Time) < canvasWidth - 90;
  22. }
  23. public boolean getY() {
  24. return y + getySpeed() < canvasHeight - 110;
  25. }
  26. public Matrix getArrow() {
  27. Matrix matrix = new Matrix();
  28. matrix.postRotate((int) getAngle(), arrowWidth / 2, arrowHeight / 2);
  29. if (a && !ab) {
  30. return a(x);
  31. }
  32. if (b && !ab) {
  33. return b(y);
  34. }
  35. if (ab) {
  36. x = x + (int) Math.abs(xSpeed * Time);
  37. y = y + (int) getySpeed();
  38. matrix.postTranslate(x, y);
  39. }
  40. return matrix;
  41. }
  42. public Matrix b(int yy) {
  43. Matrix matrix = new Matrix();
  44. matrix.postRotate((int) getAngle(), arrowWidth / 2, arrowHeight / 2);
  45. matrix.postTranslate(canvasWidth - 90, yy);
  46. return matrix;
  47. }
  48. public Matrix a(int xx) {
  49. Matrix matrix = new Matrix();
  50. matrix.postRotate((int) getAngle(), arrowWidth / 2, arrowHeight / 2);
  51. matrix.postTranslate(xx, canvasHeight - 100);
  52. return matrix;
  53. }
  54. }

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

英文:
  1. public class Arrow {
  2. protected static int x;
  3. protected static int y;
  4. public void setA(boolean a) {
  5. this.a = a;
  6. }
  7. public void setB(boolean b) {
  8. this.b = b;
  9. }
  10. public void setAb(boolean ab) {
  11. this.ab = ab;
  12. }
  13. public Arrow( int x1, int y1) {
  14. this.x=x1;
  15. this.y=y1;
  16. }
  17. public double getySpeed(){
  18. return (-ySpeed*Time+Time*Time/10);
  19. }
  20. public boolean getX(){
  21. return x +Math.abs(xSpeed * Time)&lt;canvasWidth-90;
  22. }
  23. public boolean getY(){
  24. return y+getySpeed()&lt;canvasHeight-110;
  25. }
  26. public Matrix getArrow(){
  27. Matrix matrix = new Matrix();//1140,540
  28. matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
  29. if (a&amp;&amp;!ab) {
  30. // here if i do sout(x) it will show 75 which is the value i gave it in the constructor
  31. return a(x);
  32. }
  33. if(b&amp;&amp;!ab){
  34. // here if i do sout(y) it will show 125 which is the value i gave it in the constructor
  35. return (b(y));
  36. }
  37. if(ab){
  38. x =x+(int) Math.abs(xSpeed * Time);
  39. y = y+(int)getySpeed();
  40. matrix.postTranslate(x,y );
  41. }
  42. return matrix;
  43. }
  44. public Matrix b(int yy ){
  45. Matrix matrix = new Matrix();
  46. matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
  47. matrix.postTranslate(canvasWidth-90,yy );
  48. return matrix;
  49. }
  50. public Matrix a(int xx ){
  51. Matrix matrix = new Matrix();
  52. matrix.postRotate((int)getAngle(), arrowWidth/2, arrowHeight / 2);
  53. matrix.postTranslate(xx,canvasHeight-100 );
  54. return matrix;
  55. }

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,使它们成为类变量,这意味着它们的值对于所有对象都是相同的。

  1. protected int x;
  2. 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.

  1. protected static int x;
  2. 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:

确定