0
我在循环中创建了几个动态图像并将它们保存在一个数组中。现在我想为每个图像创建一个onclick函数,将其删除。 的代码是这样的:删除循环中的动态图像
var arr = [];
for (var i = 0; i < 30; i++) {
var osix = new Kinetic.Image({
image: images.Osi1,
x: Math.floor(Math.random()*900+0),
y: Math.floor(Math.random()*600+0)
});
arr.push(osix); //works fine
}
for (image in arr) {
arr[image].on('click', function() {
writeMessage(score_counter); //also works fine
arr[image].remove(); //no error, but the removal is not appearing
layer.draw();
score_counter += 1;
if (score_counter >= 20) {finishing_fun();}
});
}
而如果我做这样的工作原理:
var osi1 = new Kinetic.Image({
image: images.Osi1,
x: Math.floor(Math.random()*900+0),
y: Math.floor(Math.random()*600+0)
});
osi1.on('click', function() {
writeMessage('50!!');
osi1.remove();
layer.draw();
score_counter += 1;
if (score_counter >= 20) {finishing_fun();};
}
非常感谢!我的一天得救了 – user1734984