2017-02-23 33 views
-1

(下面的代碼代表了我目前創建新bug的方法。)有沒有更快的方法來創建新的錯誤?我必須檢查這些功能,並複製並粘貼它們。這個問題不僅是爲了節省時間,而且是爲了試圖清理代碼。(Javascript)如何快速編碼多種類型的對象?

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

}; 
+0

除了'bug'的顯示方法正在對'background(255,255,255)'進行額外調用外,''bug''和'bug1'之間沒有任何區別,''這看起來像一個複製粘貼代碼如果是這樣,你應該只使用'bug'的一個聲明,並簡單地將它的實例作爲'var bug = new bug(); var bug1 =新bug();' – codtex

回答

0

當您想快速創建多個對象時,一個很好的做法是設置一個Factory。

然後,您將有一個函數,您可以從while()中調用該函數,將您的實例放入一個集合中,以便稍後可以訪問它們。

我沒有時間寫一些代碼,我希望我幫了一下!