Toggle items in StringList-newb needs a hint...

OK so i have spent the beginning of my day messing around with this with only partial success. Kind of embarrassing, seemed like it would be relatively easy. anyway i am just trying to modify an example from the reference section so that i can toggle through the items in a stringlist by pressing a mouse. ultimately i want to midi control sequential video clips but baby steps for now...(and i am guessing i might need some kind of buffer for video clips if i don`t want a delay every time i call a new clip but anyway..)

here is my code so far:

int x = 0;
void setup() {
  size(200, 200);
  inventory = new StringList();
  inventory.append("coffee");
  inventory.append("flour");
  inventory.append("tea");
  println(inventory);
  noLoop();
  fill(0);
  textAlign(CENTER);
}

void draw() {
  String item = inventory.get(x);

  for (x = 0; x < 3; ) { 
    if (mousePressed == true) { 
      x = x+1;
      text(item, width/2, height/2);
    }
  }
}

/*void mousePressed () {
 if (x < 3) { x = x + 1;
 } else { x = 0;
 }
 }*/

have managed to crash things by commenting noLoop haha! so can someone please school me??

Answers

  • Take a step back and read what the code is doing. In your draw() function, you get a String item based on x, which is 0. So item is "coffee". Then you loop 3 times. If the mouse is pressed, you increment x and draw the item. So you draw "coffee" three times for some reason, and at the end x equals 3. Then in the next call to draw(), you try to get the item at index 3 (which is the 4th index), and I would bet your program throws an error.

    I assume you just want to increment x whenever you press the mouse. Why did you comment out your mousePressed() function? And what are you trying to do with the for loop that I described above?

  • edited January 2014 Answer ✓

    Some example of mine: :D

    // forum.processing.org/two/discussion/2374/
    // toggle-items-in-stringlist-newb-needs-a-hint-
    
    final StringList foods = new StringList(new String[] {
      "coffee", "flour", "tea"
    }
    );
    
    int idx;
    
    void setup() {
      size(250, 150);
      noLoop();
      frameRate(10);
      smooth(4);
      fill(#2040F0);
    
      textAlign(CENTER, CENTER);
      textSize(0100);
    
      println(foods);
    }
    
    void draw() {
      background(0300);
      text(foods.get(idx), width>>1, height>>1);
      idx = (idx + 1) % foods.size();
    }
    
    void mousePressed() {
      redraw();
    }
    
    void keyPressed() {
      redraw();
    }
    
  • Hi and thanks! Yeah i have only been trying to speak this language for a little over a week so there is usually a big difference between what i want to say and what actually comes out of my mouth. Thankfully it is difficult to accidentally insult anybody with this language :P .

    @ kevinworkman, What i thought i was trying to say in the for loop was " starting at zero, and going no higher than 2, increment x" . but i see from your translation that's not what i said haha. I commented the mousepressed function because i didn't want to have to retype it if i decided to go back and mess with it some more, and also evidence to those whore kind enough to help, that i am at least making an effort and not running to the forum every time i get stuck!

    @ gotoloop, yes this is exactly what i was trying to do! just saw modulo in a video a couple of days ago but it didn't occur to me. also had no idea there was such a thing as redraw().

    Ok thanks!

  • edited January 2014 Answer ✓

    Just saw modulo in a video a couple of days ago...

    That modulo trick is kinda semi-advanced! :-\" You can replace that w/ this simpler form:

    if (++idx == foods.size())  idx = 0;
    

    Also had no idea there was such a thing as redraw().

    It's used when automatic draw() is turned off w/ noLoop(). That way, a program becomes input-trigger oriented! \m/

  • Thanks again!

Sign In or Register to comment.