我有一个html5 Canvas动画,我正在Adobe Animate上做,并调整了一些代码。
我在动画上有一部分就像一个组合框,其中包含导航不同帧的所有链接。问题是,我不想为许多按钮创建一堆EventListener,因为根据经验,我知道它不能很好地工作。所以我想要一个更有创意的解决方案。这是我的想法。
这是我到目前为止所做的,(不多)
var combobox = [this.btncasco , this.btnbanyera , this.btnLumbrera , this.btnproapopa, this.btnestriborbabor ];
for (var i=0; i<combobox.length; i++) {
var clipcasco = gotoAndStop(0);
var clipbanyera = gotoAndStop(2);
var cliplumbera = gotoAndStop(4);
var clipproapoa = gotoAndStop(6);
var clipestriborbabor = gotoAndStop(8);
}
那可行吗?
在您的示例中,您只是分配gotoAndStop的结果(没有范围,因此您可能在控制台中收到错误)
我想你正在寻找这样的东西:
for (var i=0; i<combobox.length; i++) {
// This is kind of complex, but if you just reference "i" in your callback
// It will always be combobox.length, since it references the value of i
// after the for loop completes variable.
// So just store a new reference on your button to make it easy
combobox[i].index = i*2; // x2 lines up with your code 0,2,4,etc.
// Add a listener to the button
combobox[i].on("click", function(event) {
// Use event.target instead of combobox[i] for the same reason as above.
event.target.gotoAndStop(event.target.index);
}
}
您可能遇到与未定义按钮的其他StackOverflow帖子相同的问题(请检查控制台)。实际上Animate导出中存在一个错误,其中剪辑的子项不能立即可用。要解决这个问题,你可以在开始时调用this.gotoAndStop(0);
来强制它更新孩子们。