Program works in Java but not JavaScript. Can't figure out why!

edited October 2013 in JavaScript Mode

This is just a sample of code from a larger program, but even this will not run in JavaScript! I have compared it to other programs on OpenProcessing.org, but haven't had any luck finding important differences. I would appreciate it if you could take a quick look and suggest something to try.

float[] backdrop = new float[160]; 
int PR = 255; 
int PG = 105; 
int PB = 180; 

void setup () {
  size(800, 600);
  backdropgenerate();
  backdroprender();
}

void draw() {
  backdroprender(); 
  drawplayer();
}

void backdropgenerate () {
  for (int i=0;i<160;i=i+2) {
    backdrop[i] = random(0, 800);
    backdrop[i+1] = random(0, 600);
  }
}

void backdroprender () {
  noStroke(); 
  smooth(); 
  fill(225); 
  rect(0, 0, 800, 600);
  int fill = 200; 
  int size = 120; 
  for (int i=0;i<160;i=i+2) {
    if (i>40&&i<80) {
      size = 80;
      fill = 125;
    }
    if (i>80&&i<120) {
      size = 50 ;
      fill = 75 ;
    }
    if (i>120) {
      size = 10;
      fill = 25 ;
    }
    fill(fill);
    if (level == 7) {
        fill(colors[i*4], colors[i*4+1], colors[i*4+2], colors[i*4+3]);
    } 
    ellipse(backdrop[i], backdrop[i+1], size, size);
  }
  fill(0);
  rect(0, 500, 800, 600);
}

void drawplayer() {
  fill(PR, PG, PB);
  ellipse(xpos, ypos-20, 40, 40);
  ellipse(xpos, ypos+20, 45, 45);
  rect(xpos-22, ypos+20, 45, 30);
}

Answers

  • This list is incomplete. Not even in Java it's working! X(

  • General, but important advice in JavaScript mode: don't use variable names like fill and size! In Java, it is OK, but in JavaScript, if you overwrite the fill variable, you will not be able to use the fill() function!

Sign In or Register to comment.