2

我正在使用这个函数为我的粒子系统创建一个粒子:

function particle()
{
    this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
    this.location = {x: 50, y: 150};
    this.radius = 5+Math.random()*8;
    this.life = 4+Math.random()*8;
    this.remaining_life = this.life;
    this.r = 255;
    this.g = 140;
    this.b = 30;
}

我正在使用此功能在动画过程中更新粒子特征:

particle.prototype.updateparticle = function()
{
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0)
    {
        this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
        this.location = {x: 50, y: 150};
        this.radius = 5+Math.random()*8;
        this.life = 4+Math.random()*8;
        this.remaining_life = this.life;
        this.r = 255;
        this.g = 140;
        this.b = 30;    
    }
}

有没有办法可以避免冗余代码?

我也试过使用this = new particle(),但没有用。我想不出它不应该工作的原因。为什么不呢?

在完全不相关的情况下,Firefox 无法处理粒子动画吗?Chrome 使用了我 5% 的 CPU。Firefox 使用了大约 30 个,但仍然滞后!我有一个i5 2500k,所以这应该不是问题。我正在运行两者的最新版本。

4

4 回答 4

2

应用函数,将当前对象作为this参数传递:

particle.prototype.updateparticle = function()
{
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0)
    {
        particle.apply(this);
    }
}
于 2013-04-21T04:59:38.977 回答
2

创建一个原型函数来初始化值

function particle() {
    this.init();
}

particle.prototype.init = function(){
    this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
    this.location = {x: 50, y: 150};
    this.radius = 5+Math.random()*8;
    this.life = 4+Math.random()*8;
    this.remaining_life = this.life;
    this.r = 255;
    this.g = 140;
    this.b = 30;
}

particle.prototype.updateparticle = function() {
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0) {
        this.init();  
    }
}
于 2013-04-21T04:59:44.927 回答
2

您可以将其设为另一个函数并在需要时调用。

function particle()
{
    this.initialize();
}

particle.prototype.initialize = function(){
    this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
    this.location = {x: 50, y: 150};
    this.radius = 5+Math.random()*8;
    this.life = 4+Math.random()*8;
    this.remaining_life = this.life;
    this.r = 255;
    this.g = 140;
    this.b = 30;    
}

particle.prototype.updateparticle = function()
{
    this.remaining_life--;
    this.radius--;
    this.location.x += this.speed.x;
    this.location.y += this.speed.y;

    if(this.remaining_life < 0 || this.radius < 0)
    {
        this.initialize();
    }
}
于 2013-04-21T04:59:46.957 回答
0
function particle(x, y, r, g, b) {
  this.location = {x: x, y: y};
  this.r = r;
  this.g = g;
  this.b = b;

  // Group everything that randomises in update()
  this.update = function() {
    this.speed = {x: -1.5+Math.random()*3, y: -12+Math.random()*12};
    this.radius = 5+Math.random()*8;
    this.life = 4+Math.random()*8;
    this.remaining_life = this.life;
  }

  this.update();
}


particle.prototype.updateparticle = function() {
  this.remaining_life--;
  this.radius--;
  this.location.x += this.speed.x;
  this.location.y += this.speed.y;

  if(this.remaining_life < 0 || this.radius < 0) {
    this.update();
  }
}

//Instantiation
var p1 = new particle(50, 150, 255, 140, 30);
于 2013-04-21T05:10:27.420 回答