Java 如何使船只在出界时停止移动?

Java 如何使船只在出界时停止移动?,java,Java,我有这个代码的问题,如果有人能给我指出正确的方向,我将非常感激。已经粘了好几天了 基本上我是想阻止船在到达边界时移动。 边界是6和-6 这是代码。谢谢:) public void move(int方向)//如果位置超过5,则玩家将 //不要再朝那个方向移动了。 { 如果(位置>5) { 在这里写什么? } 否则,如果(位置5) { 位置=5; } 否则,如果(位置5) { 位置=5; 方向*=-1 } 否则,如果(位置5) { 位置=5; } 否则,如果(位置5 | |位置谢谢,这有效:)在if

我有这个代码的问题,如果有人能给我指出正确的方向,我将非常感激。已经粘了好几天了

基本上我是想阻止船在到达边界时移动。 边界是6和-6

这是代码。谢谢:)

public void move(int方向)//如果位置超过5,则玩家将
//不要再朝那个方向移动了。
{
如果(位置>5)
{
在这里写什么?
}
否则,如果(位置<-5)
{
在这里写什么?
}
位置=位置+方向;
gun1.移动(方向);
2.移动(方向);
}

问题取决于具体情况。如果你想在物体到达边界时停止它,那么像

// Move first
position = position + direction;

// Boundary check second...
if (position > 5)
{
     position = 5;
}
else if (position < -5)
{
     position = -5;
}

gun1.move(direction);
gun2.move(direction);
//先移动
位置=位置+方向;
//边界检查第二。。。
如果(位置>5)
{
位置=5;
}
否则,如果(位置<-5)
{
位置=-5;
}
gun1.移动(方向);
2.移动(方向);
如果你想“反弹”墙壁

// Move first
position = position + direction;

// Boundary check second...
if (position > 5)
{
     position = 5;
     direction *= -1
}
else if (position < -5)
{
     position = -5;
     direction *= -1
}

gun1.move(direction);
gun2.move(direction);
//先移动
位置=位置+方向;
//边界检查第二。。。
如果(位置>5)
{
位置=5;
方向*=-1
}
否则,如果(位置<-5)
{
位置=-5;
方向*=-1
}
gun1.移动(方向);
2.移动(方向);

可能会起作用-在没有任何上下文的情况下很难判断…

这将使玩家保持在边界上,即使他试图移出边界

public void move (int direction) //if position exceeds 5 then playership will 
                                 //no long move in that direction.
{
    if (position > 5)
    {
        position = 5;

    }
    else if (position < -5)
    {
        position = -5;

    }

    position = position + direction;
    gun1.move(direction);
    gun2.move(direction);

}
public void move(int方向)//如果位置超过5,则玩家将
//不要再朝那个方向移动了。
{
如果(位置>5)
{
位置=5;
}
否则,如果(位置<-5)
{
位置=-5;
}
位置=位置+方向;
gun1.移动(方向);
2.移动(方向);
}

您的方法必须将状态(已移动或未移动)传递给调用代码。 您可以通过两种方式执行此操作:

1返回状态代码

public int move(int direction)//如果位置超过5,则玩家将
//不要再朝那个方向移动了。
{

如果(位置>5 | |位置谢谢,这有效:)在if语句中,我刚刚给出了:position=5;position=-5;谢谢:d唯一的问题是,在边界检查之后,运动会被应用,这意味着对象可以在至少一个周期内移出边界…@MadProgrammer-我想这个答案是假设边界是-6到6,而你假设的是excl独家报道。OP不太清楚“达到”是<还是<≤ 关系。为什么?虽然我不想争论这个想法,但你认为OP引擎是如何工作的?基于move()API和调用代码不是紧密耦合的假设。如果它们是紧密耦合的,那么它们就不应该紧密耦合。
move()
无论如何使用,都应以标准方式操作。你不同意吗?正如我所说,我并不反对这个想法,只是它将如何帮助OP回答他们的问题;)
public void move (int direction) //if position exceeds 5 then playership will 
                                 //no long move in that direction.
{
    if (position > 5)
    {
        position = 5;

    }
    else if (position < -5)
    {
        position = -5;

    }

    position = position + direction;
    gun1.move(direction);
    gun2.move(direction);

}
public int move (int direction) //if position exceeds 5 then playership will 
                                 //no long move in that direction.
{
    if (position > 5 || position <-5)
    {
        return -1;   //status code for no movement

    }
public void move (int direction) throws InvalidArgumentException//if position exceeds 5 then playership will 
                                 //no long move in that direction.
{
    if (position > 5 || position <-5)
    {
        throw new InvalidArgumentException("Cannot move!");   //status code for no movement

    }