-1
(以下のコードは私の現在の新しいバグの作成方法を表しています)新しいバグを作成する方法はありますか?私はこれらの機能のそれぞれに行き、コピーして貼り付ける必要があります。この質問は、時間を節約するだけでなく、コードを少しきれいにすることを試みることです。(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();
};
'bug'の表示メソッドが' background(255、255、255); 'を追加呼び出ししている点を除いて' bug'と 'bug1'の違いはありません。このような場合は、 'bug'の宣言を一つだけ使用し、単に' var bug = new bug(); 'のようにインスタンス化してください。 var bug1 =新しいバグ(); ' – codtex