Touch enter cocos

is there a way to handle touch enter with cocos creator 1.9?

this.node.on(cc.Node.EventType.TOUCH_START, () => { ... }, this);

i tried but this event is called only if your finger was not on screen, and i look for an event that is called when my finger was out of a node and then it cames in

It depends how you have your touch events setup. I have my touch event listening on the canvas node and then I use cc.Node.EventType.TOUCH_START to figure out which child node is being touched. If you want to detect touches outside and inside a particular node, I’d use cc.Node.EventType.TOUCH_MOVE, cc.Node.EventType.TOUCH_END and cc.Node.EventType.TOUCH_CANCEL to detect when the touch is outside and inside of a particular node. There may be other ways but this is how I’d do it.

1 Like

But how do you detect which child is being Touched?
could you join me an exemple of printing something like the name on the console when touch enter on a child node of the canvas ?

This is how I detect a back button touch in one of my scenes inside TOUCH_START… Same logic works for cc.Node.EventType.TOUCH_MOVE and cc.Node.EventType.TOUCH_END.

cc.Node.EventType.TOUCH_CANCEL assumes the person’s finger has left the screen.

My example assumes parent is the canvas. If the node is inside a child, you can do this.node.parent.parent. If you have many nodes inside of other nodes, you can declare the canvas inside properties.

As you can see in my example below, I use much of my own calculations but Cocos has many ways to detect touches so my way might not be the best for you depending on how you have your nodes etc setup. I just got used to calculating things on my own… You can use cc.Intersection though…

In my opinion, the canvas node touch is the most dependable.

this.canvas = this.node.parent;
    
var self = this;

    self.canvas.on(cc.Node.EventType.TOUCH_START, function (event) {

        let touches = event.getTouches();

        if (touches.length > 1) { return; }

        let touchLoc = touches[0].getLocation();

        let backLocation = self.backBtn.convertToNodeSpaceAR(touchLoc);
        let backWidth = (self.backBtn.getContentSize().width / 2);
        let backHeight = (self.backBtn.getContentSize().height / 2);

         if (backLocation.x >= -backWidth
             && backLocation.x <= backWidth
             && backLocation.y >= -backHeight
             && backLocation.y <= backHeight) {//hit back btn

             console.log("touched back btn...");

        }

    }, self.node);

Take a look at these example cases because they helped me a lot:

Take a look at:

OnTouchCtrl
OnMultiTouchCtrl