cocos creator基础-(十八)高级UI的实现 - 弹出式对话框、个性化时间进度条

1: 完成弹出式对话框;

2: 完成个性化时间进度条;

弹出式对话框

1:对话框的结构:

  根节点 -->

        mask: 全屏的单色精灵,监听事件,关闭对话框;  

        dlg 与它的孩子: 对话框的内容,监听事件,挡住不让他传递到mask节点上;      

        弹出式动画:

          mask: 渐变进来;

          对话框内容缩放,并加上easing缓动对象;

        收起式动画:

          mask: 渐变出去;

          对话框内容缩小,并加上easing 缓动对象;

2: 对话框组件脚本

  (1)show_dlg

  (2)hide_dlg

// popup_dlg.js
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
        // },
        // ...
        mask: {
            type: cc.Node,
            default: null,
        },

        mask_opacity: 128,

        content: {
            type: cc.Node,
            default: null,
        },

    },

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

    show_dlg: function() {
        this.node.active = true;
        // mask 渐变出来;
        this.mask.opacity = 0;
        var fin = cc.fadeTo(0.3, this.mask_opacity);
        this.mask.runAction(fin);
        // dlg由小到大

        this.content.scale = 0;
        var s = cc.scaleTo(0.4, 1).easing(cc.easeBackOut());
        this.content.runAction(s);
    },

    hide_dlg: function() {
        // 
        var fout = cc.fadeOut(0.3);
        this.mask.runAction(fout);

        var s = cc.scaleTo(0.3, 0).easing(cc.easeBackIn());
        var end_func = cc.callFunc(function() {
            this.node.active = false;
        }.bind(this));

        var seq = cc.sequence([s, end_func]);
        this.content.runAction(seq);
    },
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
// game_scene.js
var popup_dlg = require("piopup_dlg");
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;
        //     }
        // },
        dlg : {
            type : popup_dlg,
            default : null
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },
    on_show_dlg_click: function(){
        this.dlg.showDlg();
    },
    // update (dt) {},
});

个性化时间进度条

1: 编写脚本, 来使用sprite的扇形来显示当前的进度:

  属性:

    time_sec: 定时器的时间

    clockwise: 是否为顺时针或逆时针;

    reverse: 是否反转

  start_clock_action: 开始累积时间,看时间过去的百分比,来改变精灵显示的百分比;

  stop_clock_action: 停止计时累积;

// timebar.js
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
        // },
        // ...

        action_time: 15,
        clockwise: false, // 是否为顺时针
        reverse: false, // false, 由少变多,否者的话的就是由多变少;

        play_onload: true, // 是否在加载的时候开始倒计时
    },

    // use this for initialization
    onLoad: function () {
        this.now_time = 0;
        this.is_running = false;
        this.node.active = false; 

        this.sprite = this.getComponent(cc.Sprite);
        if (this.play_onload) {
            this.start_clock_action(this.action_time);
        }    
    },


    start_clock_action: function(action_time, end_func) {
        if (action_time <= 0) {
            return;
        }

        this.end_func = end_func;

        this.node.active = true;
        this.action_time = action_time;
        this.now_time = 0;
        
        this.is_running = true;
    },

    stop_clock_action: function() {
        this.node.active = false;
        this.is_running = false;
    },
    
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if (!this.is_running) {
            return;
        }

        this.now_time += dt;
        var per = this.now_time / this.action_time;
        if (per > 1) { // 结束了,超时了
            per = 1;
            this.is_running = false;
            if (this.end_func) {
                this.end_func();
            }
            
        }
        
        if (this.reverse) {
            per = 1 - per;
        }

        if (this.clockwise) {
            per = -per;
        }
        this.sprite.fillRange = per;
    },
});

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos Creator是一款面向游戏开发者的跨平台游戏开发工具,它集成了Cocos2d-x游戏引擎和Cocos Studio编辑器,支持多种平台上的游戏开发,如iOS、Android、Windows等。 消灭星星是一款基于Cocos Creator开发的游戏。在游戏中,玩家需要消除屏幕上的星星,以获得分数。游戏开始时,屏幕上会随机生成一些星星,玩家可以通过点击相连的星星来消除它们。消除的星星越多,得到的分数就越高。玩家可以通过不断消除星星来刷新高分记录,挑战自己的极限。 在消灭星星游戏中,Cocos Creator提供了丰富的功能和工具,为游戏开发者提供了便利。开发者可以使用Cocos Creator的图形界面编辑器来创建游戏场景、导入资源和设置游戏规则等。同时,Cocos Creator还提供了强大的脚本编写功能,开发者可以使用JavaScript或TypeScript编写游戏逻辑,实现游戏中的各种功能。 除了基本的消除星星玩法,Cocos Creator还支持添加特殊道具、关卡设计、人物角色等功能。开发者可以根据自己的需求,自定义游戏的玩法和功能,使游戏更加有趣和有挑战性。 总而言之,Cocos Creator游戏开发工具提供了强大的功能和便捷的开发环境,使开发者可以轻松地开发出各种各样的游戏,包括消灭星星这样的小而精致的休闲游戏。无论是想要学习游戏开发还是实现自己的游戏创意,Cocos Creator都是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值