examples/conway.js
function start(name) {
  var orb = spheros[name],
      contacts = 0,
      age = 0,
      alive = false;

  orb.detectCollisions();

  born();

  orb.on("collision", function() { contacts += 1; });

  setInterval(function() { if (alive) { move(); } }, 3000);
  setInterval(birthday, 10000);

  // roll Sphero in a random direction
  function move() {
    orb.roll(60, Math.floor(Math.random() * 360));
  }

  // stop Sphero
  function stop() {
    orb.stop();
  }

  // set Sphero's color
  function color(str) {
    orb.color(str);
  }

  function born() {
    contacts = 0;
    age = 0;
    life();
    move();
  }

  function life() {
    alive = true;
    color("green");
  }

  function death() {
    alive = false;
    color("red");
    stop();
  }

  function enoughContacts() {
    return contacts >= 2 && contacts < 7;
  }

  function birthday() {
    age += 1;

    if (alive) {
      console.log("Happy birthday,", name);
      console.log("You are", age, "and had", contacts, "contacts.");
    }

    if (enoughContacts()) {
      if (!alive) { born(); }
    } else {
      death();
    }
  }
}
function start(name) {
  var orb = spheros[name],
      contacts = 0,
      age = 0,
      alive = false;

  orb.detectCollisions();

  born();

  orb.on("collision", function() { contacts += 1; });

  setInterval(function() { if (alive) { move(); } }, 3000);
  setInterval(birthday, 10000);

  // roll Sphero in a random direction
  function move() {
    orb.roll(60, Math.floor(Math.random() * 360));
  }

  // stop Sphero
  function stop() {
    orb.stop();
  }

  // set Sphero's color
  function color(str) {
    orb.color(str);
  }

  function born() {
    contacts = 0;
    age = 0;
    life();
    move();
  }

  function life() {
    alive = true;
    color("green");
  }

  function death() {
    alive = false;
    color("red");
    stop();
  }

  function enoughContacts() {
    return contacts >= 2 && contacts < 7;
  }

  function birthday() {
    age += 1;

    if (alive) {
      console.log("Happy birthday,", name);
      console.log("You are", age, "and had", contacts, "contacts.");
    }

    if (enoughContacts()) {
      if (!alive) { born(); }
    } else {
      death();
    }
  }
}
function start ( name ) { var orb = spheros [ name ] , contacts = 0 , age = 0 , alive = false ; orb . detectCollisions ( ) ; born ( ) ; orb . on ( "collision" , function ( ) { contacts += 1 ; } ) ; setInterval ( function ( ) { if ( alive ) { move ( ) ; } } , 3000 ) ; setInterval ( birthday , 10000 ) ; // roll Sphero in a random direction function move ( ) { orb . roll ( 60 , Math . floor ( Math . random ( ) * 360 ) ) ; } // stop Sphero function stop ( ) { orb . stop ( ) ; } // set Sphero's color function color ( str ) { orb . color ( str ) ; } function born ( ) { contacts = 0 ; age = 0 ; life ( ) ; move ( ) ; } function life ( ) { alive = true ; color ( "green" ) ; } function death ( ) { alive = false ; color ( "red" ) ; stop ( ) ; } function enoughContacts ( ) { return contacts >= 2 && contacts < 7 ; } function birthday ( ) { age += 1 ; if ( alive ) { console . log ( "Happy birthday," , name ) ; console . log ( "You are" , age , "and had" , contacts , "contacts." ) ; } if ( enoughContacts ( ) ) { if ( ! alive ) { born ( ) ; } } else { death ( ) ; } } }
tells each Sphero what to do
tells each Sphero what to do