How could I make an image move using arrow keys?

Here is my code: My attempt is under //---CONTROLS PLAYER---//

//---BOOLEAN(SET VARIABLE (TRUE/FALSE)) FOR CURSOR HIDE---//
boolean Lpressed = false;
int FPS = 120;
int Wait = 150;
int IW = 95;
int IH = IW + 10;

//---IMAGES---//
PImage bg;

//---THE PLAYER---//
Player Erstad;
class Player {

  void setup() {
    Erstad = loadImage("Erstad.png");
  }
  PImage Erstad;
  float posX;
  int y; 
  Player() {
    bg = loadImage("City.jpg");
    Erstad = loadImage("Erstad.png");
    y=height-105;
    posX = (width/2) - 60;
  }

  void draw() {
    image(bg, 0, 0, width, height);
    image(Erstad, posX, y, IW, IH);
    imageMode(CORNER);
  }
  //---CONTROLS PLAYER---//
  void keyPressed() {
    if (keyCode == RIGHT)
      posX++;
    if  (keyCode == LEFT) 
      posX--;
  }
}

void setup() {

  //---SCREEN SETTINGS---//
  fullScreen(P2D, 1);
  frameRate(FPS);

  //---CURSOR DESIGN---//
  cursor(HAND);
  noCursor();

  Erstad = new Player();

  //---NAME OF THE GAME TEXT TOP LEFT CORNER---//
  String s = "The Legend of Erstad ©Josh Lisk";
  fill(230);
  text(s, (width/2)-680, (height/2)-380, 140, 90);
}
//---MAIN LOOP---//
void draw() {
  //---CLOSE PROGRAM - WHEN ESC PRESSED---//
  if (keyPressed) {
    if (keyCode == ESC ) {
      exit();
    }
  }
  //---HIDE/SHOW CURSOR---//
  if (Lpressed == false) {  
    if  (keyPressed) {
      if (key == 'l' || key == 'L') {
        Lpressed = true ;
        noCursor();
        //---DELAY BETWEEN THE END AND THE START OF THE LOOP---//
        delay(Wait);
      }
    }
  } else if (Lpressed == true) { 
    if  (keyPressed) {
      if (key == 'l' || key == 'L') {
        Lpressed = false;
        cursor();
        //---DELAY BETWEEN THE END AND THE START OF THE LOOP---//
        delay(Wait);
      }
    }
  }
  //---DRAW THE PLAYER---//
  Erstad.draw();
}

Answers

  • edited November 2017

    WAH! @-)

    https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest

    At some point you must draw your image:

    image(img, x, y);
    

    You draw it at some position, (x,y). If x and y are variables:

    float x, y;
    

    You can just change their values and thus the image will change positions:

    void keyPressed(){
      if( keyCode == UP ) y--;
    }
    
  • edited November 2017

    I can't still get the sketch to work?

  • Now that I can see your code, I see that you are using delay()!

    NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO. ^#(^

    Unless you are doing weird black magic with multiple threads, you should not be using delay. Whatever you think it does is not what it does. It is not doing what you think it is doing. You don't need to use it. Don't use. NO. NO. DO NOT USE DELAY().

    Just remove all calls to delay and post your code again without any calls to delay in it. Just don't have it in there at all. I don't care it it ruins the feel of what you are trying to do. Just take it out.

    NO. CALLS. TO. DELAY().

    Then we'll try again.

  • edited November 2017

    Ok, now I have deleted that part

    //---BOOLEAN(SET VARIABLE (TRUE/FALSE)) FOR CURSOR HIDE---//
    boolean Lpressed = false;
    int FPS = 120;
    int IW = 95;
    int IH = IW + 10;
    
    //---IMAGES---//
    PImage bg;
    
    //---THE PLAYER---//
    Player Erstad;
    class Player {
    
      void setup() {
        Erstad = loadImage("Erstad.png");
      }
      PImage Erstad;
      float posX;
      int y; 
      Player() {
        bg = loadImage("City.jpg");
        Erstad = loadImage("Erstad.png");
        y=height-105;
        posX = (width/2) - 60;
      }
    
      void draw() {
        image(bg, 0, 0, width, height);
        image(Erstad, posX, y, IW, IH);
        imageMode(CORNER);
      }
      //---CONTROLS PLAYER---//
      void keyPressed() {
        if (keyCode == RIGHT)
          posX++;
        if  (keyCode == LEFT) 
          posX--;
      }
    }
    
    void setup() {
    
      //---SCREEN SETTINGS---//
      fullScreen(P2D, 1);
      frameRate(FPS);
    
      //---CURSOR DESIGN---//
      cursor(HAND);
      noCursor();
    
      Erstad = new Player();
    
      //---NAME OF THE GAME TEXT TOP LEFT CORNER---//
      String s = "The Legend of Erstad ©Josh Lisk";
      fill(230);
      text(s, (width/2)-680, (height/2)-380, 140, 90);
    }
    //---MAIN LOOP---//
    void draw() {
      //---CLOSE PROGRAM - WHEN ESC PRESSED---//
      if (keyPressed) {
        if (keyCode == ESC ) {
          exit();
        }
      }
      //---DRAW THE PLAYER---//
      Erstad.draw();
    }
    
Sign In or Register to comment.