Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

anyone have any suggestions as to how i can make this a little more functional? it's the code for the score ticker on my site, http://www.qtoro.com. (uses the prototype js framework.) it was feeling pretty good til i had to put in those 'if' statements...

 Urtog.score = new function() {
  this.init = function() {
    this.upmod; // boolean.  if increasing score, it's true; if decreasing, false.
    this.unit = 34;  // the pixel height of one digit
    this.digits = $$('div#scoredigits img.digit');  // array of all the digit images
  }

  /* change the score.  
   * first arg is the # of points to add or subtract (if negative).  
   * second arg is optional, specifies which digit img element to mod.  the default is the "1's" digit.
   */
  this.mod = function( points, digit ) {
    this.upmod = (points > 0);
    digit = digit || this.digits.last();
    var di = this.digits.indexOf(digit);
    var dy = (this.upmod ? 0-this.unit : this.unit);
    (Math.abs(points)).times(function(i) {
      new Effect.MoveBy(digit, dy, 0, {
        queue: {position:"end", scope:"d"+di},
        duration: 0.075,
        transition: Effect.Transitions.linear,
        beforeSetup: preroll.bind(Urtog.score)
      });
    })
  }

  /* the callback that's run before the digit is modded.  
   * does two things:
   * 1. resets the image strip to the top/bottom if necessary 
   * 2. checks if we should simultaneously mod the 10x digit (e.g. going from 09=>10 or 10=>09)
   */
  function preroll(effect) {
    var d = effect.element;
    var di = this.digits.indexOf(d);
    var offset = d.style.top;
    if (this.upmod) { // adding points
      if (offset=="-340px") { // at the bottom of the strip, so reset to the top
        d.style.top=offset="0px";
      }
      if (offset=="-306px") { // transitioning from 9=>0, so mod the 10x digit, too
        this.mod( 1, this.digits[di-1] );
      }
    }
    else { // subtracting points
      if (this.digits.all(function(d) { return is_zero(d.style.top); })) { // we've hit zero!
        Effect.Queues.instances.keys().each(function(k) {
          if (k.match(/d[0-9]*$/)) { // cancel all pending digit mods -- digit effect queue names are like d3,d2,d1
            Effect.Queues.instances[k].each(function(e) { 
              e.cancel() 
            }); 
          }
        });
        new Effect.UrtogShuffle('scoredigits');  // :)
        return false;
      }
      if (is_zero(offset)) { // at the top of the strip, so reset to the bottom
        d.style.top=offset="-340px";
      }
      if (offset=="-340px") { // transitioning from 0=>9, so mod the 10x digit, too
        this.mod( -1, this.digits[di-1] );
      }
    }
  }

  // account for bizarre safari behavior... wtf is "-0px"??
  function is_zero(offset) {
    return (offset=="0px" || offset=="-0px");  
  }
 } // end Urtog.score


Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: