I'm trying to create a random amount of squares on canvas each time I press the button and then I want them to move up. Right now it completely ignores the for loop and only generates one square instead of random amount. How to solve this?
// Spawn
var numberArray = [0, 60, 120, 180, 240, 300, 360, 420];
var posX = numberArray[Math.floor(Math.random() * numberArray.length)];
var posY = 240;
function spawnRandomObject() {
// Game Object
ctx.fillStyle = '#f2a365';
ctx.globalCompositeOperation = "destination-over";
ctx.beginPath();
ctx.fillRect(posX, posY, 60, 60);
ctx.stroke();
}
// Blocks moving up
document.getElementById("button").addEventListener("click", function(){
// Spawn random amount of objects
for (var i=0; i<Math.floor((Math.random()*8)+1); i++){
spawnRandomObject(i);
}
posY -=60;
});
Aucun commentaire:
Enregistrer un commentaire