Change the list with the "up" and "down" keys in processing.

I am a french student in graphic design. For my graduation diploma, I use processing, and currently I have a problem with my code.

In fact, I do not know how to change my lists by increasing and decreasing with the keys "up" and "down".

Here is my code:

PImage[] images0 = new PImage[6] ;

PImage[] images1 = new PImage[6] ;

PImage[] images2 = new PImage[6] ;

PImage[] images3 = new PImage[6] ;

PImage[] images4 = new PImage[6] ;

PImage[] images5 = new PImage[6] ;

void setup() {

size(600,600);

frameRate(4);

for(int i=0; i<6; i++) for(int images=0; images<6; images++) {

images0[i] = loadImage("module_" + i + ".png");

images1[i] = loadImage("module1_" + i + ".png");

images2[i] = loadImage("module2_" + i + ".png");

images3[i] = loadImage("module3_" + i + ".png");

images4[i] = loadImage("module4_" + i + ".png");

images5[i] = loadImage("module5_" + i + ".png");}

}

void draw() {

background(255);

smooth();

noStroke();

noFill();

image(images1[(int)random(6)], 0, 0);

if (keyCode == UP){

background(255);

image(images3[(int)random(6)], 0, 0);}

}

For now I am interested in the action of changing the list increasingly. I guess the reverse code itself, more or less.

My goal is that when the "up" key is pressed once, the list1 goes to the list2, and when pressing the "up" key again, list 2 goes to list3, etc.

I thank you in advance and apologize if my English is bad.

I look forward to your advice.

Tagged:

Answers

  • Answer ✓

    Some hints:
    Put the keyCode statements inside keyPressed() routine of Processing.
    Use a variable to remember the current list (i.e. the list to move).
    When press UP use a temporary list to exchange the two lists.
    What want you do when the current list is the last list ?

  • edited March 2017

    When it's the last list, it's the last list that's played. And if the user wants to go back to a previous list, he must press the "down" key.

    Sorry I'm still a little novice in processing. But what do you say:

    Use a variable to remember the current list (i.e. the list to move).

    I need to create a variable like this:

    int image0[i] = a;

    int image1[i] = b ;

    int image2[i] = c ;// for exemple?

    ...

    And place it in the void. draw() ?

  • Answer ✓

    This is untested code but it should work. I made few changes but nothing critical.

    Kf

    PImage[] currList;
    int currIdx=0;
    int n=6;  //Max number of lists
    
    PImage[] images0 = new PImage[6] ;
    PImage[] images1 = new PImage[6] ;
    PImage[] images2 = new PImage[6] ;
    PImage[] images3 = new PImage[6] ;
    PImage[] images4 = new PImage[6] ;
    PImage[] images5 = new PImage[6] ;
    
    void setup() {
    
      size(600, 600);
      smooth();
      noStroke();
      noFill();
    
      frameRate(4);
    
      for (int i=0; i<6; i++) for (int images=0; images<6; images++) {
        images0[i] = loadImage("module_" + i + ".png");
        images1[i] = loadImage("module1_" + i + ".png");
        images2[i] = loadImage("module2_" + i + ".png");
        images3[i] = loadImage("module3_" + i + ".png");
        images4[i] = loadImage("module4_" + i + ".png");
        images5[i] = loadImage("module5_" + i + ".png");
      }
    }
    
    void draw() {
      background(255);
      image(currList[(int)random(currList.length)], 0, 0);
    }
    
    void keyReleased() {
    
      boolean validKey=false;
    
      if (keyCode == UP) {
        validKey=true;
        currIdx++;
      }
    
      if (keyCode == DOWN) {
        validKey=true;
        currIdx--;
      }
    
      if (validKey==true) {
        currIdx=currIdx%n; //Ensures it stays between 0 and 5 when n=6 for example
        switch(currIdx) {
        case 0: 
          currList=images0;
          break;
        case 1: 
          currList=images1;
          break;
        case 2: 
          currList=images2;
          break;
        case 3: 
          currList=images3;
          break;
        case 4: 
          currList=images4;
          break;
        case 5: 
          currList=images5;
          break;
        }
      }
    }
    
  • thank kfrajer

    But when I play the program, processing displays:

    nullpointerexception

    For the line:

    image(currList[(int)random(currList.length)], 0, 0);

  • You can use a 2D array (untested), but remember to rename your "module_n.png" files to "module0_n.png" -

    int currIdx = 0;
    int n = 6;  //Number of lists
    int images_n = 6; //images per list 
    
    PImage[][] images = new PImage[n][images_n];
    
    void setup() {
    
       size(600, 600);
       smooth();
       noStroke();
       noFill();
    
       frameRate(4);
    
       for(int i = 0; i < images_no; i++){
          for(int b = 0; b < n; b++){
             images[b][i] = loadImage("module" + b + "_" + n + ".png");
          }
       }
    }
    
    
    void draw() {
       background(255);
       image(images[currIdx%n][(int)random(images_no)], 0, 0);
    }
    
    void keyReleased(){
       if (keyCode == UP) {
          currIdx++;
       }
    
       if (keyCode == DOWN) {
          currIdx--;
       }
    }
    
  • edited March 2017

    thank you so much Lord_of_the_Galaxy

    It works. But my lists do not come alive ... I don't understand why?

    Animation stays frozen on the last image of each list.

  • But my lists do not come alive ... I don't understand why?

    Please explain.

  • edited March 2017

    When I play the program, the first list appears and the same for others. But normally each list composed of images should activate as a gif.

    For example:

    I launch the program.

    The first list appears as a gif.

    I press the "up" key, the second list appears as a gif.

    Etc.

    But it does:

    I launch the program.

    The first list appears on my last picture of my list.

    Etc.

    Etc.

    I do not know if I'm clear :/

  • Oh, so that's what you want? I got a bit confused because of @kfrajer's approach.

  • edited March 2017 Answer ✓

    Try this -

    int currIdx = 0;
    int n = 6;  //Number of lists
    int images_no = 6; //images per list 
    int imgIdx = 0;
    
    PImage[][] images = new PImage[n][images_no];
    
    void setup() {
    
       size(600, 600);
       smooth();
       noStroke();
       noFill();
    
       frameRate(4);
    
       for(int i = 0; i < images_no; i++){
          for(int b = 0; b < n; b++){
             images[b][i] = loadImage("module" + b + "_" + i + ".png");
          }
       }
    }
    
    
    void draw() {
       background(255);
       image(images[currIdx%n][imgIdx%images_no]0, 0);
       imgIdx++;
    }
    
    void keyReleased(){
       if (keyCode == UP) {
          currIdx++;
          imgIdx = 0;
       }
    
       if (keyCode == DOWN) {
          currIdx--;
          imgIdx = 0;
       }
    }
    
  • I found!

    It was just a value that was not in its right place.

    images[b][i] = loadImage("module" + b + "_" + i + ".png");

    instead of :

    images[b][i] = loadImage("module" + b + "_" + n + ".png");

    Thank you so much to all of you! You helped me a lot and I thank you again. :)

  • edited March 2017 Answer ✓

    (Oops :D ) Still, check out my new program. I think that's more like what you may want.

  • edited March 2017

    it works ! ;) thanks again :)

Sign In or Register to comment.