Error in Processing (key, keyPressed, map,... don´t exist)

edited February 2017 in Library Questions

Hello. I have to write a game for an essay like class test but now i got stuck. I have errors on every "key" or "keyPressed" and even more because they "don´t exist". Big thanks for every helpful idea.

Answers

  • If you have questions, just ask.

  • Showing us the code is more useful than just telling us about the code.

    Exact error messages are better than your vague descriptions of errors.

    Titles like "help me" are no use to anyone.

  • edited February 2017
    import queasycam.*;
    
    int size = 29;
    int scl = 10;
    
    
    
    Baustein[] wand;
    Baustein[][] boden;
    int cols, rows;
    int w = size * scl;
    int h = size * scl;
    float speed= 0.1;
    float px = 0;
    float pz = 0;
    
    
    MazeGenerator mz;
    Player player;
    
    
    
    void setup() {
     // size(1000,800, P3D);
      fullScreen(P3D);
      strokeWeight(2);
    
       player = new Player(this, size);
    
      mz = new MazeGenerator(size, scl);
      mz.prepare();
    
      cols = w/scl;
      rows = h/scl;
    
      boden = new Baustein[cols][rows];
      wand = new Baustein[0];
    
      player.position = PVector.add(new PVector(0,0,0), new PVector(scl, -scl, scl)); 
    
    
      for (int i = 0; i<cols; i++) {
           for (int j = 0; j<rows; j++) {
                float x = i * scl;
                float y = 0;
                float z = j * scl;
                boden[i][j] = new Baustein(x,y,z,scl,scl,scl);
                boden[i][j].update();
                boden[i][j].display();
                if(mz.Maze[i*rows + j] == 0){
                  Baustein[] temp = new Baustein[wand.length + 1];
                  temp = wand;
                  wand = new Baustein[temp.length];
                  wand = temp;
                  wand[wand.length - 1] = new Baustein(x,-(scl/2),z,scl,scl,scl);
                  wand[wand.length - 1].update();
                  wand[wand.length - 1].display();
                }
           }
         }
    
    
    
    
    }
    
    
    void draw() {
    
      background(0);
         stroke(255);
         //noStroke();
         fill(255, 200, 0);
         lights();
    
          for (int i = 0; i<cols; i++) {
           for (int j = 0; j<rows; j++) {
                boden[i][j].update();
                boden[i][j].display();
                }
           }
           for(int i = 0; i<wand.length ; i++){
             wand[i].update();
             wand[i].display(); 
           }
         }
          /*
         for (int i = 0; i<cols; i++) {
           for (int j = 0; j<rows; j++) {
                float x = i * scl;
                float y = 0;
                float z = j * scl;
                boden[i][j] = new Baustein(x,y,z,scl,scl,scl);
                boden[i][j].update();
                boden[i][j].display();
                if(mz.Maze[i*rows + j] == 0){
                  Baustein temp = new Baustein(x,-(scl/2),z,scl,scl,scl);
                  temp.update();
                  temp.display();
                }
           }
         }
         */
    
         player.update();
    
    
      }
    
  • class Baustein{
    
    
     color fillColor;
     boolean visited;
     float pX;
     float pY;
     float pZ;
     float wi;
     float he;
     float de;
    
       Baustein(float x, float y, float z, float w, float h, float d){
        pX = x;
        pY = y;
        pZ = z;
        wi = w;
        he = h;
        de = d;
    
    
        fillColor = color(0);
    
      }
    
      void update(){
        float playerLeft = player.position.x - player.dimensions.x/2;
        float playerRight = player.position.x + player.dimensions.x/2;
        float playerTop = player.position.y - player.dimensions.y/2;
        float playerBottom = player.position.y + player.dimensions.y/2;
        float playerFront = player.position.z - player.dimensions.z/2;
        float playerBack = player.position.z + player.dimensions.z/2;
    
        float boxLeft = pX - wi/2;
        float boxRight =pX + wi/2;
        float boxTop = pY - he/2;
        float boxBottom = pY +he /2;
        float boxFront = pZ -de/2;
        float boxBack = pZ + de/2;
    
        float boxLeftOverlap = playerRight - boxLeft;
        float boxRightOverlap = boxRight - playerLeft;
        float boxTopOverlap = playerBottom - boxTop;
        float boxBottomOverlap = boxBottom - playerTop;
        float boxFrontOverlap = playerBack - boxFront;
        float boxBackOverlap = boxBack - playerFront;
    
        if (((playerLeft > boxLeft && playerLeft < boxRight || (playerRight > boxLeft && playerRight < boxRight)) && ((playerTop > boxTop && playerTop < boxBottom) || (playerBottom > boxTop && playerBottom < boxBottom)) && ((playerFront > boxFront && playerFront < boxBack) || (playerBack > boxFront && playerBack < boxBack)))){
          float xOverlap = max(min(boxLeftOverlap, boxRightOverlap), 0);
          float yOverlap = max(min(boxTopOverlap, boxBottomOverlap), 0);
          float zOverlap = max(min(boxFrontOverlap, boxBackOverlap), 0);
    
          if (xOverlap < yOverlap && xOverlap < zOverlap){
            if (boxLeftOverlap < boxRightOverlap){
              player.position.x = boxLeft - player.dimensions.x/2;
            } else {
              player.position.x = boxRight + player.dimensions.x/2;
            }
          }
    
          else if (yOverlap < xOverlap && yOverlap < zOverlap){
            if (boxTopOverlap < boxBottomOverlap){
              player.position.y = boxTop - player.dimensions.y/2;
              player.velocity.y = 0;
              player.grounded = true;
            } else {
              player.position.y = boxBottom + player.dimensions.y/2;
            }
          }
    
          else if (zOverlap < xOverlap && zOverlap < yOverlap){
            if (boxFrontOverlap < boxBackOverlap){
              player.position.z = boxFront - player.dimensions.x/2;
            } else {
              player.position.z = boxBack + player.dimensions.x/2;
            }
          }
    
        }
        if(boxFrontOverlap<8 && boxRightOverlap <8 && boxRightOverlap > -8    && boxFrontOverlap> -8  && boxTopOverlap <15 && keyPressed && key == 'm'){
            fillColor= color(70);
          }
    
      }
    
      void display(){
        pushMatrix();
        translate(pX,pY, pZ);
        fill(fillColor);
        box(wi, he, de);
        popMatrix();
      }
    
      void moveDown(){
        pY += 5;
      }
    }
    
  • class MazeGenerator{
    
    
    
    int W; // Maze Width 
    int H; // Maze Height 
    int S;  // Block size 
    int g_intDepth; 
    
    int[] Maze; 
    int intIndex, x, y; 
    
     public MazeGenerator(int size, int scale){
        W=size;
        H=size;
        S=scale;
        Maze  = new int[W*H]; ;
      }
    
    void prepare() { 
    
    
      for (intIndex = 0; intIndex < (W*H)-1; intIndex++) Maze[intIndex] = 0; 
    
      g_intDepth = 0; 
    
      DigMaze(Maze, 1, 1); 
      Maze[1*W+1] = 2; 
      Maze[(scl-2)*W+(scl-1)] = 1; 
    
      for (y = 0; y<H; y++ )  
       for (x = 0; x<W; x++ )  
    
          switch( Maze[y*W+x] ) { 
            case 0: noStroke(); fill(0); rect(x*S,y*S,x*S+S,y*S+S); break; 
            case 2: stroke(255); fill(0); rect(x*S,y*S,x*S+S,y*S+S); break; 
            case 1: stroke(255); fill(255); rect(x*S,y*S,x*S+S,y*S+S); break; 
          }; 
    
    } 
    
    void loop() { 
    } 
    
    
    
    void DigMaze(int[] Maze, int x, int y) { 
      int newx; 
      int newy; 
    
      g_intDepth = g_intDepth + 1; 
    
      Maze[y*W+x] = 1; 
    
      int intCount = ValidCount(Maze, x, y); 
    
      while (intCount > 0) { 
        switch (int(random(1)*4)) { 
          case 0: 
            if (ValidMove(Maze, x,y-2) > 0){ 
              Maze[(y-1)*W+x] = 1; 
              DigMaze(Maze, x,y-2); 
            } 
            break; 
          case 1: 
            if (ValidMove(Maze, x+2,y) > 0) { 
              Maze[y*W+x+1] = 1; 
              DigMaze (Maze, x+2,y); 
            } 
            break; 
          case 2: 
            if (ValidMove(Maze, x,y+2) > 0) { 
              Maze[(y+1)*W+x] = 1; 
              DigMaze (Maze, x,y+2); 
            } 
            break; 
          case 3: 
            if (ValidMove(Maze, x-2,y) > 0) { 
              Maze[y*W+x-1] = 1; 
              DigMaze (Maze, x-2,y); 
            } 
            break; 
        } // end switch 
        intCount = ValidCount(Maze, x, y); 
      } // end while 
    
      g_intDepth = g_intDepth - 1; 
    } // end DigMaze() 
    
    
    public int ValidMove(int[] Maze, int x, int y) { 
      int intResult = 0; 
      if (x>=0 && x<W && y>=0 && y<H 
               && Maze[y*W+x] == 0) intResult = 1; 
      return intResult; 
    } 
    
    public int ValidCount(int[] Maze, int x, int y) { 
      int intResult = 0; 
    
      intResult += ValidMove(Maze, x,y-2); 
      intResult += ValidMove(Maze, x+2,y); 
      intResult += ValidMove(Maze, x,y+2); 
      intResult += ValidMove(Maze, x-2,y); 
    
      return intResult; 
    } 
    }
    
  • class Player extends QueasyCam {
      PVector dimensions;
      PVector velocity;
      PVector gravity;
      boolean grounded;
    
      Player(PApplet applet, int size){
        super(applet);
        speed = map(size,1,49,0.01,0.1);
        dimensions = new PVector(1, 3, 1);
        velocity = new PVector(0, 0, 0);
        gravity = new PVector(0, 0.01, 0);
        grounded = false;
      }
    
      void update(){
        velocity.add(gravity);
        position.add(velocity);
    
        if (grounded && keyPressed && key == ' '){
          grounded = false;
          velocity.y = -0.5;
          position.y -= 0.1;
        }
      }
    
     }
    
  • edited February 2017
    Error on "update" Consider adding "Identifier"   Test1    105
    The function "color(int)" does not exist              Baustein   22
    The variable "player" does not exist                   Baustein   27,...
    The function "min(float,float) does not exist      Baustein   49,...
    also keyPressed, key, pushMatrix(), fill(int), ...
    

    Error on "}"
    Player 27

    I hope that helped.

  • Test1 is the name of the sketch, I take it?

    And you're programming this using the processing ide? Or some other ide, like eclipse?

  • (Because those errors suggest that the processing pre-processor isn't running, which means you don't get access to all the inbuilt functions like color and key)

    Read the tutorial about using processing with eclipse, specifically the bit about multiple classes and passing the parent around.

  • It if you're using some other IDE, you must have had to use some container class to hold the sketch, right? So you'd have written something like public class Test1 extends PApplet.

  • Yes koogs. Test1 is the name of the sketch. I am using the Processing IDE. The strange thing is that in the Test1 tab everything works fine. But not in the other tabs.

  • Answer ✓

    ok, in your Test1 tab, hit ctrl-t

    you'll see that you've one too many }s meaning that player.update(); is outside a method. that's probably causing all the other errors.

    you probably want to delete line 86

  • ctrl-t is your friend here. properly indented code makes the sructure easier to see.

  • edited February 2017

    or maybe delete line 80. it's a tossup as to which bracket is wrong.

  • Thank you koogs. I found it. Now i have to look for the "could not run the sketch (target vm failed to initialize)" error.

  • after fixing that, then this is the next problem

    wand = new Baustein[0];
    ...
    wand[wand.length - 1]
    

    because wand.length = 0 and wand[-1] throws an error.

    god, i hate debugging fullscreen programs - when they fail they leave behind a screen that you can't close and that gets in the way of accessing everything else.

  • I will fix it. Thank you. Yes, I just turned the fullscreen off too.

  • "could not run the sketch (target vm failed to initialize)"

    in my experience the above is intermittent and usually not something wrong with the sketch. often it'll go away if you just rerun it

Sign In or Register to comment.