typescriptcocoscreator

Add click event listener to the sprite on Cocos Creator


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) {}
}

Solution

  • you have to set the script node as Click Node

    [1]: https://i.sstatic.net/vntc0.png