我做了這個小小的腳本,使圖像反彈(這是工作正常)。幾個畫布對象的實例
現在我試圖在畫布上添加更多圖片(該對象的更多實例),但由於某些原因,它仍然只顯示一個。
如果有人可以發現我錯過了什麼,它將不勝感激。
var img = function() {
this.props = {
x: Math.floor(Math.random() * W + 1),
y: 50,
radius: 40,
// Velocity
vy: 1,
// angle
angle: 0,
direction: 1
};
var self = this;
this.draw = function() {
ctx.drawImage(im, self.props.x, self.props.y);
},
this.update = function() {
ctx.clearRect(0, 0, W, H);
self.draw();
self.props.y += self.props.vy;
self.props.vy += gravity;
if(self.props.y + self.props.radius > H) {
self.props.y = H - self.props.radius;
self.props.vy *= -bounceFactor;
self.props.direction *= -1;
}
};
};
// my instances
var el = new img();
var el2 = new img();
var el3 = new img();
function main() {
el.update();
el2.update();
el3.update();
}
setInterval(main, 1000/60);
下面是完整的代碼:FIDDLE
就是這樣!謝謝! – Skwal