Why isn't my object moving anymore?

edited February 2016 in Questions about Code

I'm making some kind of moving and turning stars. I started with one object and now made an array of objects from it. Somehow, while my single object was turning, the 20 objects don't do that anymore. I have a function called "move" which adds one to the rotation. That DID work (look here: http://www.openprocessing.org/sketch/305960/)

float r = 40, hoek=0, radhoek; 
int arm=4;
float[] xpos = new float[20];
float[] ypos = new float[20];
TurningWheel[] turningwheel = new TurningWheel[20];

void setup() {
  size(800, 800);
  for (int count=0; count <20; count=count+1) {
    turningwheel[count] = new TurningWheel(random(60, 800), random(60, 800));
  }
}

void draw() {
  background(210);
  for (int count=0; count <20; count=count+1) {
    turningwheel[count].display();
    turningwheel[count].move();
    turningwheel[count].numberArms();
  }
}

class TurningWheel {
  float xpos, ypos;

  TurningWheel (float tempxpos, float tempypos) {
    radhoek=radians(hoek);
    xpos = tempxpos;
    ypos = tempypos;
  }

  void display() {
    for (int i = 0; i < arm; i++) {
      float[] xarmen = new float[arm];
      float[] yarmen = new float[arm];  
      xarmen[i] = xpos + r * cos(radhoek+radians(i*360/arm));
      yarmen[i] = ypos + r * sin(radhoek+radians(i*360/arm));
      line(xpos, ypos, xarmen[i], yarmen[i]);
      ellipse(xarmen[i], yarmen[i], 6, 6);
    }
    ellipse(xpos, ypos, 20, 20);
  }

  void move() {
    if (hoek>360) {
      hoek=1;
    }
    hoek+=1;
  }

  void numberArms() {  
    if (keyPressed) {
      if (key == 'a') {
        arm=arm+1;
      }
    } else if (key == 'q') {
      arm=arm-1;
    }
    if (arm < 1) {
      arm = 1;
    }
    if (arm > 18) {
      arm = 18;
    }
  }
}

Another problem is, you can choose the number of arms of one "star", by pressing a and q. With the single star, it worked, though in stead of adding (or subtracting) one arm for each time you pressed a or q, it added (subtracted) more. Now, it just goes from the minimum (1) to the maximum (18). Why isn't it going stepwise?

Tagged:

Answers

  • Answer ✓

    keypressed is checked every draw loop, about 60 times a second by default. if you're pressing the key from more than 1/3rd of a second then it'll max out.

    keypressed() the method runs when a key is pressed. try using that instead.

    (the duplicate name for the method and the variable is a design mistake imo, this is the third answer like this i've written in a week)

  • Answer ✓

    it doesn't help that you're calling numberArms in a loop within draw...

  • edited February 2016 Answer ✓

    delete numberArms() (and the call) and add this to the end of the code (same level as setup() and draw())

    void keyPressed() {
      if (key == 'a') {
        arm++;
      } else if (key == 'q') {
        arm--;
      }
      arm = constrain(arm, 1, 18);
    }
    

    (edit, forgot to assign constraint value back to arm)

  • Answer ✓

    move line 27 to the start of display() to get the stars rotating again. the constructor only runs once...

  • edited February 2016 Answer ✓
    int arm = 4;
    
    void draw() {
    }
    
    void keyPressed() {
      final int k = keyCode;
      arm = constrain(arm + (k == 'A'? 1 : k == 'Q'? -1 : 0), 1, 18);
      print(arm, TAB);
    }
    
  • edited February 2016

    Great! Indeed, I didn't notice the difference between keyPressed and keyPressed()!

    About putting the numberArms as a function: I was in the impression that making classes and functions of almost everything, was the way to work.

    I didn't know the use of keyCode or final yet. I tested GoToLoops example as well and I see it works, but I don't understand it very well... (the way you write the condition (k == 'A'? 1 : k == 'Q'? -1 : 0) is very new to me.

    Thank you koogs and GoToLoop!

  • edited February 2016

    So that's fixed, now I'm going to make them move and add a collision-detection. That's for tomorrow, it's 2 o-clock in the night overhere...

    http://www.openprocessing.org/sketch/305960

  • Ah :) The second and third link I already checked, the first was what I needed to completely understand your code...

  • P.S.: More than 80% of keyCode's reference link is wrong, superstitious or both! So be warned! :-\"

  • edited February 2016

    I removed this question:
    1) it didn't match the original questions description anymore.
    2) I already checked the problem as "solved".

    Like elsewhere in the forum is said: sometimes I'd like to say "thank you" even when the problem wasn't (competely) solved. For this reason it's tempting to click on "Yes"... (in this case, my problem WAS solved AND thank you also ;-))

This discussion has been closed.