Hello everyone, I have created several sprite-type nodes after I converted them to prefabs, using a “for” cycle I add them to the scena, I need to know how I can add the listening event to know when one of those elements is pressed and get its position , try this
cc.systemEvent.addListener
event: cc.EventListener.MOUSE
But I am not very sure of how the code should go in the “for” cycle so that each element listens to the event, can you give me an idea of how to do it?
the prefab must have this script
@Andres_IC Good afternoon,
Thanks for the contribution, since the node generates information in the console when clicking, but it does not show the position, it indicates that it is an object,
console.log (this.node.position);
Simulator: JS: [object Object]
If I use this other option, mark error too
console.log (“pos” + this.node.getPosition ());
this.x.toFixed is not a function
That script is associated with the prefab
cc.Class({
extends: cc.Component,
properties: {
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.node.on(cc.Node.EventType.TOUCH_START,this.OnButtonPress,this);
},
start () {
},
// update (dt) {},
OnButtonPress() {
console.log(this.node.position);
console.log("pos " + this.node.getPosition ( ));
}
});
u just tested the same code as works.
can you check the output of
console.log(this);
console.log(this.node)
Good afternoon,
console.log(this.node.position);
console.log(this);
console.log(this.node)
-
Simulator: JS: [object Object]
-
Simulator: JS: [object Object]
-
Simulator: JS: [object Object]
@Andres_IC
Good afternoon, I already found a way to get the information, I did it in the following way:
console.log("pos X: " + this.node.x);
console.log("pos Y: " + this.node.y);
what version are you using?