首先創建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<this.TOTAL;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();
}