Below i get touched location and move the node to that position which in theory works but it doesn’t (the coordinates become that location but it’s not at that position i don’t know how to explain so i uploaded a video sorry i was too lazy to record on pc)
onLoad: function () {
this.node.parent.on('touchstart', ( event ) => {
console.log('Tourched!');
var xpos = event.getLocationX();
var ypos = event.getLocationY();
var aksi = cc.moveTo(0.2,xpos,ypos);
this.node.runAction(aksi);
console.log(xpos +" VE "+ ypos);
console.log(this.node.getPositionX() +" VE "+ this.node.getPositionY());
/*var aksi1 = cc.moveBy(0.2, cc.p(0,-100));
this.runAction(aksi1);*/
});
onLoad: function () {
this.node.parent.on('touchstart', ( event ) => {
console.log('Tourched!');
var point = this.node.convertToNodeSpace(event.getLocationInView());
var xpos = point.x;
var ypos = point.y;
var aksi = cc.moveTo(0.2,xpos,ypos);
this.node.runAction(aksi);
console.log(xpos +" VE "+ ypos);
console.log(this.node.getPositionX() +" VE "+ this.node.getPositionY());
});
Beware of the this keyword.
Also, depending on who is the ‘this’ you migth need to change it to this.node.parent.convert… instead of the this.node.convert…
OK, I have got it. Now it works and I have tested it:
// Ballon.js
cc.Class({
extends: cc.Component,
properties: {
},
myClick(evento) {
let pos = this.node.parent.convertToNodeSpaceAR(evento.getLocation());
// You can also use this.node.parente.convertTouchToNodeSpaceAR(evento);
this.node.setPosition(pos);
},
onLoad() {
// The last parameter is the object that should call myClick function
this.node.parent.on(cc.Node.EventType.TOUCH_START, this.myClick, this);
// It is the bind for the this keyword in this.myClick
}
});
It is important to make the tranformation Anchor Relative (AR), because the origin (anchor) of the canvas is (0.5, 0.5) and not the (0, 0).
It was a mistake of mine to use the getLocationInView(), the View coordinate has the origin in the upper left corner and the y-axis inverted, it should be just getLocation().
So what is happening? We have the touch position in global coordinates, and we have to convert the global coordinate to the local coordinate that the ballon is in, that is his parent’s coordinate system.
For example, if you click the lower left coner, that is the (0, 0) global coordinate, we have to transform it to the (-width/2, -height/2) coordinate of the canvas.
stupid me it worked when the anchor was 0,5 and 0,5, but when one of them is 6 it doesnt it always goes a little below the position so how do i fix this or do i have to keep the anchor 0,5