help array

edited November 2015 in Questions about Code

help

Tagged:

Answers

  • edited November 2015

    you mean show the images when hitting key d ?

    ok, first, you don't need to load all images again - you loaded them in setup() and that is what setup() is for.

    second, you need to say frame=0; before the 2 for loops and then say frame++; after println() to go on to the next image

  • it says it out of bounds String [] suit = {"Clubs", "Diamonds", "Hearts", "Spades"}; String [] rank ={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}; String [] deckofCards=new String [rank.length*suit.length]; int a; int b; int temp; int numFrames=52; PImage [] images= new PImage[numFrames]; int frame=0; float x=0;

    void setup(){ size(600,400); background(0);

    frameRate(2);

    for(int i=0;i<images.length;i++){ String imageName = "cards-"+i+".png"; images[i]=loadImage(imageName);

    }

    }

    void draw(){

    frame=frameCount%numFrames;}

    void mousePressed(){ int a=(int)(random(rank.length)); int b=(int)(random(suit.length)); println(rank[a]+" "+"of"+" "+suit[b]);

    }

    void keyPressed(){ if (key=='d'){ frame=0;

    for(int i=0; i < rank.length; i++){
    for(int j=0; j < suit.length; j++){ 
      image(images[frame],width/4,0,300,400);
      deckofCards[rank.length*i+j]=rank[i]+"of"+suit[j]; 
    println(rank[i]+" "+"of"+" "+suit[j]); frame++;
    

    }}}

    else if(key== 's'){ int t=deckofCards.length; for( int c=0; c<t;c++){ int l=c+ (int)(random(t-c));

     String temp= deckofCards[l];
     deckofCards [l]= deckofCards [c];
    
     deckofCards [c]=temp;
    

    println(deckofCards[c]);}}}

  • edited November 2015

    is rank.length*suit.length == 51 ?

    because you have the array this size

    learn how to format code

    go back, edit your post and format the code correctly

  • no its 52 i fixed it, but the images still won't play, what could be wrong?

  • go back, edit your sketch and format the code correctly

  • obviously when displaying images all on top of each other (same x and y position), you'll see only the last image (the screen is only updated once at the end of draw() (each time the sketch gets there) and not throughout)

  • edited November 2015

    to solve this set/ say frameRate(12);

    and then make a state "display"

    start this state and in draw() don't use for loop but just

    image(images[frame],width/4,0,300,400);
    deckofCards[rank.length*i+j]=rank[i]+"of"+suit[j]; 
    println(rank[i]+" "+"of"+" "+suit[j]); frame++;
    
    j++;
    if (j>=suit.length) {
        j=0;
        i++;
    }
    
  • Please format your code for this forum. You have been asked 3 times in this discussion to format your code and you have completely ignored them.

    Making your code legible makes it easier for members help you!

    If you don't know how to do it then read this discussion and then go back and edit your comments above.

  • //Declare Variables
    <String [] suit = {"Clubs", "Diamonds", "Hearts", "Spades"};
    String [] rank ={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
    String [] deckofCards=new String [rank.length*suit.length];
    int a;
    int b;
    int temp;
    int numFrames=52;
    PImage [] images= new PImage[numFrames];
    int frame=0;
    float x=0;>
    
    
    <void setup(){
    //Declare Canvas
    size(600,400);
    background(0);
    frameRate(12);
    //Declares Image loop
    for(int i=0;i<images.length;i++){
      String imageName = "cards-"+i+".png";
      images[i]=loadImage(imageName);>
    
    }}
    
    void draw(){
     frame=frameCount%numFrames;}
    
    //Mouse pressed the draw random card  
     void mousePressed(){
      int a=(int)(random(rank.length));
      int b=(int)(random(suit.length));
      println(rank[a]+" "+"of"+" "+suit[b]);
    
     }
    
    
    //Press key d to bring up non-shuffled deck
      void keyPressed(){ 
        if (key=='d'){   
        frame=0;
        for(int i=0; i < rank.length; i++){
        for(int j=0; j < suit.length; j++){ 
          deckofCards[suit.length*i+j]=rank[i]+"of"+suit[j]; 
        println(rank[i]+" "+"of"+" "+suit[j]);  image(images[frame],width/4,0,300,400);
       frame++; }}}
    
    
    //Press s to shuffle deck
      else if(key== 's'){
       int t=deckofCards.length;
       for( int c=0; c<t;c++){
         int l=c+ (int)(random(t-c)); 
         String temp= deckofCards[l];
         deckofCards [l]= deckofCards [c];
         deckofCards [c]=temp;
         println(deckofCards[c]);
     }}}
    

    my apologies, here is my code, please help make images appear after I Press d

  • I told you how to do it

  • it didn't work

  • show your attempt

    ;-)

  • could you clarify how to fix it?, I don't think I understood?

  • What do you mean by state display?

  • edited November 2015

    Here is an example that demonstrates drawing things at different positions based on the x and y values used to do the looping, as well as a way to have your sketch draw different things depending on which state it is in.

    int state = 0;
    
    void setup() {
      size(441, 441);
      ellipseMode(CORNER);
    }
    
    void draw() {
      background(255);
      for (int x=0; x<10; x++) {
        for (int y=0; y<10; y++) {
          fill(25*x, 25*y, 255-12.5*(x+y));
          if( state == 0 ){
            rect(44*x, 44*y, 44, 44);
          }
          if( state == 1 ){
            ellipse(44*x, 44*y, 44, 44);
          }
        }
      }
      if(state == 2 ){
        textSize(36);
        textAlign(CENTER, CENTER);
        fill(0);
        text("NOTHING!", 220, 220);
      }
    }
    
    void mousePressed() {
      state++;
      state%=3;
    }
    
  • I can apply this to pressing different keys?

  • Yes. You already have code that detects when different keys are pressed. Depending on which key was pressed, you can set your sketch's state to a different value, and draw different things based on the current value of the state variable.

  • another problem Im having is that if I press the s key first, "null" comes out, I have to press the d key first inorder for the s key to display normal

  • That's because you are trying to get values from your deckOfCards array before you've assigned any string values to them!

  • how is this fixed please? <3

  • edited November 2015

    You should probably give each of your array's strings a starting value. I would do this in setup(), using a loop. It's actually the same thing you're doing as when the 'd' key is pressed. You might want to move that code into a new function, and just call it twice.

  • At this point there is a lot of help that has been given to you. The latest code you have posted does not reflect any of the advice you have been given. If you need more help, please attempt to resolve the existing issues first, and post the formatted version of your attempt.

  • I see, well thanks for the attempt

Sign In or Register to comment.