TouchSpecialEffComp.ts 1.37 KB
import { ResUtils } from "simba-cc-resutils";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu("自定义UI组件/TouchSpecialEffComp")
export default class TouchSpecialEffComp extends cc.Component {

    onLoad() {

    }

    onEnable() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.callback, this, false);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.callback, this, false);
        this.node.on(cc.Node.EventType.TOUCH_END, this.callback, this, false);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.callback, this, false);
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_END, this.callback, this);
    }

    onDisable() {
        this.node.off(cc.Node.EventType.TOUCH_END, this.callback, this);
    }

    async callback(event) {
        if (event.type == cc.Node.EventType.TOUCH_END) {
            let spNode = new cc.Node();
            spNode.name = "touchSpNode";
            let sp = spNode.addComponent(cc.Sprite);
            sp.spriteFrame = await ResUtils.loadRes("textures/common/gift_AD", cc.SpriteFrame);
            spNode.parent = this.node;
            spNode.setPosition(this.node.convertToNodeSpaceAR(event.getLocation()));
            this.scheduleOnce(() => {
                if (cc.isValid(spNode)) {
                    spNode.destroy();
                }
            }, 0.5);
        }

    }
}