我有一个在图层中创建对象数组的代码,如下所示:
var labels = layer.get('Label');
var labelCount = labelLeft.length;
var tweens = [];
var tweenCounter = 1;
var duration=5;
for(var i=0; i<labelCount; i++)
{
var tween = new Kinetic.Tween({
node: labelLeft[i],
duration: animspeed[i],
x: 0,
onFinish: function() {
if (tweenCounter !== labelCount) { //Prevent an undefined tween from being played at the end
tweens[tweenCounter].play();
tweenCounter++;
}
}
});
tweens.push(tween);
}
tweens[0].play();
问题是我想在使用onFinish滚动到左边时隐藏对象。我尝试使用labelLeft [i] .hide()
onFinish: function() {
labelLeft[i].hide();
if (tweenCounter !== labelCount) { //Prevent an undefined tween from being played at the end
tweens[tweenCounter].play();
tweenCounter++;
}
}
但这会触发TypeError:labelLeft [i]未定义 有任何想法吗?请帮忙。感谢
答案 0 :(得分:0)
尝试调试代码并检查i
是否已定义
我认为这不是因为你在for循环中并在循环结束时执行代码。您可以使用匿名函数轻松解决此问题,从而形成范围。
for(var i=0; i<labelCount; i++)
{
(function(i){
//put code here
}(i))
}
答案 1 :(得分:0)
似乎是关闭问题。你可以尝试一下吗,我不确定它是否会起作用但无论如何:
for(var i=0; i<labelCount; i++)
{
var label = labelLeft[i];
var tween = new Kinetic.Tween({
node: labelLeft[i],
duration: animspeed[i],
x: 0,
onFinish: function(l) {
return function()
hide(l);
}(label)
});
tweens.push(tween);
}
function hide(label) {
labelLeft[label].hide();
if (tweenCounter !== labelCount) { //Prevent an undefined tween from being played at the end
tweens[tweenCounter].play();
tweenCounter++;
}
}