【cocos creator】点击屏幕粒子效果,不妨碍按钮点击[2022.07更新]

在这里插入图片描述


const { ccclass, property } = cc._decorator;
/**
 * 拖拽类,挂在要拖拽的节点上
 */
@ccclass
export default class DragBasics extends cc.Component {
    @property(cc.Prefab)
    move_particle: cc.Prefab = null;
    @property(cc.Prefab)
    touch_particle: cc.Prefab = null;
    slideParticle: cc.ParticleSystem = null;
    private _isTouchMove: boolean;
    clickParticle: cc.ParticleSystem = null;
    private _isLoaderParticle: boolean = false;

    onLoad() {
        this.node.setSiblingIndex(this.node.parent.childrenCount - 1);
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        //@ts-ignore
        this.node._touchListener.setSwallowTouches(false);//取消触摸节点的穿透事件

        //加载滑动的粒子
        // cc.loader.loadRes("prefabs/SlideParticle", cc.Prefab, (err, prefab) => {
        //     var node = cc.instantiate(prefab);
        //     node.parent = cc.find("Canvas").parent;
        //     this.slideParticle = node.getComponent(cc.ParticleSystem);
        //     this.slideParticle.node.active = false;
        // });
        var node = cc.instantiate(this.move_particle);
        node.parent = cc.find("Canvas").parent;
        this.slideParticle = node.getComponent(cc.ParticleSystem);
        this.slideParticle.node.active = false;

        //cc.game.removePersistRootNode(this.node);
    }

    onTouchStart(event) {
        var pos = event.touch.getLocation();
        if (this.slideParticle) {
            this.slideParticle.node.position = pos;
            this.slideParticle.resetSystem();
            this.slideParticle.duration = -1;//粒子一直发射
        }
    }

    onTouchMove(event) {
        this.onHideTouchParticle();//移动的时候不播放点击的粒子动画
        this._isTouchMove = true;
        if (this.slideParticle) {
            this.slideParticle.duration = -1;//粒子一直发射
            this.slideParticle.node.position = event.touch.getLocation();
            this.slideParticle.node.active = true;
        }
    }

    onTouchEnd(event) {
        if (this.slideParticle) {
            this.slideParticle.duration = 1;//粒子生命周期调为1s
        }
        var pos = event.touch.getLocation();
        if (!this._isTouchMove) this.playClickParticle(pos);
        this._isTouchMove = false;
    }

    /**播放点击的粒子动画 */
    playClickParticle(pos) {
        if (this.clickParticle) {
            this.clickParticle.duration = 0.3;//粒子生命周期调为1s
            this.clickParticle.node.position = pos;
            this.clickParticle.node.active = true;
            this.clickParticle.resetSystem();
        } else {
            if (this._isLoaderParticle) {
                return;
            }
            this._isLoaderParticle = true;
            // cc.loader.loadRes("prefabs/ClickParticle", cc.Prefab, (err, prefab) => {
            //     var node = cc.instantiate(prefab);
            //     node.parent = cc.find("Canvas").parent;
            //     this.clickParticle = node.getComponent(cc.ParticleSystem);
            //     this.clickParticle.node.position = pos;
            //     this.clickParticle.resetSystem();
            // });

            var node = cc.instantiate(this.touch_particle);
            node.parent = cc.find("Canvas").parent;
            this.clickParticle = node.getComponent(cc.ParticleSystem);
            this.clickParticle.node.position = pos;
            this.clickParticle.resetSystem();
            this.clickParticle.duration = 0.3;//粒子生命周期调为1s
        }
        if (this.slideParticle) {
            this.slideParticle.node.active = false;
        }
    }
    /**隐藏点击的粒子动画 */
    onHideTouchParticle() {
        if (this.clickParticle) {
            this.clickParticle.node.active = false;
        }
    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值