Friday, 4 March 2011

_06_Functions

int batX = 240;
int batY = 700;


//Here we go again
void setup() {
  size( 480, 800 );
  background( 0 );
}

//Here the code to control and draw the bat has been moved out of the 'draw' function
//which is now a lot simpler.  The program does exactly the same thing as before,
//the code has just been reorganized a bit.  This makes the 'draw' function much easier
//read.
void draw() {
  background( 0 );

  moveTheBat();
}

//This is not a built in function.  We can define as many of our own functions
//as we need and use them from (almost) anywhere we want.
void moveTheBat() {
  fill( 255 );
 
  batX = mouseX;
 
  //if the bat 'hits' the left edge move it back
  if( batX < 50 ) {
    batX = 50;
  }
 
  //if the bat 'hits' the right edge move it
  if( batX > 430 ) {
    batX = 430;
  }
 
  rect( batX - 50, batY, 100, 25 );
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.