代码拉取完成,页面将自动刷新
cc.Class({
extends: cc.Component,
properties: {
},
//使用对象池动态实例化预制资源
newClickNode(position, callBack) {
let newNode = null;
if (!this._clickPool) {
//初始化对象池
this._clickPool = new cc.NodePool();
}
if (this._clickPool.size() > 0) {
//从对象池请求对象
newNode = this._clickPool.get();
this.setClickNode(newNode, position, callBack);
} else {
// 如果没有空闲对象,我们就用 cc.instantiate 重新创建
cc.loader.loadRes("prefab/clickAnim", cc.Prefab, function (err, prefab) {
if (err) {
return;
}
newNode = cc.instantiate(prefab);
this.setClickNode(newNode, position, callBack);
}.bind(this));
}
},
setClickNode(newNode, position, callBack) {
newNode.name = "clickNode"; //设置节点名称
newNode.setPosition(position);
newNode.getComponent(cc.Animation).play('click', 0);
newNode.setPosition(position); //设置节点位置
this.clickNode.addChild(newNode); //将新的节点添加到当前组件所有节点上
if (callBack) {
callBack(newNode); //回调节点
}
},
start() {
this._initNodeTouchEvent();
this.clickNode = this.node.getChildByName("ClickNode");
},
_initNodeTouchEvent() {
//监听事件
this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
},
_destroyTouchEvent() {
//销毁事件
this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
cc.log("销毁事件...");
},
_onTouchBegin: function (event) {
//获取当前点击的全局坐标
let temp = event.getLocation();
//获取当前点击的局部坐标
let tempClick = this.node.convertToNodeSpaceAR(temp)
this.newClickNode(tempClick, function (node) {
if (!node) return
cc.log("子节点数:" + this.clickNode.children.length);
node.on("clickDone", function (event) {
// this._clickPool.put(event.obj);
event.obj.destroy();
cc.log("顺利回收....");
}.bind(this));
}.bind(this));
},
_onTouchMoved: function (event) {
cc.log('_onTouchMoved');
},
_onTouchEnd: function (event) {
cc.log('_onTouchEnd');
},
_onTouchCancel: function (event) {
cc.log('_onTouchCancel');
},
onDestroy() {
//销毁事件
this._destroyTouchEvent();
},
toBack() {
cc.director.loadScene("Scene");
},
// onLoad () {},
// update (dt) {},
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。