CreateJS / Adobe Animate CC:测试一个简单的函数 [英] CreateJS / Adobe Animate CC: testing a simple function
问题描述
我正在尝试测试运行在第一帧中声明的简单函数。当我在第50帧
上写 fl_DoRestart();
时,我收到以下错误:未捕获的ReferenceError :fl_DoRestart未定义
,但它在第一帧定义。为什么这不起作用?这在actionscript中过去非常简单:(
I'm trying to test run a simple function that's declared in the first frame. When I write fl_DoRestart();
on frame 50
I get the following error: Uncaught ReferenceError: fl_DoRestart is not defined
, but it's defined on frame one. Why's this not working? This used to be very simple in actionscript :(
我最终需要能够从另一个函数调用此函数,现在我只是测试它。
I eventually need to be able to call this function from another function, right now I'm just testing it.
这是我在第一帧的功能:
Here's my function on frame one:
function fl_DoRestart(){
this.gotoAndPlay(1);
console.log("play From Start");
}
推荐答案
该函数已在该框架上定义,但它没有任何引用。您只能从该框架脚本调用它(和此
可能是窗口
而不是该脚本)
The function has been defined on that frame, but it has no reference to anything. You can call it from that frame script only (and this
may be window
instead of that script)
使用Animate导出,建议在这个
上存储方法调用,这样就可以访问它们。
With Animate export, it is recommended to store method calls on this
, so they can be accessed.
this.fl_DoRestart(){
this.gotoAndPlay(1);
console.log("play From Start");
}
this.fl_DoRestart();
// From the root
exportRoot.someMoveClip.fl_DoRestart();
// Using a callback
btn.addEventListener("click", someMovieClip.fl_DoRestart.bind(this));
// Shortcut with "on()"
btn.on("click", someMovieClip.fl_DoRestart, this);
希望能够说明这是如何运作的。另外需要考虑的是Animate CC导出中的帧脚本将在每次访问该帧时运行,因此您可能需要在运行脚本之前检查是否已定义事物。
Hope that sheds some light on how this works. One additional thing to consider is that frame scripts in Animate CC export will run each time that frame is accessed, so you may want to check if things are defined before running scripts.
if (this.fl_DoRestart == null) {
// Then define stuff here
}
干杯。
这篇关于CreateJS / Adobe Animate CC:测试一个简单的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!