Loading...
Logo
Processing Forum
Hi,
This code is working well in java mode but when I tried uploading it on openprocessing.org it doesn't show any thing other than background color so I tried tested it in javascript mode and but still it is not working. 

help! I am missing anything ?


Copy code
  1. int rectsize=50, up, down, right, left;
  2. Rectpush[] rect;
  3. void setup()
  4. {
  5.   size(600, 300);
  6.   rect = new Rectpush[8];
  7.   for (int i=0;i<rect.length;i++) {
  8.     float x = random(0, width-50);
  9.     float y = random(0, height-50);
  10.     rect[i] = new Rectpush(x, y, rectsize, i );
  11.   }
  12. }
  13.  
  14. void draw()
  15. {
  16.   background(#00CAFF);
  17.     noStroke();
  18.   for (int i=0;i<rect.length;i++) {
  19.     if ( rect[i].x< mouseX && mouseX < rect[i].x+rectsize && rect[i].y< mouseY && mouseY < rect[i].y+rectsize) {
  20.       fill(#FFC800);
  21.       if (right==1) rect[i].x++;
  22.       if (left==1) rect[i].x--;
  23.       if (down==1) rect[i].y++;
  24.       if (up==1) rect[i].y--;
  25.     }
  26.     else {
  27.       fill(255);
  28.     }
  29.     rect[i].update();
  30.   }
  31. }
  32. void keyPressed() {
  33.   if (key == CODED) {
  34.     if (keyCode == UP) up=1;
  35.     if (keyCode == DOWN) down=1;
  36.     if (keyCode == LEFT) left=1;
  37.     if (keyCode == RIGHT)right=1;
  38.   }
  39. }
  40.  
  41. void keyReleased()
  42. {
  43.   up=0;
  44.   down=0;
  45.   right=0;
  46.   left=0;
  47. }
  48.  
class 

Copy code

  1.  
  2. class Rectpush {
  3.   float x, y;
  4.   int rectsize;
  5.   private int i;
  6.   Rectpush(float _x, float _y, int _rectsize, int _i)
  7.   {
  8.     x = _x;
  9.     y = _y;
  10.     i = _i;
  11.     rectsize = _rectsize;
  12.   }
  13.   void update()
  14.   {
  15.     rect(x, y, rectsize, rectsize);
  16.     fill(#FF0077);
  17.     textAlign(CENTER);
  18.     text( i ,x+25,y+30);
  19.   }
  20. }

Replies(3)

You cannot use the same name as a function. rect is a Processing function so you can't use this for your array. In the code below I have changes the name of the array to rects and it works fine in JS mode.



Copy code
  1. int rectsize=50, up, down, right, left;
  2. Rectpush[] rects;

  3. void setup()
  4. {
  5.   size(600, 300);
  6.   rects = new Rectpush[8];
  7.   for (int i=0;i<rects.length;i++) {
  8.     float x = random(0, width-50);
  9.     float y = random(0, height-50);
  10.     rects[i] = new Rectpush(x, y, rectsize, i );
  11.   }
  12. }

  13. void draw()
  14. {
  15.   background(#00CAFF);
  16.   noStroke();
  17.   for (int i=0;i<rects.length;i++) {
  18.     if ( rects[i].x< mouseX && mouseX < rects[i].x+rectsize && rects[i].y< mouseY && mouseY < rects[i].y+rectsize) {
  19.       fill(#FFC800);
  20.       if (right==1) rects[i].x++;
  21.       if (left==1) rects[i].x--;
  22.       if (down==1) rects[i].y++;
  23.       if (up==1) rects[i].y--;
  24.     }
  25.     else {
  26.       fill(255);
  27.     }
  28.     rects[i].update();
  29.   }
  30. }

  31. void keyPressed() {
  32.   if (key == CODED) {
  33.     if (keyCode == UP) up=1;
  34.     if (keyCode == DOWN) down=1;
  35.     if (keyCode == LEFT) left=1;
  36.     if (keyCode == RIGHT)right=1;
  37.   }
  38. }

  39. void keyReleased()
  40. {
  41.   up=0;
  42.   down=0;
  43.   right=0;
  44.   left=0;
  45. }

  46. class Rectpush {
  47.   float x, y;
  48.   int rectsize;
  49.   private int i;

  50.   Rectpush(float _x, float _y, int _rectsize, int _i)
  51.   {
  52.     x = _x;
  53.     y = _y;
  54.     i = _i;
  55.     rectsize = _rectsize;
  56.   }

  57.   void update()
  58.   {
  59.     rect(x, y, rectsize, rectsize);
  60.     fill(#FF0077);
  61.     textAlign(CENTER);
  62.     text( i, x+25, y+30);
  63.   }
  64. }

This 1 is an eternal classic: don't use same labels for both variables & methods if JS compatibility is desired!

You've declared a variable labeled rect . But rect is also the label for rect() method!
So, you've overshadowed Processing's own rect()
Copy code
    // http://forum.processing.org/topic/not-running-in-browser-or-javascript-mode-why
    
    final static int NUM = 8, DIM = 50;
    final RectPush[] rects = new RectPush[NUM];
    
    boolean up, down, right, left;
    
    void setup() {
      size(600, 300);
      textAlign(CENTER);
      rectMode(CORNER);
    
      for (int i=0; i!=NUM; i++) {
        final int x = (int) random(width  - DIM);
        final int y = (int) random(height - DIM);
        rects[i] = new RectPush(x, y, DIM, i);
      }
    }
    
    void draw() {
      background(#00CAFF);
      noStroke();
    
      for (int i=0; i!=NUM; i++) {
        final RectPush r = rects[i];
    
        if (r.x < mouseX & mouseX < r.x + DIM & r.y < mouseY & mouseY < r.y + DIM) {
          fill(#FFC800);
    
          if (right)   ++rects[i].x;
          if (left)    --rects[i].x;
          if (down)    ++rects[i].y;
          if (up)      --rects[i].y;
        }
    
        else   fill(-1);
    
        rects[i].update();
      }
    }
    
    void keyPressed() {
      if (keyCode == UP)     up    = true;
      if (keyCode == DOWN)   down  = true;
      if (keyCode == LEFT)   left  = true;
      if (keyCode == RIGHT)  right = true;
    }
    
    void keyReleased() {
      up = down = right = left = false;
    }
    
    class RectPush {
      int x, y;
      int dim;
      int i;
    
      RectPush(int _x, int _y, int _dim, int _i) {
        x = _x;
        y = _y;
        i = _i;
        dim = _dim;
      }
    
      void update() {
        rect(x, y, dim, dim);
        fill(#FF0077);
        text(i, x+25, y+30);
      }
    }
    
         

Stupid me .......... thanks a lot ! :)