Accessibility of variables problem! Cocos2d- JS

Hi!
I am fairly new to gamedevelopment, as well as javascript, and I need some help!

Within my layer, I have the touch listeners set up.
However, how would I access the layer’s variables in the onTouchBegan function.
If I use this.LayerVariable to call upon a variable, it does not work, as it is undefined in the eventlistener.

I tried Layer.LayerVariable, and things like Layer.ctor.LayerVariable, but neither works.
I am kind of lost at the moment.

Any help would be appreciated!

if (cc.sys.capabilities.hasOwnProperty('touches')){
            cc.eventManager.addListener({
                event: cc.EventListener.TOUCH_ONE_BY_ONE,

                onTouchBegan:function(touch,event){
                    cc.log("touch detected");
                    //do something here with the layer's variables
                    //this.LayerVariable.runAction(LayerAction);  <= doesn't work
                    //note that the actions and variables were defined in the ctor thing.
                    return true;
                },

            },this);
        }//if touch

Hi again! xd

It’s a thing with js the listener are kinda out of the class so to access:

var target = event.getCurrentTarget(); // here you have the instance of your class
target.LayerVariable…

Layer.LayerVariable has no sense because “Layer” is the definition of the class and not an instance so it does not have the values created in runtime (like your variable).
And the ctor is a function :smile: it is called when you do a new of your layer called “Layer”. I dont know if I explain myself.

Hi @alexgg again haha,

so if I had a variable or a method in the layer, I would have to set them to new variables in the eventListener?
Such as:

var eventListenerMethod = event.getMethod(); //to get a method from the layer
or
var eventListenerVariable = event.getVariable(); //to get a variable from the layer

I understand the layer.layerVariable and the ctor explanation. Just a little confused as to how I would access the variables/method in the layer.

no u don’t…

here is an example…

var MyLayer = cc.Layer.extend({
      _var1: 12,
      _var2:  55,

     ctor:function() {
    this._super();

    if (cc.sys.capabilities.hasOwnProperty('touches')) {
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,

            onTouchBegan: function (touch, event) {
                cc.log("touch detected");
                var myLayer = event.getCurrentTarget();
                
                //This will print 12 to console
                cc.log(myLayer._var1);

                return true;
            }
        }, this);
    }
}         
     }
});

this been said…
just imagine the variable ‘myLayer’ in the function as ‘this’, as u wud normally use it outside…

@tipsycoder

Ahh I understand now!
Thank you so much!

@tipsycoder

One more question. How would I go about accessing the event through global functions?
event.getCurrentTarget doesn’t seem to work.

Why do you need it out of the touchbegan?
If you want to use the instance of the layer in another function in the layer simply use this.

@alexgg

I currently have a global function to level up. But when I level up, I would like to flash an message on the screen, like “perfect!” or “great!”.

However I need a way to access the scene through my global function to show an image.

cc.director.getRunningScene() gives you the current scene

@alexgg

Ahhh, thank you!

@alexgg for some reason, when I do something like:

var myLayer = cc.director.getRunningScene();
myLayer.ALayerFunction();
or
myLayer.ALayerVariable.runAction(Action);

the console says that myLayer.ALayerFunction or myLayer.ALayerVariable is not defined.

Is there a step that I am missing? Or a particular mistake that you may see?

Thanks for the help!

I need the code to really see what’s happening but take into account that the current scene is different to your layer I am almost sure.
You have your scene and your layer attached to it.
In your scene you should have something like this:

onEnter: function() {
var myLayer = new cc.Layer //etc
this._myLayer = myLayer; //here you assign the layer to the scene
}

then everywhere else

var scene = cc.director.getRunningScene();
scene._myLayer.ALayerFunction();

@alexgg

Thanks for the clarification! I didn’t realize that the scene and the layer are actually different things. Sorry for my noobiness.

Thanks again