Fixing "processing the makeymakey" code

edited November 2017 in Questions about Code

Hi Everyone,

I am trying to follow the tutorial "Processing the MakeyMakey" that was released by SparkFun. The pdf can be found here

So far, I have a maze image loading and the ellipse is in place. However, the keyPressed() code doesn't seem to be responding in that nothing happens to my ellipse when I use the arrow keys. Here's my code:

 
int x = 170;
int y = 315;
PImage maze;
float hit;

void setup() {
  size(322, 322);
  maze = loadImage("maze.png");
}

void draw() {
  background(maze);
  hit = red(get(x, y));
  
  if(hit == 0); {
    x = 168;
    y = 318;
  }
 
  image(maze, 0, 0);
  
  pushMatrix();
  translate(x, y);
  fill(255, 0, 0);
  ellipse(0, 0, 5, 5);
  popMatrix();
    
  if(x > 148 && x < 158) {
    if(y < 2) {
      fill (0);
      text("YOU WIN!", 50, 150, 200, 50);
    }
  }
}

void keyPressed() {
  
      if(key==CODED && keyCode==UP) {
   y--;
  }
  if(key==CODED && keyCode==DOWN) {
    y++;
  }
  if(key==CODED && keyCode==RIGHT) {
    x++;
  }
  if(key==CODED && keyCode==LEFT) {
    x--;
  }
  
}

I added the variable typses "PImage maze" and "float hit" because they were missing from the tutorial. The code loads and there aren't any errors, it just doesn't do what I want it to do.

Tagged:

Answers

  • @amcbride the problem is your hit variable and statement

      if(hit == 0); {
        x = 168;
        y = 318;
      }
    

    I dont know what they are supposed to do but thats what stops your ellipse from moving

  • I think I figured that part out last night but it still doesn't quite answer my question. That part of the code is supposed to check if the ellipse hits the black edge of the maze wall and, if it does, reset the position of the ellipse to the beginning of the maze. I know that's what is keeping the ellipse from moving but I'm not sure why. I think it has do with the code that's above it which checks for the color value which is used in the hit function later.

     hit = red(get(x, y)); 

    If I comment out the if statement with the hit variable, the ellipse does move but then just moves freely and doesn't respond at all to the walls of the maze. Thanks for the response so far. Any more help would be greatly appreciated.

  • println(hit);
    
  • if the pixel isn't matching the colour you THINK it is then you can just print the value out and see what the problem is.

  • I added a println(hit) funtcion to print the value of the "hit" variable to the console. the above block is functioning properly. It gives a value of 255 until it comes in contact with the center of a maze line, at which point, it returns a value of zero so back to the drawing board!

  • wow koogs, we were seriously typing our last responses at the same time! crazy. Thanks though, that definitely gives me some insight.

  • Answer ✓

    ha, it's not that.

      if (hit == 0); {
        println("hit");
        x = 168;
        y = 318;
      }
    

    hit is printed every frame, so your condition is always true.

    BUT IS IT?

    if i add a couple of newlines the code becomes this

      if (hit == 0);
    
     {
        println("hit");
        x = 168;
        y = 318;
      }
    

    the condition is false but the conditional block is that single ; on the same line as the condition. everything between the {}s is run unconditionally...

    delete the extraneous ;

  • edited November 2017

    HA! omg I can't believe I missed that. Thanks for your help. I feel like a real dummy right now.

  • Answer ✓

    (printing the value of hit would make more sense, yes. but i'd already done that in the code above and was only bothered whether the conditional block was running at this point.)

  • I figured that out after I posted the comment so I deleted it. Thanks for your help! I appreciate it.

Sign In or Register to comment.