您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-js 3.0 单选按钮

2014-11-23 13:38 176 查看

本人只贴上代码,有点基础的童鞋都应该能看懂就不做解释了。

var RadioGroup=cc.Layer.extend({

    _menu:null,

    _selectId:0,

    ctor:function (){

        this._super();

    },

    init:function(arraytxt,arrayPoint){

        cc.assert(arraytxt.length==arrayPoint.length,"must 相等");

        var num=arraytxt.length;

        this._menu = cc.Menu.create();

        this._menu.x = 0;

        this._menu.y = 0;

        this.addChild(this._menu);

        for(var i=0;i<num;i++){

            var point=arrayPoint[i];

            var bt = cc.MenuItemSprite.create(cc.Sprite.create(res.radio), cc.Sprite.create(res.radio1),this._radioFun, this);

            bt.x=point.x;

            bt.y=point.y;

            bt.addChild(this._createLf(arraytxt[i],bt.getContentSize().width,bt.getContentSize().height/2));

            this._menu.addChild(bt);

        }

        this.setSelectId(0);

    },

    _createLf:function(word,px,py){

        var fontSize=80;

        var fontDefRedStrokeShadow = new cc.FontDefinition();

        fontDefRedStrokeShadow.fontName = "Arial";

        fontDefRedStrokeShadow.fontSize = fontSize;

        fontDefRedStrokeShadow.textAlign = cc.TEXT_ALIGNMENT_CENTER;

        fontDefRedStrokeShadow.verticalAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP;

        fontDefRedStrokeShadow.strokeEnabled = true;

        fontDefRedStrokeShadow.shadowEnabled = true;

        var wordLf=cc.LabelTTF.create(word,fontDefRedStrokeShadow);

        wordLf.color=cc.color(82,47,19);

        wordLf.anchorX=0;

        wordLf.x=px;

        wordLf.y=py;

        return wordLf;

    },

    _radioFun:function(menuItem){

        var items=this._menu.getChildren();

        for(var i=0;i<items.length;i++){

            if(items[i]==menuItem){

                this.setSelectId(i);

                break;

            }

        }

    },

    setSelectId:function(num){

        var items=this._menu.getChildren();

        items[this._selectId].unselected();

        items[num].selected();

        this._selectId=num;

    },

    getSelectId:function(){

        return this._selectId;

    }

});

RadioGroup.create=function(arraytxt,arrayPoint){

    var sg=new RadioGroup();

    sg.init(arraytxt,arrayPoint)

    return sg;

};

调用方式:
 this._qiJuRadio=RadioGroup.create(["转盘","飞盘"],[cc.p(130,1090),cc.p(470,1090)]);

 this.addChild(this._qiJuRadio);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息