I want to get the location of a touch, here is the code:
onLoad: function () {
this.node.on("touchstart", function( event) {
console.log(event.getLocationY);
});
},
It logs out this:
function () {
return this.touch ? this.touch.getLocation() : cc.v2();
}
Any ideas?
PS: same thing with “mousedown”
Hi, here are the working codes for you.
onLoad: function () {
var self = this;
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan: function(touch, event) {
// x = touch.getLocation().x
// y = touch.getLocation().y
return true
},
onTouchMoved: function(touch, event) {
// x = touch.getLocation().x
// y = touch.getLocation().y
return true
},
onTouchEnded: function(touch, event) {
// x = touch.getLocation().x
// y = touch.getLocation().y
return true
}
}, self.node);
},
for more information:
http://cocos2d-x.org/docs/api-ref/creator/v1.0/classes/Touch.html
Thanks a lot for the answer, you seem to be already familiar with Cocos2d-JS because that code is not in the Creator docs:
Here
No I am not familiar with Cocos-2d js or c++.
I saw some codes an example at github/cocos-creator and use it.
Perhaps I should start doing that, as relying solely on the Docs, which still need a lot of work, will get me nowhere.
Hi, I just read document and dont know how to get touch location in current node, is there any thing like : applyTransform for x, y?
I think what you need is getStartLocation()
touchStart: function (event) {
cc.log(‘touch start location:’,event.getStartLocation());
},
http://cocos2d-x.org/docs/api-ref/creator/v1.4/classes/Event.EventTouch.html#method_getStartLocation
I believe this might be useful:
http://cocos2d-x.org/docs/api-ref/creator/v1.4/classes/Node.html#method_getWorldToNodeTransform