Cocos2d-js 開發記錄:自訂按鈕

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   sp   div   

遊戲開發總是有些特殊一般的預製的UI無法滿足要求,其實對於不複雜的功能,與其看文檔還不如自己寫一個。比如遊戲中一個虛擬鍵盤,其中的按鍵在按下時會增長,在變為原來的兩倍高度,在原來高度上方顯示按鍵的字如:

  

整體鍵盤:

 

 

 

/* implementation element(key button) used by keyboard */var KeyMenuItem = cc.MenuItem.extend({    _label: null,    _extending: null,    _hiding: null,    _normal_sprite: null,    _press_sprite: null,    FONT_EXTENDED_BOTTOM_PADDING_FACTOR: 0.75,    FONT_BOTTOM_PADDING_FACTOR: 0,    FONT_SIZE_FACTOR: 0.4,    ctor: function(normal_img, press_img, text, callback, target) {        cc.MenuItem.prototype.ctor.call(this);        this.initWithCallback(callback, target);        var normal_sprite = new cc.Sprite(normal_img);        var press_sprite = new cc.Sprite(press_img);        this._normal_sprite = normal_sprite;        this._press_sprite = press_sprite;        this.width = normal_sprite.width;        this.height= normal_sprite.height;        this._extending = false;        this._hiding = false;        var label = new cc.LabelTTF(text, "Arial", Math.ceil(normal_sprite.width * this.FONT_SIZE_FACTOR));        label.setColor(cc.color(0, 0, 0, 255));        this._label = label;        this.setNormal();        this.addChild(label, 2);        this.addChild(press_sprite, 0);        this.addChild(normal_sprite, 1);        this.cascadeColor = true;        this.cascadeOpacity = true;    },    selected: function() {        cc.MenuItem.prototype.selected.call(this);        if (this._enabled) {            this.setPress();            cc.log("custom button selected");        }        cc.audioEngine.playMusic(res.button_press_mp3, false);    },    unselected: function() {        cc.MenuItem.prototype.unselected.call(this);        if (this._enabled) {            this.setNormal();            cc.log("custom button unselected");        }    },    setNormal: function() {        this.setLabelNormal();        this.setSpriteNormal();    },    setPress: function() {        this.setLabelPressed();        this.setSpritePressed();    },    setLabelNormal: function () {        var label = this._label;        var nsprite = this._normal_sprite;        label.setPosition(nsprite.width / 2.0, this.height * (0.5 + this.FONT_BOTTOM_PADDING_FACTOR));    },    setLabelPressed: function() {        var label = this._label;        var psprite = this._press_sprite;        var factor = this.FONT_EXTENDED_BOTTOM_PADDING_FACTOR;        label.setPosition(psprite.width / 2.0, psprite.height * factor);    },    setSpriteNormal: function () {        var nsprite = this._normal_sprite;        var psprite = this._press_sprite;        psprite.visible = false;        nsprite.visible = true;        nsprite.setPosition(this.width / 2.0, this.height / 2.0);        psprite.setPosition(psprite.width / 2.0, psprite.height / 2.0);    },    setSpritePressed: function() {        var nsprite = this._normal_sprite;        var psprite = this._press_sprite;        psprite.visible = true;        nsprite.visible = false;        psprite.setPosition(psprite.width / 2.0, psprite.height / 2.0);        nsprite.setPosition(this.width / 2.0, this.height / 2.0);    },    setEnabled: function(enabled) {        var nsprite = this._normal_sprite;        var psprite = this._press_sprite;        var label = this._label;        if (this._enabled != enabled) {            if (enabled == false) {                this.setOpacity(0);            } else {                this.setOpacity(255);            }        }        cc.MenuItem.prototype.setEnabled.call(this, enabled);    },    getString: function () {        return this._label.getString();    }});

 

Cocos2d-js 開發記錄:自訂按鈕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.