Functions

edited October 2013 in Questions about Code

my program is not running and I am trying to get it to do some functions

//Variables Established
float X=100, Y=100, r;
boolean t;
//Setup a loop to draw once
void setup() {
  size(200, 200);
  ellipseMode(CENTER);
  rectMode(CENTER);
}
//Command: Draw the background and alien
void draw() {
  background(0);
  display();
  move();
  bounce();
}
void move() {
x = x + speed;
}
// Alien points, color, and outline set
void display() {
  translate(X, Y);
  rotate(r=(r+(t?.1:0))%TWO_PI);
  stroke(39, 242, 29);
  fill(255);
  triangle(0, 0, 30, 80, -30, 80);
  fill(39, 242, 29);
  ellipse(0, 5, 60, 60);
  stroke(127);
  line(10, 20, -10, 20);
  fill(mouseX, 0, mouseY);
  ellipse(-19, 0, 16, 25);
  ellipse(19, 0, 16, 25);
  stroke(255);
  line(-10, 80, -20, 100);
  line(-20, 100, -10, 60);
  line(10, 80, 20, 100);
  line(20, 100, 10, 50);
}
void bounce() {
  if ((x > width) || (x < 0)) {
    speed = speed * -1;
  }

//Direction of Zoog 
void keyPressed() {
  if (key==CODED)
  {
    if (keyCode==UP)Y--;
    if (keyCode==DOWN)Y++;
    if (keyCode==RIGHT)X++;
    if (keyCode==LEFT)X--;
  }
  if (key=='t') {
    t=!t;
  }
}
// end of sketch 

Answers

  • edited October 2013

    There were just a few things wrong with your code. Variables X and Y should probably be x and y. You were using X and Y in some places... Usually people writing Processing sketches use camelCase for their variables. You didn't define the speed variable anywhere! You didn't close the bounce() function.

    //Variables Established
    float x = 100, y = 100, r;
    float speed = 1;
    boolean t;
    //Setup a loop to draw once
    void setup() {
      size(200, 200);
      ellipseMode(CENTER);
      rectMode(CENTER);
    }
    //Command: Draw the background and alien
    void draw() {
      background(0);
      display();
      move();
      bounce();
    }
    void move() {
    x = x + speed;
    }
    // Alien points, color, and outline set
    void display() {
      translate(x,y);
      rotate(r=(r+(t?.1:0))%TWO_PI);
      stroke(39, 242, 29);
      fill(255);
      triangle(0, 0, 30, 80, -30, 80);
      fill(39, 242, 29);
      ellipse(0, 5, 60, 60);
      stroke(127);
      line(10, 20, -10, 20);
      fill(mouseX, 0, mouseY);
      ellipse(-19, 0, 16, 25);
      ellipse(19, 0, 16, 25);
      stroke(255);
      line(-10, 80, -20, 100);
      line(-20, 100, -10, 60);
      line(10, 80, 20, 100);
      line(20, 100, 10, 50);
    }
    void bounce() {
      if ((x > width) || (x < 0)) {
        speed = speed * -1;
      }
    }
    
    //Direction of Zoog 
    void keyPressed() {
      if (key==CODED)
      {
        if (keyCode==UP)y--;
        if (keyCode==DOWN)y++;
        if (keyCode==RIGHT)x++;
        if (keyCode==LEFT)x--;
      }
      if (key=='t') {
        t=!t;
      }
    }
    // end of sketch 
    
  • You have made several errors which I have corrected below.

    (1) The method bounce() was not 'closed' in that the closing } was missing at line 44.

    (2) You declared variables X and Y then later used x and y. Java is case-sensitive and by convention all variables and method/function names start with a lowercase letter so I have changed these accordingly.

    (3) You had not declared a variable called speed but attempted to use it in the bounce method.

    In future to post code into this forum, copy and paste it from Processing, then highlight it in the edit box and click on the C button. This will tab all the code to the right and will then be formatted nicely in you post. I did this for you above.

    //Variables Established
    float x=100, y=100, r, speed = 2;
    boolean t;
    //Setup a loop to draw once
    void setup() {
      size(200, 200);
      ellipseMode(CENTER);
      rectMode(CENTER);
    }
    //Command: Draw the background and alien
    void draw() {
      background(0);
      display();
      move();
      bounce();
    }
    void move() {
      x = x + speed;
    }
    // Alien points, color, and outline set
    void display() {
      translate(x, y);
      rotate(r=(r+(t?.1:0))%TWO_PI);
      stroke(39, 242, 29);
      fill(255);
      triangle(0, 0, 30, 80, -30, 80);
      fill(39, 242, 29);
      ellipse(0, 5, 60, 60);
      stroke(127);
      line(10, 20, -10, 20);
      fill(mouseX, 0, mouseY);
      ellipse(-19, 0, 16, 25);
      ellipse(19, 0, 16, 25);
      stroke(255);
      line(-10, 80, -20, 100);
      line(-20, 100, -10, 60);
      line(10, 80, 20, 100);
      line(20, 100, 10, 50);
    }
    void bounce() {
      if ((x > width) || (x < 0)) {
        speed = speed * -1;
      }
    }
    //Direction of Zoog
    void keyPressed() {
      if (key==CODED)
      {
        if (keyCode==UP)y--;
        if (keyCode==DOWN)y++;
        if (keyCode==RIGHT)y++;
        if (keyCode==LEFT)y--;
      }
      if (key=='t') {
        t=!t;
      }
    }
    // end of sketch 
    
Sign In or Register to comment.