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;
}
}
}
【cocos creator】点击屏幕粒子效果,不妨碍按钮点击[2022.07更新]
于 2021-03-16 18:38:56 首次发布