(Javascript)如何快速编码多种类型的对象?

时间:2017-02-23 13:34:00

标签: javascript

(下面的代码代表了我目前创建新bug的方法。)是否有更快的方法来创建新的bug?我必须仔细检查这些功能,然后复制并粘贴它们。这个问题不仅是为了节省时间,而且是为了尝试清理代码。

var xBugSpeed = random(1, 3);
var yBugSpeed = 1;
var bugColor = random(15, 100);

var bug = function() {
    this.position = new PVector(random(width), random(height));
    this.velocity = new PVector(xBugSpeed, yBugSpeed);
};
var bug1 = function() {
    this.position = new PVector(random(width), random(height));
    this.velocity = new PVector(xBugSpeed, yBugSpeed);
};

bug.prototype.update = function() {
    this.position.add(this.velocity);
};
bug1.prototype.update = function() {
    this.position.add(this.velocity);
};

bug.prototype.display = function() {

    background(255, 255, 255);
    fill(138, bugColor, bugColor);
    noStroke();
    ellipse(this.position.x, this.position.y, 15, 15);
    ellipse(this.position.x - 20, this.position.y, 30, 15);
    //legbottom
    rect(this.position.x - 14, this.position.y + 4, 5, 10);
    rect(this.position.x - 22, this.position.y + 4, 5, 20);
    rect(this.position.x - 30, this.position.y + 4, 5, 10);
    beginShape(QUADS);
    vertex(this.position.x - 9, this.position.y + 14);
    vertex(this.position.x - 14, this.position.y + 14);
    vertex(this.position.x - 9, this.position.y + 22);
    vertex(this.position.x - 4, this.position.y + 22);
    endShape();
    beginShape(QUADS);
    vertex(this.position.x - 25, this.position.y + 14);
    vertex(this.position.x - 30, this.position.y + 14);
    vertex(this.position.x - 35, this.position.y + 22);
    vertex(this.position.x - 30, this.position.y + 22);
    endShape();
    //legtop
    rect(this.position.x - 14, this.position.y - 14, 5, 10);
    rect(this.position.x - 22, this.position.y - 23, 5, 20);
    rect(this.position.x - 30, this.position.y - 14, 5, 10);
    beginShape(QUADS);
    vertex(this.position.x - 9, this.position.y - 14);
    vertex(this.position.x - 14, this.position.y - 14);
    vertex(this.position.x - 9, this.position.y - 22);
    vertex(this.position.x - 4, this.position.y - 22);
    endShape();
    beginShape(QUADS);
    vertex(this.position.x - 25, this.position.y - 14);
    vertex(this.position.x - 30, this.position.y - 14);
    vertex(this.position.x - 35, this.position.y - 22);
    vertex(this.position.x - 30, this.position.y - 22);
    endShape();
};
bug1.prototype.display = function() {
    fill(138, bugColor, bugColor);
    noStroke();
    ellipse(this.position.x, this.position.y, 15, 15);
    ellipse(this.position.x - 20, this.position.y, 30, 15);
    //legbottom
    rect(this.position.x - 14, this.position.y + 4, 5, 10);
    rect(this.position.x - 22, this.position.y + 4, 5, 20);
    rect(this.position.x - 30, this.position.y + 4, 5, 10);
    beginShape(QUADS);
    vertex(this.position.x - 9, this.position.y + 14);
    vertex(this.position.x - 14, this.position.y + 14);
    vertex(this.position.x - 9, this.position.y + 22);
    vertex(this.position.x - 4, this.position.y + 22);
    endShape();
    beginShape(QUADS);
    vertex(this.position.x - 25, this.position.y + 14);
    vertex(this.position.x - 30, this.position.y + 14);
    vertex(this.position.x - 35, this.position.y + 22);
    vertex(this.position.x - 30, this.position.y + 22);
    endShape();
    //legtop
    rect(this.position.x - 14, this.position.y - 14, 5, 10);
    rect(this.position.x - 22, this.position.y - 23, 5, 20);
    rect(this.position.x - 30, this.position.y - 14, 5, 10);
    beginShape(QUADS);
    vertex(this.position.x - 9, this.position.y - 14);
    vertex(this.position.x - 14, this.position.y - 14);
    vertex(this.position.x - 9, this.position.y - 22);
    vertex(this.position.x - 4, this.position.y - 22);
    endShape();
    beginShape(QUADS);
    vertex(this.position.x - 25, this.position.y - 14);
    vertex(this.position.x - 30, this.position.y - 14);
    vertex(this.position.x - 35, this.position.y - 22);
    vertex(this.position.x - 30, this.position.y - 22);
    endShape();
};

bug.prototype.checkEdges = function() {

  if (this.position.x > width) {
    this.position.x = 0;
  } 
  else if (this.position.x < 0) {
    this.position.x = width;
  }

  if (this.position.y > height) {
    this.position.y = 0;
  } 
  else if (this.position.y < 0) {
    this.position.y = height;
  }
};
bug1.prototype.checkEdges = function() {

  if (this.position.x > width) {
    this.position.x = 0;
  } 
  else if (this.position.x < 0) {
    this.position.x = width;
  }

  if (this.position.y > height) {
    this.position.y = 0;
  } 
  else if (this.position.y < 0) {
    this.position.y = height;
  }
};

var bug = new bug();
var bug1 = new bug1();

draw = function() {

    bug.update();
    bug.checkEdges();
    bug.display();
    bug1.update();
    bug1.checkEdges();
    bug1.display();

};

1 个答案:

答案 0 :(得分:0)

想要快速创建多个对象时的一个好习惯是设置工厂。

然后,您将拥有一个函数,您可以从while()调用将实例放入集合中,以便稍后访问它们。

我现在没有时间写一些代码,我希望我帮了一下!