When new instance of class is created, the last one dissappears?

edited December 2016 in Programming Questions

Hey everyone! I just started messing with processing and really enjoying it so far. I'm trying to create a program that sends points(orbs) flying away from the mouse, then creates lines from a point grid to those orbs. The problem i'm facing is when i try to create the orbs flying away from the mouse, the orbs i sent earlier just dissappear. Could someone check my code because i think its just a rookie mistake i made but i can't seem to find it.

Also, i don't know how to properly post code on here as its my first post. if someone could explain it to me I'll edit my post!

float[] PointsX = {200, 300, 400, 500, 600, 700, 800, 900, 1000};
float[] PointsY = {100, 200, 300, 400, 500, 600, 700, 800, 900};
float visibility = 10;
float x, y, dx, dy, x2, y2;
float r;
float dis;
float O;

int PointAmount=PointsX.length;

PVector location, mouse;

void setup() {
  size(displayWidth, displayHeight);
  background(-1);
  smooth();
  location = new PVector(width/2, height/2);
}

void draw() {
  frame.setLocation(displayWidth/2-width/2, displayHeight/2-height/2);
  fill(255);
  rect(0, 0, displayWidth, displayHeight);
  translate(0, 0);
  //drawGrid();
  // checkMouseDistance();
  shoot();
  stroke(0, 0, 0);
  strokeWeight(10);
  text(O, mouseX, mouseY);
}

void shoot() {
  if (frameCount%200==0)
  {
    Orb o = new Orb(10);
    O++;
    orbs.add(o);
  }
  for (Orb o : orbs) {
    o.display(); 
    o.update();
  }
}
void drawGrid() {
  fill(0);
  stroke(15);
  //strokeWeight(visibility);
  for (int i=0; i< PointAmount; i++)
  {
    for (int ib=0; ib< PointAmount; ib++)
    {
      point(PointsX[i], PointsY[ib]);
    }
  }
}



ArrayList <Orb> orbs= new ArrayList <Orb>();

float bsize = 1024;
class Orb {
  float Speed = 1;
  Orb(float r) {
    dis = r;
  }


  void update() {
    dis +=Speed;
  }

  void display() {
    //ellipse(mouseX, mouseY, dis, dis);
    for (int i = 0; i < bsize - 1; i+=60)
    {
      colorMode(RGB);//sets color mode back to Red green and blue 
      //fill(EQColorR,EQColorG,EQColorB);
      float x = (dis)*cos(i*2*PI/bsize);
      float y = (dis)*sin(i*2*PI/bsize);
      x2=mouseX; 
      y2=mouseY;
      noFill();
      strokeWeight(0);
      ellipse(x+x2, y+y2, 10, 10);
      for (int ib=0; ib< PointAmount; ib++)
      {
        for (int ic=0; ic< PointAmount; ic++)
        {
          if (dist(x+x2, y+y2, PointsX[ib], PointsY[ic]) <= 200) 
          {
            if (dist(mouseX, mouseY, PointsX[ib], PointsY[ic])<= 100) {
              visibility=0;
            }
            else
            {
              visibility =200/dist(mouseX, mouseY, PointsX[ib], PointsY[ic]);
            }
            stroke(15);
            strokeWeight(visibility);
            line(x+x2, y+y2, PointsX[ib], PointsY[ic]);
          }
        }
      }
    }
  }
}

Any advice is greatly appreciated!

Answers

  • You can edit your old post

    Then leave an empty line before and after the code section

    Select the code section

    Hit ctrl-o or use the C in the command bar

  • Answer ✓

    Make dis part of your class

    At the moment it seems to be on sketch level, but it must be on class level, so each orb can have a different dis

  • Damn thanks so much! That was fast. Now that you mention it it seem logical ;P

  • No problem

  • some remarks

    you can do some house cleaning

    you could:

    First

    to do the displaying in shoot() confuses the name shoot. better make a new function display...() or re-name it shootAndDisplay or so

    remember the name of a function should reflect what it does

    Second

    Then place all global vars, arrays etc. in one spot - line 60 and 62 belongs before setup()

    Third

    not sure if line 21 shouldn't go into setup() ?

Sign In or Register to comment.