Search code examples
javascriptcocos2d-js

How can I correct this error in cocos2d-x


I just made simple Cocos2d-x project. It has serveral scenes and everyone has it's own 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);
    };
});

here is console logs:

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

But Label's text is like 10 after 1s 8 and after 1s 6 ... I am looking for if this dashboard.js is called twice in dashboard.scene but nothing!

Sometimes it works corretly but sometimes it works wrong.... Why?

I solve this problom and want to show correct remain time.


Solution

  • That error occurs because over-loading script. I mean dashboard.js linked to the dashboard scene. so this script is loaded one time when dashboard scene loaded and script doesn't destoryed even dashboard scene exited until refresh the browser.

    So function schedule is running in RAM storage. To solve this error, I link all script with the first scene, not self scene.