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);
相关文章推荐
- Cocos2d-js 3.0 连接 Cocos Studio
- cocos2d-js 3.0 RC0 监听返回键、菜单键、进入后台(home键)、恢复显示等事件
- cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding
- cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding
- cocos2d-js 3.0 ios平台编译打包
- Cocos2d-js 3.0 alp2 使用指南
- Cocos2d-js 3.0 jsb环境调用底层Objective-C代码
- cocos2d-js 3.0 屏幕适配方案 分辨率适应
- cocos2d-js 3.0 显示网络图片
- Cocos2d-js 3.0 颜色变换(调整sprite/图片的色调)
- cocos2d-js 3.0 rc0 编译release报错 value for keystore is not valid. it must resolve to a single path
- cocos2d-js 3.0 jsb环境调用底层java代码
- cocos2d-js 3.0 屏幕适配方案 分辨率适应
- cocos2d-js 3.0 ios平台编译打包
- cocos2d js 3.0 弹出框的实现
- 【cocos2d-js 3.0】制作2048
- Cocos2d-JS打包, cocos2dx-3.0
- 在Cocos2d-JS 3.0开发的游戏中使用的 js 工具库总结
- cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding
- cocos2d-js 3.0 屏幕适配方案 分辨率适应