Don't like ads? PRO users don't see any ads ;-)
Guest

lime pong demo

By: a guest on Jun 15th, 2012  |  syntax: JavaScript  |  size: 1.94 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. goog.provide('pong.Player');
  2.  
  3. goog.require('lime.RoundedRect');
  4.  
  5. pong.Player = function(is_down) {
  6.     lime.Sprite.call(this);
  7.  
  8.     //this.setFill(200,100,0,.3);
  9.     this.setSize(80, 50);
  10.  
  11.     this.is_down = is_down;
  12.  
  13.     this.setAnchorPoint(.5, 0);
  14.  
  15.     var front_color = '#000';
  16.     var back_color = '#DDD';
  17.     var grad = new lime.fill.LinearGradient().
  18.         addColorStop(0, is_down ? front_color : back_color).
  19.         addColorStop(1, is_down ? back_color : front_color);
  20.     this.inner = new lime.RoundedRect().setSize(80, 15).setFill(grad).setAnchorPoint(.5, 0);
  21.     this.appendChild(this.inner);
  22.  
  23.  
  24.     if (!is_down) {
  25.         this.setAnchorPoint(.5, 1);
  26.         this.inner.setAnchorPoint(.5, 1);
  27.     }
  28.  
  29.     this.score = 0;
  30. };
  31. goog.inherits(pong.Player, lime.Sprite);
  32.  
  33. pong.Player.prototype.enableInteraction = function() {
  34.     var py = this.getPosition().y;
  35.     goog.events.listen(this, ['mousedown', 'touchstart'], function(e) {
  36.         var whalf = this.getSize().width / 2;
  37.         var width = this.getParent().getSize().width - whalf;
  38.         var py = this.getPosition().y;
  39.         e.startDrag(false, new goog.math.Box(py, width, py, whalf));
  40.     },false, this);
  41. };
  42.  
  43. pong.Player.prototype.enableSimulation = function() {
  44.     this.v = 0;
  45.     this.a = 0;
  46. };
  47.  
  48. pong.Player.prototype.updateTargetPos = function(ballx,vy,dt) {
  49.     var px = this.getPosition().x;
  50.     var diff = ballx - px;
  51.  
  52.     this.a = diff;
  53.     if (vy > 0) this.a *= .3;
  54.  
  55.     this.v += this.a;
  56.     var absv = Math.abs(this.v);
  57.     if (absv > 300) this.v = 300 * (this.v > 0 ? 1 : -1);
  58.  
  59.     this.v *= .9;
  60.  
  61.     px += this.v * dt * 0.001;
  62.  
  63.     var whalf = this.getSize().width / 2;
  64.     var width = this.getParent().getSize().width - whalf;
  65.  
  66.     if (px < whalf) {
  67.         px = whalf;
  68.         this.v *= -1;
  69.     }
  70.     else if (px > width) {
  71.         px = width;
  72.         this.v *= -1;
  73.     }
  74.  
  75.     this.setPosition(px, this.getPosition().y);
  76. };