javascript - 球弹跳 - javascript

标签 javascript jquery

function randomXToY(minVal,maxVal,floatVal)
  {
   var randVal = minVal+(Math.random()*(maxVal-minVal));
   return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
  }

  Ball = (function() {

   // constructor
 function Ball(x,y,radius,color){
  this.center = {x:x, y:y};  
  this.radius = radius;               
  this.color = color;
  this.dx = 2;               
  this.dy = 2;        
  this.boundaryHeight = $('#ground').height();
  this.boundaryWidth = $('#ground').width();

  this.dom  = $('<p class="circle"></p>').appendTo('#ground');

  // the rectange div a circle
  this.dom.width(radius*2);
  this.dom.height(radius*2);
  this.dom.css({'border-radius':radius,background:color});

  this.placeAtCenter(x,y);         
}

// Place the ball at center x, y
Ball.prototype.placeAtCenter = function(x,y){
  this.dom.css({top: Math.round(y- this.radius), left: Math.round(x - this.radius)});
  this.center.x = Math.round(x);        
  this.center.y = Math.round(y);             
};

Ball.prototype.setColor = function(color) {
  if(color) {
    this.dom.css('background',color);
  } else {
   this.dom.css('background',this.color);
 }           
};

// move and bounce the ball
 Ball.prototype.move = function(){
  var diameter = this.radius * 2;                                               
  var radius = this.radius;  
  if (this.center.x - radius < 0 || this.center.x + radius > this.boundaryWidth ) {
   this.dx = -this.dx;
 }
  if (this.center.y - radius < 0 || this.center.y  + radius > this.boundaryHeight ) {
    this.dy = -this.dy;
  }
  this.placeAtCenter(this.center.x + this.dx ,this.center.y +this.dy);

};

return Ball;
})();

var number_of_balls = 5;
var  balls = [];   

$('document').ready(function(){
  for (i = 0; i < number_of_balls; i++) { 
  var boundaryHeight = $('#ground').height();
  var boundaryWidth = $('#ground').width();
  var y = randomXToY(30,boundaryHeight - 50);
  var x = randomXToY(30,boundaryWidth - 50);
  var radius = randomXToY(15,30);
   balls.push(new Ball(x,y,radius,     '#'+Math.floor(Math.random()*16777215).toString(16))); 
 }
 loop(); 
 });

loop = function(){
 for (var i = 0; i < balls.length; i++){
  balls[i].move();
}

   setTimeout(loop, 8);    
};

我从未在 javascript 中使用过 oops 概念。当球相互接触时如何改变球的颜色?

这是链接:http://jsbin.com/imofat/1/edit

最佳答案

您目前没有与球进行任何互动。您可以做的是检查两个球是否在彼此“内部”,并在这种情况下改变颜色:http://jsbin.com/imofat/1491/ .

// calculates distance between two balls
var d = function(a, b) {
  var dx = b.center.x - a.center.x;
  var dy = b.center.y - a.center.y;
  return Math.sqrt(dx*dx + dy*dy);
};

和:

// for each ball
for(var i = 0; i < balls.length; i++) {
  // check against rest of balls
  for(var j = i + 1; j < balls.length; j++) {
    var a = balls[i];
    var b = balls[j];
    // distance is smaller than their radii, so they are inside each other
    if(d(a, b) < a.radius + b.radius) {
      // set to some other color using your random color code
      a.setColor('#'+Math.floor(Math.random()*16777215).toString(16));
      b.setColor('#'+Math.floor(Math.random()*16777215).toString(16));
    }
  }
}

仍然有一些需要改进的地方:

  • 只要球在彼此内部,它们就会改变颜色,而不仅仅是一次。
  • 如果您希望它们“触摸”,您可能需要实现某种弹跳效果以使其更加逼真。

关于javascript - 球弹跳 - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12322888/

相关文章:

javascript - 如何自定义文件上传对象并在框中包含特殊文本?

php - 如何使用jquery ajax php上传文件

javascript - 如何使用javascript检查视口(viewport)是否小于特定宽度?

jquery - 无法隐藏选择器,jQuery Uniform 插件

jquery - 将 'touchstart' 绑定(bind)到文档时的 Mobile Safari (iPad) 滚动性能

javascript - Jquery/Javascript $.get 的变量范围问题

javascript - 如何观察整个 session 的游戏统计数据?

javascript - 如何动态更新 css 类 onclick 下拉 jquery

javascript - 在 Node.js 中设置模型的正确方法

jquery - 如何使用 jQuery 动态访问 JSON 键