creator 常用方法

监听返回键

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(){
        // if(!cc.sys.isNative && cc.sys.isMobile){
            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)){
                //!#zh: 是否优先将设计分辨率高度撑满视图高度。 */
                //cvs.fitHeight = true;
                
                //如果更长,则用定高
                finalH = dr.height;
                finalW = finalH * rw/rh;
            }
            else{
                /*!#zh: 是否优先将设计分辨率宽度撑满视图宽度。 */
                //cvs.fitWidth = true;
                //如果更短,则用定宽
                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,           // prefab
    _detailLabel: null,   // 内容
    _cancelButton: null,   // 确定按钮
    _enterButton: null,   // 取消按钮
    _enterCallBack: null,   // 回调事件
    _animSpeed: 0.3,    // 动画速度
    _sprite: null,   //人物
};

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...

    },

/**
     * detailString :   内容 RichText 类型.
     * enterCallBack:   确定点击事件回调  function 类型.
     * neeCancel:       是否展示取消按钮 bool 类型 default YES.
     * duration:        动画速度 default = 0.3.
    */
    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.startFadeIn();

            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.startFadeOut();
            self.onDestory();
        };

        // 销毁 alert 
        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;

        };
    },


    // use this for initialization
    onLoad: function () {

    },
    onDestory: function () {

    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值