Access properties of a scene

Hey everybody
I am new to cocos creator and I have this problem for a few days and its driving me crazy.

I have attached a script called ‘game’ to my canvas, in my game there should be a slider, so I created a slider too and created a script for it called ‘forceSlider’. Now my slider works just fine, my problem is that I need to know the value of ‘slider.progress’ in my ‘game’ script, so I can handle the game.
So my problem is: “How can I access the value of the slider in my ‘game.js’ script?” is there a way that I can pass variables and values between these two js files?
BTW, I learned how to work with sliders here.

Hi @heretic133

Please look at below:

http://cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/modular-script/index.html

God Bless…

Sincerely,

Sunday

1 Like

Thank you very much for your answer, It was really helpful.

Because I was not aware of modular script, I used the trick that is used in this tutorial, that is giving an instance of my game.js to the class that I have defined somewhere else. But I don’t know if that’s efficient or not.

Details: I have a js file called slider.js in this file there is the script for a slider that I need to use in my game:

cc.Class({
    extends: cc.Component,

    properties: {
       slider: cc.Slider,
    },
    
    onLoad: function () {
       this.slider.node.on('slide', this.callback, this);
    },
    
    callback: function (event) {
        var slider = event.detail;
        this.game.sliderState = slider.progress;
    }
});

And in my game.js I have: this.slider.getComponent('slider').game = this;
So everytime that the slider’s progress is changed, I update the sliderState in my game.js via the game object that I passed to my slider.js. Now is that efficient? Will it cause some kind of overload or something?