// This implementation makes use of a boolean variable 'touching' that // is changed whenever the balls first create contact or subsequently separate int radius = 20; color c = color(255); boolean touching = false; void setup() { size(500,400); } void draw() { background(204); if (dist(width/2, height/2, mouseX, mouseY) <= 2 * radius) { if (!touching) { touching = true; c = color(random(255), random(255), random(255)); } } else { touching = false; } fill(0); ellipse(width/2, height/2, 2*radius, 2*radius); fill(c); ellipse(mouseX, mouseY, 2*radius, 2*radius); }