[英]How to create two of the same objects but in different states
I am trying to create at least two independent objects using the same class. 我试图使用同一类创建至少两个独立的对象。 Meaning that I want each of them to use the same class but use the variables in it independently. 这意味着我希望他们每个人都使用相同的类,但要独立使用其中的变量。 I am not sure if this is possible but that is why I am here. 我不确定这是否可能,但这就是为什么我在这里。
The two skeletons (skeleton1 and skeleton2) are the two objects that I want to be independent of each other. 两个骨架(skeleton1和skeleton2)是我要彼此独立的两个对象。
@Override
public void render(GameContainer gc, StateBasedGame gm, Graphics g)
throws SlickException {
background.draw(0, 0, Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
g.drawImage(ccb, mkX, mkY);
Skeleton skeleton1 = new Skeleton(g, gm);
Skeleton skeleton2 = new Skeleton(g, gm);
score = new DeathScreenButton(scoreCounter, (Constants.WIN_WIDTH / 2) / 2, 0, g, Color.transparent);
}
More specifically, I want their x and y axis to be independent shown here (they are sX and sY): 更具体地说,我希望它们的x和y轴独立显示在此处(它们分别是sX和sY):
public class Skeleton {
private Graphics g;
private StateBasedGame gm;
private int mkX = InGame.getmkX();
private int mkY = InGame.getmkY();
// This makes them spawn on each other :(
// "static" prevents them from popping around the screen
private static int sX = (int)(Math.random() * Constants.WIN_WIDTH) + 1;
private static int sY = (int)(Math.random() * Constants.WIN_HEIGHT) + 1;
private Image skele;
public Skeleton (Graphics g, StateBasedGame gm) {
this.g = g;
this.gm = gm;
try {
init();
} catch (SlickException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
render();
try {
update();
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Your Skeletons are independent already. 您的骨骼已经是独立的。 One of Objected-Oriented programming purposes are class instances. 面向对象编程目的之一是类实例。 This way, two instances of the same class may have the same attributes, but they will never be the same . 这样,同一类的两个实例可能具有相同的属性,但是它们永远不会相同 。 It may be a little confusing in the beginning, but the objective is to develop reusable code, then you may instantiate 1..n Skeletons, and they will be independent. 一开始可能有些混乱,但是目标是开发可重用的代码,然后您可以实例化1..n Skeleton,它们将是独立的。
Skeleton skel1 = new Skeleton(Graphics g, StateBasedGame gm);
Hope it was helpful. 希望对您有所帮助。 Thank you. 谢谢。
Write/Overwrite the update
method, if you haven't already. 编写/覆盖update
方法(如果尚未安装)。 Update your positions in that method. 用这种方法更新您的职位。
Put the initialization of the variables sX
and sY
in the constructor. 把变量的初始化sX
和sY
在构造函数中。
Remove static
. 删除static
。 By marking sX
and sY
static
, all instances of your class will always share these values, so you can never have to Skeletons with different coordinates. 通过标记sX
和sY
static
,你的类的所有实例将始终共享这些价值观,所以你可以永远不必骷髅使用不同的坐标。 If you change the value for one instance, you change it for all instances. 如果更改一个实例的值,则更改所有实例的值。
same class but use the variables in it independently 同一个类,但独立使用其中的变量
That basically means: I want to use instance variables. 这基本上意味着:我想使用实例变量。 Variables marked static
are class variables. 标记为static
的变量是类变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.