Sample bounce; int xspeed; int yspeed; int x; int y; int r; //our setup function void setup() { size(400,200); //define size xspeed = 3; //(int) random(-5,5); //if we want random (note "random" gives us a float yspeed = 1; //(int) random(-5,5); //so we have to "cast" it back into an "int") x = width/2; //initialize our x,y,r variables y = height/2; r = 10; Sonia.start(this); // Start Sonia engine. // create a new sample object. bounce = new Sample("ouch.wav"); } void loop() { background(50); //first we draw the background ellipseMode(CENTER_DIAMETER); //set our ellipse mode noStroke(); fill(255,200,0); //set ellipse color ellipse(x,y,r,r); //draw ellipse //radius always decreases back to 10 if it's bigger if (r > 10) { r--; } //adjust x,y based on speed x = x + xspeed; y = y + yspeed; //acount for bouncing off edges if ((x > width) || (x < 0)) { xspeed = xspeed * -1; bounce.play(); r = 30; //adjust radius when bouncing } if ((y > height) || (y < 0)) { yspeed = yspeed * -1; bounce.play(); r = 30; } } // Safely close the sound engine upon Browser shutdown. public void stop(){ Sonia.stop(); super.stop(); }