首先创建helloworld的主要框架,然后具体的我再在代码中分析一下
var HelloWorldLayer = cc.Layer.extend({
_mainUI:null,
_a:null,
//
MAX:6,
MIN:1,
_c:null,
ctor:function () {
//////////////////////////////
// 1. super init first
this._super();
this._init();
},
_init:function() {
this._a = 4 ;
this._mainUI = ccs.load(res.Ui_Json).node;
this.addChild(this._mainUI);
this._c=this._getWidgetByName(this._mainUI,"Label_5");
this._setLabString(this._a);
this._getWidgetByName(this._mainUI,"Button_2").addTouchEventListener(this._leftBtn.bind(this),this._mainUI);
this._getWidgetByName(this._mainUI,"Button_1").addTouchEventListener(this._rightBtn.bind(this),this._mainUI);
this._getWidgetByName(this._mainUI,"Button_3").addTouchEventListener(this._startGame.bind(this),this._mainUI);
},
这里是开始切换场景的一个方法
_startGame:function(){
cc.director.runScene(new GameScene());
//this._a =this._getWidgetByName(this._mainUI,"Button_3");
//this._a.addTouchEventListener(this._inputHandler.bind(this),this._mainUI);
//var btn;
//for(var i = 0;i // btn=this._getWidgetByName(this._mainUI,"Button_"+i);
// btn.addTouchEventListener(this._inputHandler.bind(this),this._mainUI)
//}
},
这是回调方法
_inputHandler: function (sender,type) {
if(type==ccui.Widget.TOUCH_ENDED){
//var str=sender.name.split("Button_")[1];
if(sender.name == "Button_3") this._changeScene();
else if(sender.name == "Button_2") this._leftBtn();
else if(sender.name == "Button_1") this._rightBtn();
}
},
其实这是一个拼图游戏裁出来的一部分,这里是左按钮,不做分析
_leftBtn:function(sender,type){
if(type==ccui.Widget.TOUCH_ENDED) {
this._a--;
if (this._a < this.MIN) this._a = this.MAX;
this._setLabString(this._a);
}
},
同样的,这里是右按钮
_rightBtn:function(sender,type){
if(type==ccui.Widget.TOUCH_ENDED) {
this._a++;
if (this._a > this.MAX) this._a = this.MIN;
this._setLabString(this._a);
}
},
切换场景
_changeScene:function(){
var scene =new GameScene();
cc.director.runScene(scene);
},
封装的API
_getWidgetByName:function(par,name){
return ccui.helper.seekWidgetByName(par,name);
},
_setLabString:function(str){
this._c.setString(str);
},
_getLabString:function(){
return this._c.getString();
}