1 /** 2 * The golden button 3 * @param {BB_Sprite} sprite 4 * @param {Number} x 5 * @param {Number} y 6 * @constructor 7 */ 8 function BB_Coin(sprite, x, y) { 9 this.sprite = sprite; 10 this.x = x || 0; 11 this.y = y || 0; 12 this.r = 5; 13 this.a = Math.round(Math.random() * 23); 14 this.has = false; 15 this.hide = false; 16 } 17 18 /** 19 * Extends CP_Item 20 */ 21 BB_Coin.prototype = new CP_Item(20, 20); 22 23 /** 24 * Draw the button 25 */ 26 BB_Coin.prototype.paint = function(ctx) { 27 if (!this.hide) { 28 this.sprite.paint(ctx, this.x, this.y, this.w, this.h, this.a * this.w, 80); 29 } 30 }; 31 32 /** 33 * Animation thread 34 */ 35 BB_Coin.prototype.run = function() { 36 if (this.has && !this.hide) { 37 this.hide = this.x < 1 && this.y < 1; 38 this.a = this.a < 23 ? this.a + 1 : 0; 39 this.x -= this.x / 10; 40 this.y -= this.y / 10; 41 return true; 42 } 43 return false; 44 }; 45 46 /** 47 * Check ball collision 48 * @param {BB_Ball} ball 49 * @returns {Boolean} 50 */ 51 BB_Coin.prototype.check = function(ball) { 52 if (this.has) { 53 return true; 54 } 55 var vx = this.ox() - ball.ox(), 56 vy = this.oy() - ball.oy(), 57 d = Math.sqrt((vx * vx) + (vy * vy)); 58 this.has = d < this.r + ball.r; 59 return this.has; 60 }; 61