COCOS2D-JS Development Record: Custom button

Source: Internet
Author: User
Tags addchild

Game development is always some special general pre-fabricated UI can not meet the requirements, in fact, for the non-complex features, rather than looking at the document itself to write one. For example, a virtual keyboard in the game, where the keys will grow when pressed, in the original twice times the height of the original height above the display of key words such as:

Overall keyboard:

/*implementation Element (key button) used by keyboard*/varKeymenuitem =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); varNormal_sprite =Newcc.        Sprite (NORMAL_IMG); varPress_sprite =Newcc.        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; varLabel =NewCc. 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 () {        varLabel = This. _label; varNsprite = This. _normal_sprite; Label.setposition (Nsprite.width/2.0, This. Height * (0.5 + This.    Font_bottom_padding_factor)); }, setlabelpressed:function() {        varLabel = This. _label; varPsprite = This. _press_sprite; varFactor = This.        Font_extended_bottom_padding_factor; Label.setposition (Psprite.width/2.0, Psprite.height *factor); }, Setspritenormal:function () {        varNsprite = This. _normal_sprite; varPsprite = 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() {        varNsprite = This. _normal_sprite; varPsprite = 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) {varNsprite = This. _normal_sprite; varPsprite = This. _press_sprite; varLabel = 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 Development Record: Custom button

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.