如何在不使用固定数字的情况下返回2D形状对象的x位置



我有一个2D火箭,火箭是由3个物体组成的集合,一个鼻子(三角形(、一个身体(正方形(和一个喷气式飞机(圆形(,它们共同构成了火箭。

我必须设置身体(正方形(相对于鼻子(三角形(,使它们相互对齐。

到目前为止,我有这个;

public Rocket(Triangle t, Square s, Circle c) {
this.nose = t;
this.body = s; 
this.jet = c;    
this.nose.setXPos(50);//initial positions of the nose X
this.nose.setYPos(300);//initial positions of the nose Y
this.body.setXPos(getBodyXPos());//sets the body relative to the nose X
this.body.setYPos(getBodyYPos());//sets the body relative to the nose Y

我也有方法;

private int getBodyXPos()
{   
return this.xPos;
}
private int getBodyYPos()
{
return this.yPos;
}
private int getJetXPos()
{
return this.xPos;
}
private int getJetYPos()
{
return this.yPos;
}

我不是大声使用固定数字,而是需要以某种方式使用辅助方法,使形状出现在屏幕上的正确位置。我已经被困在这个问题上好几天了,希望能得到一些帮助。非常感谢。

您只需使用适当的偏移设置各个部件相对于彼此的位置

int rocketx = 50;
int rockety = 300;
this.nose.setXPos(rocketx);
this.nose.setYPos(rockeyy);
this.body.setXPos(rocketx);
this.body.setYPos(rockety+this.nose.height()); 
this.jet.setXPos(rocketx);
this.jet.setYPos(rockety+this.nose.height()+this.body.height()); 

你可以在鼻子和身体上找到他们的身高,而不是固定的身高值,然后用它来代替。如果你的火箭是水平飞行的,那么你需要对X做同样的操作,而不是对Y。

最新更新