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
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 it is called when you do a new of your layer called “Layer”. I dont know if I explain myself.
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.
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();