[英]Using a javascript constructor to re-initialize an existing object
I'm using this function to create a particle for my particle system: 我正在使用此功能为我的粒子系统创建粒子:
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;
}
and I'm using this function to update the particles characteristics during the course of my animation: 我在动画过程中使用此功能来更新粒子特征:
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;
}
}
Is there a way with which I can avoid the redundant code? 有什么办法可以避免冗余代码?
Also I tried using this = new particle()
and it didn't work. 我也尝试使用
this = new particle()
,但是没有用。 I can't think of a reason why it shouldn't work. 我想不出它为什么不起作用的原因。 why doesn't it?
为什么不呢?
And on a totally unrelated note, is Firefox incapable of handling particle animations? 完全不相关的注释是,Firefox无法处理粒子动画吗? Chrome uses 5% of my CPU.
Chrome使用了我5%的CPU。 Firefox uses around 30 and still lags!
Firefox使用大约30种,仍然落后! I have an i5 2500k so that shouldn't be a problem.
我有i5 2500k,所以应该不是问题。 I'm running the latest versions of both.
我正在运行两者的最新版本。
Apply the function, passing the current object as the this
argument: 应用函数,将当前对象作为
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);
}
}
Create a prototype function to initialize the values 创建原型函数以初始化值
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();
}
}
You can make it another function and invoke whenever required. 您可以使其成为另一个函数,并在需要时调用。
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.