Java 类中的平台在调用时不移动

Java 类中的平台在调用时不移动,java,class,variables,processing,Java,Class,Variables,Processing,为了上大学,我正在和一个朋友做一个游戏。一般的想法是,我们有一些平台从右向左移动,每次离开屏幕时,它都会在右侧的一个随机x和y位置生成(在某些限制范围内)。会有一个小精灵从一个平台跳到另一个平台 我们遇到了一个不确定如何解决的问题。我们有所有正确的代码和一切,但平台就是不会移动。它们应该以每帧-4像素的恒定速度向左移动(rectVelocity) 然而,我们无法让他们移动;它们在屏幕上的初始调用位置是静态的 这是我能做的最精简的代码: Platforms [] mainPlats; void

为了上大学,我正在和一个朋友做一个游戏。一般的想法是,我们有一些平台从右向左移动,每次离开屏幕时,它都会在右侧的一个随机
x
y
位置生成(在某些限制范围内)。会有一个小精灵从一个平台跳到另一个平台

我们遇到了一个不确定如何解决的问题。我们有所有正确的代码和一切,但平台就是不会移动。它们应该以每帧
-4
像素的恒定速度向左移动(
rectVelocity

然而,我们无法让他们移动;它们在屏幕上的初始调用位置是静态的

这是我能做的最精简的代码:

Platforms [] mainPlats;

void setup() {
  size(750, 400);

  mainPlats = new Platforms[3];
}

void draw() {
  level();
}

void level() {

  //This is the code for the first platform
  mainPlats[0] = new Platforms(200, 200, 100, 15); //These values need to be     set inside the class so that
  //they aren't constantly overwriting the movement variables in the class
  mainPlats[0].displayPlat();
  mainPlats[0].platTransition();


  //This is the code for the second platform
  mainPlats[1] = new Platforms(420, 300, 100, 15);
  mainPlats[1].displayPlat();
  mainPlats[1].platTransition();


  //This is the code for the third platform
  mainPlats[2] = new Platforms(570, 350, 100, 15);
  mainPlats[2].displayPlat();
  mainPlats[2].platTransition();
}

class Platforms {
  PImage platform;
  int rectX, rectY, rectWidth, rectHeight;
  int rectVelocity = 4;

  Platforms(int x, int y, int w, int h) {
    rectX = x; 
    rectY = y;
    // rectX = (int(random(600, 800))); //Tried to randomise start position,     failed hilariously
    //rectY = (int(random(150, 350)));
    rectWidth = w;
    rectHeight = h;
  }

  void displayPlat() {
    platform = loadImage ("images/tiles.png");
    //imageMode(CENTER);

    image(platform, rectX, rectY, 100, 15); //rectangle platforms replaced     with images
  }

  void platMove() {
    rectX -= rectVelocity;
  }

  void platTransition() {
    if (rectX < -200) {
      rectX = (int(random(700, 1000)));
      rectY = (int(random(150, 350)));
    }
  }
 }
平台[]主平台;
无效设置(){
规模(750400);
主平台=新平台[3];
}
作废提款(){
水平();
}
空层(){
//这是第一个平台的代码
mainPlats[0]=新平台(20020010015);//这些值需要在类内部设置,以便
//它们不会一直覆盖类中的移动变量
mainPlats[0]。displayPlat();
主平台[0]。平台转换();
//这是第二个平台的代码
主平台[1]=新平台(420、300、100、15);
mainPlats[1]。displayPlat();
mainPlats[1]。platTransition();
//这是第三个平台的代码
主平台[2]=新平台(570、350、100、15);
mainPlats[2]。displayPlat();
mainPlats[2]。platTransition();
}
班级讲台{
PImage平台;
int rectX、rectY、rectWidth、rectHeight;
int-rectVelocity=4;
平台(整数x,整数y,整数w,整数h){
rectX=x;
rectY=y;
//rectX=(int(random(600800));//尝试将起始位置随机化,失败得很滑稽
//rectY=(int(随机(150350));
矩形宽度=w;
高度=h;
}
void displat(){
平台=加载图像(“images/tiles.png”);
//图像模式(中心);
图像(平台,矩形,矩形,100,15);//矩形平台替换为图像
}
void platMove(){
rectX-=rectVelocity;
}
void plattransformation(){
if(rectX<-200){
rectX=(int(随机(7001000));
rectY=(int(随机(150350));
}
}
}

在构建平台时,您将rectX作为正值(>0),但在调用
platTransition
时,您正在检查
rectX<-200
,这就是为什么它从不做任何事情的原因。

从draw()函数调用level()函数,它将在每个帧初始化平台阵列

这意味着您可以在每个帧的起始位置创建新的平台。你永远不会看到平台移动,因为一旦你移动了它们,你就会在起始位置用新的平台替换它们

因此,第一步是将它们的初始化从level()函数中移出,只调用它们一次,在草图的开头-setup()函数是一个可以放置它们的地方

另一个问题是,您从未真正调用platMove()函数。第二步是确保调用该函数

解决方案可能如下所示:

Platforms [] mainPlats;

void setup() {
  size(750, 400);

  mainPlats = new Platforms[3];
  mainPlats[0] = new Platforms(200, 200, 100, 15);
  mainPlats[1] = new Platforms(420, 300, 100, 15);
  mainPlats[2] = new Platforms(570, 350, 100, 15);
}

void draw() {
  level();
}


void level() {
  mainPlats[0].displayPlat();
  mainPlats[0].platMove();
  mainPlats[0].platTransition();

  mainPlats[1].displayPlat();
  mainPlats[1].platMove();
  mainPlats[1].platTransition();

  mainPlats[2].displayPlat();
  mainPlats[2].platMove();
  mainPlats[2].platTransition();
}

还要注意的是,您也不应该每一帧都加载图像。您应该在启动时只加载一次。您还可能希望使用for循环在您的平台上迭代,而不是引用每个索引。但是这些并不会真正影响你的问题。

@jornsharpe语言正在处理,因此标签。蒂莫西:你能把你的素描作为一个独立的类而不是这个分离的类吗?@jonrsharpe别担心。对于语言名称来说,处理是一个非常糟糕的选择!我已经把它作为MCVE发布了,希望能有所帮助!事实并非如此。函数的作用是:减小rectX值,因此它们最终应该为负值。问题是他在每一帧都初始化值,而且他从来没有真正调用platMove()函数。@KevinWorkman,我已经偏离了给定的代码,我认为这是不完整的。是的,事实上,从来没有调用platMove,但platTransition()是在构造之后,所以我假设OP希望platTransition在构造之后立即执行某些操作,但它不能。