监听返回键
addEscEvent:function(node){
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function(event){
if (event.keyCode == cc.macro.KEY.back) {
cc.vv.alert.show('提示','确定要退出游戏吗?',function(){
cc.game.end();
},true);
}
}, node);
},
屏幕适配
fitScreen:function(){
var cvs = cc.find('Canvas').getComponent(cc.Canvas);
cvs.fitHeight = false;
cvs.fitWidth = false;
if(!this.curDR){
this.curDR = cvs.designResolution;
}
var dr = this.curDR;
var s = cc.view.getFrameSize();
var rw = s.width;
var rh = s.height;
var finalW = rw;
var finalH = rh;
if((rw/rh) > (dr.width / dr.height)){
finalH = dr.height;
finalW = finalH * rw/rh;
}
else{
finalW = dr.width;
finalH = rh/rw * finalW;
}
cvs.designResolution = cc.size(finalW, finalH);
cvs.node.width = finalW;
cvs.node.height = finalH;
},
通用弹框
var Alert = {
_alert: null,
_detailLabel: null,
_cancelButton: null,
_enterButton: null,
_enterCallBack: null,
_animSpeed: 0.3,
_sprite: null,
};
cc.Class({
extends: cc.Component,
properties: {
},
showRichText(detailString, enterCallBack, needCancel, animSpeed) {
var self = this;
if (Alert._alert != null) return;
Alert._animSpeed = animSpeed ? animSpeed : Alert._animSpeed;
cc.loader.loadRes("p_prefabs/alertRichText", cc.Prefab, function (error, prefab) {
if (error) {
cc.error(error);
return;
}
var alert = cc.instantiate(prefab);
Alert._alert = alert;
var cbFadeOut = cc.callFunc(self.onFadeOutFinish, self);
var cbFadeIn = cc.callFunc(self.onFadeInFinish, self);
self.actionFadeIn = cc.sequence(cc.spawn(cc.fadeTo(Alert._animSpeed, 255), cc.scaleTo(Alert._animSpeed, 1.0)), cbFadeIn);
self.actionFadeOut = cc.sequence(cc.spawn(cc.fadeTo(Alert._animSpeed, 0), cc.scaleTo(Alert._animSpeed, 2.0)), cbFadeOut);
Alert._detailLabel = cc.find("RichText_content", alert).getComponent(cc.RichText);
Alert._closeButton = cc.find("btn_close", alert);
Alert._enterButton = cc.find("btn_ensure", alert);
Alert._cancelButton = cc.find("btn_cancel", alert);
Alert._enterButton.on('click', self.onButtonClicked, self);
Alert._cancelButton.on('click', self.onButtonClicked, self);
Alert._closeButton.on('click', self.onButtonClicked, self);
Alert._alert.parent = cc.find("Canvas");
self.configAlert(detailString, enterCallBack, needCancel, animSpeed);
});
self.configAlert = function (detailString, enterCallBack, needCancel, animSpeed) {
Alert._enterCallBack = enterCallBack;
Alert._detailLabel.string = detailString;
if (needCancel || needCancel == undefined) {
Alert._cancelButton.active = true;
} else {
Alert._cancelButton.active = false;
Alert._enterButton.x = 0;
}
};
self.startFadeIn = function () {
Alert._alert.position = cc.v2(0, 0);
Alert._alert.setScale(2);
Alert._alert.opacity = 0;
Alert._alert.runAction(self.actionFadeIn);
};
self.startFadeOut = function () {
Alert._alert.runAction(self.actionFadeOut);
};
self.onFadeInFinish = function () {
};
self.onFadeOutFinish = function () {
self.onDestory();
};
self.onButtonClicked = function (event) {
if (event.target.name == "btn_ensure") {
console.log("确认按钮");
if (self._enterCallBack) {
self._enterCallBack();
}
} else {
console.log("取消、关闭按钮");
}
self.onDestory();
};
self.onDestory = function () {
Alert._alert.destroy();
Alert._enterCallBack = null;
Alert._alert = null;
Alert._detailLabel = null;
Alert._cancelButton = null;
Alert._enterButton = null;
Alert._animSpeed = 0.3;
Alert._sprite = null;
};
},
onLoad: function () {
},
onDestory: function () {
}
});