CreateJS / Adobe Animate CC: testing a simple function

做~自己de王妃 提交于 2019-12-23 06:05:28

问题


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");
    }

回答1:


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)

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);

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
}

Cheers.



来源:https://stackoverflow.com/questions/39257022/createjs-adobe-animate-cc-testing-a-simple-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!