Loading...
Logo
Processing Forum
I have a strange problem.
It works on Processing, but not when exporting to the web.

At first, I thought it was something to do with some compatibility with processing.js with the noise() function on the draw() function.

I tried passing the ArrayList to the Class method -not through the Constructor- , and I had the same result.

This is passing the ArrayList to the Class,

Copy code
  1. ArrayList<Estrella> Estrellas;
  2. float EstrellasT;

  3. void setup() {
  4.   size(800, 480);
  5.   Estrellas = new ArrayList<Estrella>();
  6. }

  7. void draw() {
  8.   background(0);
  9.   
  10.   EstrellasT = random(5,15);
  11.   float noiseX = noise(1, frameCount/50.0)*width;
  12.   float noiseY = noise(2, frameCount/60.0)*height;
  13.   
  14.   
  15.   
  16.   if (frameCount % 60 == 0){
  17.     println(noiseX+" , "+noiseY);
  18.     Estrellas.add(new Estrella(noiseX, noiseY, EstrellasT, Estrellas));
  19.   }
  20.   
  21.   for (int i = Estrellas.size()-1; i >= 0; i--) { 
  22.     Estrella tal = Estrellas.get(i);
  23.     tal.mostrar();
  24.     
  25.     if (tal.murio()) {
  26.       Estrellas.remove(i);
  27.     }
  28.   }
  29.  
  30. }

  31. void mousePressed() {
  32.  Estrellas.add(new Estrella(mouseX, mouseY, EstrellasT, Estrellas));
  33. }

  34. class Estrella {

  35.   float x;
  36.   float y;
  37.   float d;
  38.   float vida = 350;
  39.   int DIST_MIN = width/4;
  40.   ArrayList<Estrella> EstrellasCol;

  41.   Estrella(float tempX, float tempY, float tempD, ArrayList<Estrella> tempEstrellasCol) {
  42.     x = tempX;
  43.     y = tempY;
  44.     d = tempD;
  45.     EstrellasCol = tempEstrellasCol;
  46.   }
  47.   
  48.   float getX() {
  49.     return x;
  50.   }
  51.   
  52.   float getY() {
  53.     return y;
  54.   }

  55.   boolean murio() {
  56.     vida--;
  57.     if (vida < 0) {
  58.       return true;
  59.     } 
  60.     else {
  61.       return false;
  62.     }
  63.   }
  64.   
  65.   void lineas() {
  66.     for(int i=0; i < EstrellasCol.size(); i++){
  67.       Estrella temp = EstrellasCol.get(i);
  68.       
  69.       if (dist(x, y, temp.getX(), temp.getY()) < DIST_MIN) {
  70.         line(x, y, temp.getX(), temp.getY());
  71.       }
  72.     }
  73.   }

  74.   void mostrar() {
  75.     fill(255, vida);
  76.     stroke(255, vida);
  77.     this.lineas();
  78.     ellipse(x, y, d, d);
  79.   }

Replies(4)

Moved from Programming Questions, since your problem seems to be with JavaScript.
Will analyze the problem later, but there is a flaw in your code, even in Java: it isn't a good idea to keep the whole collection of stars with each star.
You should design your elements independently of each other.
You can check the collision outside of the class, within a global function, or by making a class holding the collection and doing the collision check: so you have a class of elements, and a class managing these elements.
Thanks.
Yes, I know it wasn't the best idea, but I thought I was only passing the memory direction of the ArrayList to the class, not the entire arraylist, am I wrong?

I tried with different values in noise() function and now it works although in a different behaviour. noise() return closer values than in Java mode.
Yes, it is not a problem of memory or something, more about the logic of the code. Note that I did the same thing than you in the past, but I prefer now the approach I recommend.

Re: noise(), I recently saw a thread mentioning an issue with it in PJS. Perhaps search this section (this is why I moved topics...), it shouldn't be very old.

[EDIT] I see you already found it, actually!
yeah! the problem is noise() in pjs apparently.. I found another post too ( https://processing-js.lighthouseapp.com/projects/41284/tickets/1736-noise-is-not-as-random-as-it-should-be)
and mention it on the pjs IRC channel , hope someone finds the solutionXD