如何纠正cocos2d-x中的这个错误

问题描述 投票:0回答:2

我刚刚做了一个简单的Cocos2d-x项目。 它有多个场景,每个人都有自己的 JavaScript。

dashboard.js

const MyClass = cc.Class.extend({

    ...

    update: function() {
        this.remainTimes --;
    },

    // Call the schedule method in your class constructor or init method
    ctor: function() {
        // Schedule the update function to be called every frame
        this.schedule(this.update().bind(this), 1);
        label.setFontFillColor(this.remainTimes);
        cc.log('remainTimes : ', this.remainTimes);
    };
});

这是控制台日志:

remainTimes : 10
remainTimes : 9
remainTimes : 8
remainTimes : 7
...

但是Label的文字就像1s后

10
和1s后
8
一样
6
... 我正在寻找这个 dashboard.js 是否在 dashboard.scene 中被调用两次,但什么也没有!

有时它工作正常,但有时工作错误...... 为什么?

我解决了这个问题并想显示正确的剩余时间。

javascript cocos2d-js
2个回答
1
投票

我认为发生错误是因为该脚本被调用了多次。 您提到将此脚本链接链接到仪表板场景。 你不是在场景中多次调用这个调度函数吗? 请在代码中使用

unschedule()
函数。


0
投票

该错误是由于脚本超载而发生的。 我的意思是

dashboard.js
链接到仪表板场景。 因此,当仪表板场景加载时,此脚本会加载一次,并且脚本不会被破坏,即使仪表板场景退出,直到刷新浏览器。

因此函数

schedule
正在 RAM 存储中运行。 为了解决这个错误,我将所有脚本链接到第一个场景,而不是自身场景。

© www.soinside.com 2019 - 2024. All rights reserved.