如何使物体以增量方式移动

How to make an object move in increments

我被困在如何编写代码来使对象以增量方式移动到所需的数量,而不是直接跳到最终目标。

例如

将对象从 1 - 500(x,y,坐标 [0,1] 到 [0,500])移动 1、2、3、4、5...... 而不是一次移动 1 到 500?

      else if(direction == 'D'){
         xInc = 0;
         yInc = 1;
         this.setXPos(this.xPos +(distance * xInc));
         this.setYPos(this.yPos +(distance * yInc));
         this.alignAll();
         this.delay(20);
      }

   }  

这是我到目前为止的完整代码,TIA 将不胜感激。

您可以在作业之间添加一个循环,例如:

  if(direction == 'R'){
  xInc = 1;
  yInc = 0;
  for(int index= 0; index <= distance;index ++){
    this.setXPos(this.xPos +(xInc));
    this.setYPos(this.yPos +(yInc));
    this.alignAll();
    this.delay(20);  
  }

}

我也强烈建议你把这个功能外化,因为它几乎是一样的,这取决于你的方向。