How access properties variables outside the start() and onLoad() functions?

Guys Thank you very much for all help here, amazing people everyone!

I having a simple problem, I trying understand how is the correct way to manage the variables inside “properties” I have something like:

   properties: {
        lblStatus : cc.Label,
        // PotionA : cc.Node,
        PotionA : cc.Node ,
    },

I tried this too :

    properties: {

        lblStatus : cc.Label,
        // PotionA : cc.Node,
        PotionA :
        { 
            default: null, type: cc.Node 
        },

    },

When I call this.PotionA in the static start function , everything works fine with:

 start () {
        this.PotionA.on("touchmove" ,  this._Test , this.PotionA);
    },

but when I call the object in the my custom callback :

    _Test : function(events)
    {
        cc.log('working');
        let delta = events.touch.getDelta();
        this.PotionA.y += delta.y;
        this.PotionA.x += delta.x;
    },

Its calling the callback, is printing “working” but got this error "typeError: undefined is not an object (evaluating ‘this.PotionA.x’). "
how I fix this and move the x and y of the node, the variables are linked in cocos creator interface.

It’s because you are passing wrong this reference - it’s a third argument in .on( ... ) method. It should be:

this.PotionA.on("touchmove" ,  this._Test , this);

Then inside of a _Test method this will still mean this, not PotionA. It’s because this in javascript not always is this what would seem logical.

1 Like