简体   繁体   English

使用 JavaScript 在 Adob​​e Animate 中意外重新分配变量

[英]Unexpected variable reassignment in Adobe Animate using JavaScript

I have very recently started playing around with Adobe Animate + JavaScript.我最近开始玩 Adob​​e Animate + JavaScript。 I am trying to increment a variable every tick.我试图在每个滴答声中增加一个变量。 When in Adobe Animate project, at some point I encountered unexpected behaviour when reassigning variables inside an event loop:在 Adob​​e Animate 项目中,在某个时候我在事件循环内重新分配变量时遇到了意外行为:

myVar = 0;

createjs.Ticker.addEventListener('tick', mainLoop.bind(this));

function mainLoop() {
    myVar += 1;
    this.mytext.text = myVar; // mytext is a text box pre-drawn on canvas
}

This produces unexpected incrementation, seen in the text box:这会产生意外的增量,在文本框中看到:

在此处输入图片说明

If myVar is declared using let or var , the resulting value flops between 1 and 2如果myVar使用letvar声明,则结果值在 1 和 2 之间波动

在此处输入图片说明

After creating a new document, the issue was gone.创建新文档后,问题消失了。 Similar code also works as expected in jsfiddle: https://jsfiddle.net/cdf5utmx/2/ .类似的代码也可以在 jsfiddle 中按预期工作: https ://jsfiddle.net/cdf5utmx/2/。

I am unsure how the document became "messed up", as the only setting outside the code that was changed meanwhile was the frame rate setting.我不确定文档是如何变得“混乱”的,因为同时更改的代码之外的唯一设置是帧速率设置。

I would like to know if anyone had ever encountered a similar issue, because creating a new document every time would be impractical.我想知道是否有人遇到过类似的问题,因为每次都创建一个新文档是不切实际的。

I am using Adobe Animate 21.0 on Windows 10, Chrome 91.0 browser.我在 Windows 10、Chrome 91.0 浏览器上使用 Adob​​e Animate 21.0。

I am used to working with Python and new to JavaScript, meaning it could be that I don't understand something fundamental about variable assignation and scope in JavaScript.我习惯于使用 Python 和 JavaScript 新手,这意味着我可能不了解 JavaScript 中变量分配和作用域的一些基本知识。 This could also very likely be an Adobe Animate-specific issue这也很可能是 Adob​​e Animate 特定的问题

ANSWERED: The issue was related to code running in multiple frames instead of one, as Lanny pointed out回答:正如Lanny指出的那样,该问题与在多个帧而不是一个帧中运行的代码有关

Without seeing more code, I am not sure, but it looks like you might be running your code multiple times.没有看到更多代码,我不确定,但看起来您可能会多次运行您的代码。 If you have the code block above in a frame script in Aniamte, then it could be executed each time the frame runs.如果您在 Anime 的框架脚本中有上面的代码块,那么它可以在每次框架运行时执行。 Throw a console log in there to confirm it.在那里抛出一个控制台登录来确认它。 Adobe Animate will loop the frame timelines by default if there is more than one frame.如果有多于一帧,Adobe Animate 将默认循环帧时间线。

A typical approach is to move that code outside Animate and into the main HTML or JavaScript file where it can run one time.一种典型的方法是将该代码移到 Animate 之外,并移到它可以运行一次的主 HTML 或 JavaScript 文件中。 Alternately, you can put the code in a block to make sure it only runs one time:或者,您可以将代码放在一个块中以确保它只运行一次:

var runOnce = false;

if (!runOnce) {
    runOnce = true;
    var myVar = 0;
    
    createjs.Ticker.on('tick', mainLoop, this);
    
    function mainLoop() {
        myVar += 1;
        this.mytext.text = myVar; // mytext is a text box pre-drawn on canvas
    }

}

I also showed the usage of on() , which provides a scope parameter ( docs )我还展示了on()的用法,它提供了一个范围参数( docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM