0

我正在用 Java 编写一个平台游戏,其中有各种游戏对象,例如平台、玩家和(最终)敌人。我用矩形来描述这些游戏对象的位置:x 位置、y 位置、宽度和高度。但是,我还想添加其他变量来描述位置:左、上、右和下。对于最后两个,我知道每当修改 x、y、宽度或高度时我都需要更改它们,但由于 left 和 top 与 x 和 y 相同,我想知道如何让它们指向相同值作为 x 和 y。我认为这可以在 C 中使用 #define 函数来完成,但遗憾的是这是 Java,而不是 C。

如何让两个不同的变量名指向 Java 中的相同值,这样如果一个变量名发生变化,另一个变量名也会发生变化?

编辑:这是我的 GameObject 类的基础知识:

 public abstract class GameObject {
    protected float x;
    protected float y;

    protected float right;
    protected float bottom;
    protected float width;
    protected float height;

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public void setX(float x) {
        this.x = x;
        right=this.x+width;
    }

    public void setY(float y) {
        this.y = y;
        bottom=this.y+height;
    }
    //A bunch of other getters/setters

    //Subclasses must have render (for displaying graphics) and tick (mainly for updating position) functions.
    public abstract void render(Graphics g);

    public abstract void tick();
}

我想做的是再添加两个变量:

protected float left = x;
protected float top = y;

并让这些引用与 x 和 y 相同的原语(不复制原语)。显然,这似乎是不可能的。感谢您的回答!

4

5 回答 5

1

就您的意思而言,这不能在 Java 中完成。你可以有两个指向同一个对象的对象引用,但是如果你有一个普通的原始变量,它们总是不同的并且不会相互引用。你可以一个对象持有一个原语,对同一个对象有两个不同的引用,并且通过一个引用对该对象内容的更改将反映在另一个引用中,但你不能有一个int xand 和int ywhere 写作x = 10会做y == 10

于 2018-09-02T19:09:57.320 回答
1

如果您确实需要一些行为类似于对原语的引用,则需要创建一个包含该原语值的对象,然后共享“持有者”对象。

但是,我认为这不是解决您的问题的最佳方法。

据我了解您的游戏对象,它们可以完全用xy和变量来描述。, ,和另一方面,是可以从四个基本值中得出的值。heightwidthlefttoprightbottom

事实上,可以有一个仅包含 、 和 的游戏对象x,并y在需要时计算对象之外的其他四个值。这样的对象可能如下所示:heightwidth

public class GameObject {
  private int x, y, height, width;

  public int getX() { return x; }
  public int getY() { return y; }
  public int getHeight() { return height; }
  public int getWidth() { return width; }

  public void setX(int x) { this.x = x; }
  public void setY(int y) { this.y = y; }
  public void setHeight(int height) { this.height = height; }
  public void setwidth(int width) { this.width = width; }
}

请注意,实际变量private只能通过 getter 读取。这有助于避免以后使用变量引用。

现在,要以一种简单而独特的方式访问left、和top,虽然您可以添加四个变量并使它们与已经存在的其他四个变量保持同步,但我认为这不是最好的方法。rightbottom

我建议您在比平均水平稍高的吸气剂中动态计算它们。它看起来像这样:

public class GameObject {
  // getters and setters omitted for brevity
  private int x, y, height, width;

  public int getLeft() {
    return x;
  }

  public int getRight() {
    // assuming x increase toward the right
    return x + width;
  }

  public int getTop() {
    return y
  }

  public int getBottom() {
    // assuming y increase toward the bottom
    return y + height;
  }
}

moveRight(int delta)如果您需要创建一些特殊的更新方法,例如,这也会有所帮助,scale(double factor)因为您只需要将更改应用到基本变量而不是从它们派生的值。

于 2018-09-02T19:39:20.177 回答
0

在java中有两种类型的变量。1.原始类型 2.引用类型

如果要通过不同的变量引用同一个对象,则需要使用引用类型变量。

对于您的游戏应用程序,这些参数可能会经常更改。因此,如果所有时间值,left 和 top 都与 x 和 y相同,则可以使用一个变量来表示这两个参数,从而节省内存。例如:对于left 和x,使用一个变量。

或者使用 java OOP 概念,您可以将这些 x、y、left、top 参数设为私有并为它们分配 getter 和 setter。

public int setX(int x){
    this.x =x;
}

public setLeft(int left){
    this.left = left;
}

public int setY(int y){
    this.y =y;
}

public setTop(int top){
    this.top= top;
}

public setXnLeft(int xnleft){
    setX(xnleft);
    setleft(xnleft);
}

public setYnTop(int yntop){
    setY(yntop);
    setTop(yntop);
}
于 2018-09-02T04:49:56.210 回答
0

这可以通过创建一个新类轻松完成。Java 是面向对象的,所以基本上,一切都是关于类和实例的。看看下面的例子:

class GameObject
{
     //Instance variables
     //(all objects of this class will have their own)
     int x;
     int y;
     int width;
     int height;

     //Constructor
     //(you call a constructor when creating a new object)
     GameObject(int x, int y, int width, int height)
     {
          //Here you are assigning the values received to the instance variables (marked with "this", that represents the current object)
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
     }

    //This method will return the ordinate ("y") of the object's top position (assuming the "y axis" points down)
    int getTop() {
        return (y - height/2);
    }
}

创建类GameObject后,您可以创建它的实例(对象)。往下看:

GameObject player = new GameObject(0, 0, 50, 50); //creates a new object with coordinates (0,0), width = 50 and height = 50
GameObject enemy1 = new GameObject(30, 50, 100, 100);

//Getting the "x" position of the player and the monster
System.out.println("Player X = " + player.x);
System.out.println("Enemy1 X = " + enemy1.x);

//Get the top position "y" 
System.out.println("Player top = " + player.getTop());

//Change player's "y"
player.y = 10;

//Get player's top position again
System.out.println("Player top = " + player.getTop()); //it will have changed, since "y" was changed.

Oracle 有一个关于面向对象编程的非常好的课程,您应该在这里查看。C 语言是结构化的,而 Java 是面向对象的,这是两种不同的范式。我希望这会有所帮助。干杯!

于 2018-09-02T04:43:18.190 回答
0

int/integer 按值传递,而不是在 java 中按引用传递。

您可以定义一个新类,例如,

public class Position {
     int x;
     int y;
}

让两个变量指向同一个 Position 实例。

于 2018-09-02T04:10:15.993 回答