cocos creator基礎-(十八)高級UI的實現 - 彈出式對話框、個性化時間進度條


1: 完成彈出式對話框;
2: 完成個性化時間進度條;
彈出式對話框
1:對話框的結構:
  根節點 -->
        mask: 全屏的單色精靈,監聽事件,關閉對話框;  
        dlg 與它的孩子: 對話框的內容,監聽事件,擋住不讓他傳遞到mask節點上;      
        彈出式動畫:
          mask: 漸變進來;
          對話框內容縮放,並加上easing緩動對象;
        收起式動畫:
          mask: 漸變出去;
          對話框內容縮小,並加上easing 緩動對象;
2: 對話框組件腳本
  (1)show_dlg
  (2)hide_dlg
  
 1 // popup_dlg.js
 2 cc.Class({
 3     extends: cc.Component,
 4 
 5     properties: {
 6         // foo: {
 7         //    default: null,      // The default value will be used only when the component attaching
 8         //                           to a node for the first time
 9         //    url: cc.Texture2D,  // optional, default is typeof default
10         //    serializable: true, // optional, default is true
11         //    visible: true,      // optional, default is true
12         //    displayName: 'Foo', // optional
13         //    readonly: false,    // optional, default is false
14         // },
15         // ...
16         mask: {
17             type: cc.Node,
18             default: null,
19         },
20 
21         mask_opacity: 128,
22 
23         content: {
24             type: cc.Node,
25             default: null,
26         },
27 
28     },
29 
30     // use this for initialization
31     onLoad: function () {
32         
33     },
34 
35     show_dlg: function() {
36         this.node.active = true;
37         // mask 漸變出來;
38         this.mask.opacity = 0;
39         var fin = cc.fadeTo(0.3, this.mask_opacity);
40         this.mask.runAction(fin);
41         // dlg由小到大
42 
43         this.content.scale = 0;
44         var s = cc.scaleTo(0.4, 1).easing(cc.easeBackOut());
45         this.content.runAction(s);
46     },
47 
48     hide_dlg: function() {
49         // 
50         var fout = cc.fadeOut(0.3);
51         this.mask.runAction(fout);
52 
53         var s = cc.scaleTo(0.3, 0).easing(cc.easeBackIn());
54         var end_func = cc.callFunc(function() {
55             this.node.active = false;
56         }.bind(this));
57 
58         var seq = cc.sequence([s, end_func]);
59         this.content.runAction(seq);
60     },
61     // called every frame, uncomment this function to activate update callback
62     // update: function (dt) {
63 
64     // },
65 });

 

// 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;
    },
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM