我正在使用for循环在画布上生成5个圆形,我想给它们加一个类,以便我可以用jquery控制它们,但是我做错了什么。你们可以看一下问题出在哪里吗?
var stage;
var quantity = 6,
width = 60,
height = 60,
circles = [];
function init(){
stage = new createjs.Stage("myCanvas");
stage.width = 500;
stage.height = 600;
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", onTick);
setupGame();
}
function setupGame() {
for(var i = 0; i < quantity; i++) {
var circle = document.createElement("img");
circle.setAttribute('src', 'images/circles/circle'+i+'.png');
circle.className = "circle";
circle.style.position = "absolute";
circle.style.left = Math.floor((Math.random() * 100)) + "%";
circle.style.top = Math.floor((Math.random() * 100)) + "%";
circle.style.width = width + "px";
circle.style.height = height + "px";
document.body.appendChild(circle);
circles.push(circle);
}
}
function onTick(e){
stage.update(e);
}
新版本。在JonnyD的帮助下,我现在有了一个可用的循环。唯一的问题是图像被添加到了页面主体,而不是我的舞台上。我尝试了stage.appendChild(circle),但它不起作用。
这里有一个在线资源链接,供大家查看 = 链接