How can I correct this error in cocos2d-x

44 views Asked by At

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.

2

There are 2 answers

0
Albers Joy On BEST ANSWER

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.

0
user23555517 On

I think that error occurs because that script is called several times. You mentioned that you link this script links into Dashboard scene. Don't you call this schedule function several times in scene? please use unschedule() function in your code.