Help - Using 's' key to stop a character on center position

edited May 2018 in Questions about Code

Hello Forum Team,

Can you help me understand the logic in a simple way to use the 's' key to load image 1 in a frozen in-place position and make the left and right direction images (images 2 and 3) temporarily disappear. (Here I already have the left and right images working with 'a' and 'd' keys.)

//images https://drive.google.com/file/d/1ZcnENU-SLzeiL8q4HMOxoNRBcAfBmEqy/view?usp=sharing

https://drive.google.com/file/d/1njKnrunrPtZIy4nXBfO2XcYqqsJx48k0/view?usp=sharing

https://drive.google.com/file/d/1atFLINdpkB8FK6mocuXl6Cjl4m67s0Ia/view?usp=sharing

int x1 = width/2; //START CHARACTER IN MIDDLE
int t = 255;
int r = 0;
boolean on = false;


PImage img1;
PImage img2;
PImage img3;

void setup(){
    size(800,600);
    smooth();
    strokeWeight( 2 );
    img1 = loadImage ("Mint3.png"); //CENTER IMAGE CURRENTLY NOT LOADING AFTER MOVEMENT BEGINS
    img2 = loadImage ("mintright.png");
    img3 = loadImage ("mintleft.png");
}

void draw(){
    background( 255 );
    tint (255,t);
    image(img1, x1, 300, 250,200); 

println ("x1=",x1);

    if (keyPressed) {    

   //go right   
      if (key == 'd' || key == 'D') {

          t = 0;
          noTint ();
          image(img2, x1, 300, 250,200);
          x1 += 1;


     //go left  
      } else if (key == 'a' || key == 'A') {

          t = 0;
          noTint ();
          image(img3, x1, 300, 250,200);    
          x1 -= 1;

         }  


 }   // <--close keyPressed 


} // <-- close draw
Tagged:

Answers

  • Answer ✓

    Write it out in English first, then convert this to code.

    // If a key is pressed,
    // If it's the a key,
    // move where the left image is and draw it.
    // or if it's the d key,
    // move where the right image is and draw it.
    // or if it's the s key,
    // just draw the middle image.
    
  • Hello TfGuy44!

    Thanks for working with me!

    Here's my revision based on your breakdown. Is there a simple way to make the states be continuous until the next key pressed?

    int x1 = width/2; //START CHARACTER IN MIDDLE
    
    
    PImage img1;
    PImage img2;
    PImage img3;
    
    void setup(){
        size(800,600);
        smooth();
        strokeWeight( 2 );
        img1 = loadImage ("Mint3.png"); //CENTER IMAGE CURRENTLY NOT LOADING AFTER MOVEMENT BEGINS
        img2 = loadImage ("mintright.png");
        img3 = loadImage ("mintleft.png");
    }
    
    void draw(){
        background( 255 );
        println("x1=", x1);
    
    
        if (keyPressed) {    
    
       //go right   
          if (key == 'd' || key == 'D') {
              image(img2, x1, 300, 250,200);
              x1 += 1;
          } 
    
    
    
        //go left    
         if (key == 'a' || key == 'A') {
              image(img3, x1, 300, 250,200);    
              x1 -= 1;              
             }          
    
    
    
           //just draw middle image in place 
           if  (key == 's' || key == 'S') {
               image (img1, x1, 300, 250, 200);
             }
    
        } // <-- close keyPressedß 
    
    } // <-- close draw
    
  • Answer ✓

    make a variable int state that indicates the current image (0,1,2)

    if a state = 0

    with b state = 1

    etc.

    in draw say outside if keypressed

         //go right   
              if (state== 0) {
                  image(img2, x1, 300, 250,200);
                  x1 += 1;
              } 
    
    
    
            //go left    
            else  if (state==1) {
                  image(img3, x1, 300, 250,200);    
                  x1 -= 1;              
                 }          
    
    
    
               //just draw middle image in place 
            else   if  (state==2) {
                   image (img1, x1, 300, 250, 200);
                 }
    
  • Thank you Chrisir!!!!!!!!!!!

    int x1 = width/2; //START CHARACTER IN MIDDLE
    int state = 0;
    
    PImage img1;
    PImage img2;
    PImage img3;
    
    void setup(){
        size(800,600);
        smooth();
        strokeWeight( 2 );
        img1 = loadImage ("Mint3.png"); //CENTER IMAGE CURRENTLY NOT LOADING AFTER MOVEMENT BEGINS
        img2 = loadImage ("mintright.png");
        img3 = loadImage ("mintleft.png");
    }
    
    void draw(){
        background( 255 );
        println("x1=", x1);
    
    
        if (keyPressed) {    
    
       //go right   
          if (key == 'd' || key == 'D') {
              state = 2;
          } 
    
    
    
        //go left    
         if (key == 'a' || key == 'A') {
              state = 1;            
             }          
    
    
    
           //just draw middle image in place 
           if  (key == 's' || key == 'S') {
               state = 0;
             }
    
        } // <-- close keyPressed
    
    
    
    //go right   
         if (state== 2) {
             image(img2, x1, 300, 250,200);
             x1 += 1;
         } 
    
    
    
       //go left    
       else  if (state==1) {
             image(img3, x1, 300, 250,200);    
             x1 -= 1;              
            }          
    
    
    
          //just draw middle image in place 
       else   if  (state==0) {
              image (img1, x1, 300, 250, 200);
            }
    
    } // <-- close draw
    
  • Answer ✓

    ;-)

Sign In or Register to comment.