簡體   English   中英

使用javascript構造函數重新初始化現有對象

[英]Using a javascript constructor to re-initialize an existing object

我正在使用此功能為我的粒子系統創建粒子:

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,所以應該不是問題。 我正在運行兩者的最新版本。

應用函數,將當前對象作為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);
    }
}

創建原型函數以初始化值

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();  
    }
}

您可以使其成為另一個函數,並在需要時調用。

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();
    }
}
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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM