Add click event listener to the sprite on Cocos Creator

631 Views Asked by At

I'm trying to call a function on click on sprite (sprite is just a picture). Documentation example doesn't work
This is my code, everything except the click event works correctly

import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('NewComponent')
export class NewComponent extends Component {

    start() {
        let anim = this.getComponent(cc.Animation);
        anim.play("anim_pers");
        console.log('start');

        node.on(cc.Node.EventType.MOUSE_DOWN, function (event) {
            console.log('Mouse down');
        }, this);
    }
    update(deltaTime: number) {}
}
1

There are 1 best solutions below

0
On BEST ANSWER

you have to set the script node as Click Node

[1]: https://i.stack.imgur.com/vntc0.png