按钮点击传一个回调函数,在点击按钮后执行
//挂在按钮所在panel上
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
msg: cc.Label,
btn_A: cc.Button,
btn_B: cc.Node,
text1: cc.Label,
text2: cc.Label,
lizi: cc.Prefab,
liziParent: cc.Node,
TipNode: cc.Node,
setfunc: false,
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
this.node.x = cc.winSize.width / 2;
this.node.y = cc.winSize.height / 2;
//this.touch();
this.showOne();
},
start() {
},
setMsg(str) {
this.msg.string = str;
},
// LIFE-CYCLE CALLBACKS:
touch() {
//点击粒子特效
var self = this;
this.liziParent.on(cc.Node.EventType.TOUCH_START, function (event) {
var touches = event.getTouches();
if (touches.length !== 1) {
return;
}
var touchLoc = touches[0].getLocation();
self.lizinode = cc.instantiate(self.lizi);//克隆预制体
self.lizinode.parent = self.node;//设置父节点
self.lizinode.x = touchLoc.x - cc.winSize.width / 2;
self.lizinode.y = touchLoc.y - cc.winSize.height / 2;
});
this.liziParent.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
var touches = event.getTouches();
var touchLoc = touches[0].getLocation();
self.lizinode.x = touchLoc.x - cc.winSize.width / 2;
self.lizinode.y = touchLoc.y - cc.winSize.height / 2;
});
this.node.on(cc.Node.EventType.TOUCH_CANCEL, function () {
if (self.lizinode) {
self.lizinode.destroy();
}
});
this.node.on(cc.Node.EventType.TOUCH_END, function () {
if (self.lizinode) {
self.lizinode.destroy();
}
});
},
hidBtn() {
this.btn_A.node.active = false;
},
btn() {
if (this.setfunc) {
this.Function();
}
this.node.destroy();
},
showOne() {
this.btn_B.active = false;
this.btn_A.node.active = true;
},
showTwo() {
this.btn_B.active = true;
this.btn_A.node.active = false;
},
close() {
this.node.destroy();
},
setText(str) {
this.text1.string = str;
this.text2.string = str;
},
setReGame() {
//this.setFunction(function () { require('GameUtils').loadScene("LoginScene"); });
},
setFunction(fun) {
this.Function = fun;
this.setfunc = true;
},
// update (dt) {},
});
使用:
showTip(msg, hidBtn, str, callBackfunction) {
require("AudioManager").Instance().playerBtnCheck(1);
this.tip = cc.instantiate(this.tips);
this.tip.getComponent("Tips").setMsg(msg);
this.tip.parent = this.node;
if (hidBtn === 0) {
this.tip.getComponent("Tips").hidBtn();
}
else if (hidBtn === 1) {
}
else if (hidBtn === 2) {
this.tip.getComponent("Tips").showTwo();
}
else if (hidBtn === -1) {
this.tip.getComponent("Tips").setReGame();
}
if (str) {
this.tip.getComponent("Tips").setText(str);
}
if (callBackfunction) {
this.tip.getComponent("Tips").setFunction(callBackfunction);
}
else if (!hidBtn) {
}
},
destoryTip() {
if (this.tip) {
this.tip.destroy();
}
},
简易版:(TS)
// Learn TypeScript:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const { ccclass, property } = cc._decorator;
@ccclass
export class SomeInfo extends cc.Component {
private showCloseBtn = true;
private yesCb: () => void;
showInfo(info: string, showCloseBtn: boolean = false, yesCb?: () => void) {
this.node.getChildByName("label").getComponent(cc.Label).string = info;
this.yesCb = yesCb;
this.showCloseBtn = showCloseBtn;
if (!showCloseBtn) {
this.node.getChildByName("btn_close").active = false;
}
}
btn_close() {
if (this.showCloseBtn) {
this.node.destroy();
}
}
btn_yes() {
this.node.destroy();
if (this.yesCb) {
this.yesCb();
}
}
}