var HelloWorldLayer = cc.Layer.extend({ TOTAL:19, sprite:null, _resultlab:null, _mainUI:null, _tempInput:null, //运算类型 _tempType:null, //第一个数值 _first:null, //第二个数值 _secend:null, //判断连等 _isCheck:null, ctor:function () { this._super(); this._init(); }, _init:function(){ this._isCheck = false; this._mainUI = getNodeByJson(res.bth_22); this.addChild(this._mainUI); this._resultlab = getWidgetByName(this._mainUI, "Label_20"); this._addEvent(); this._initTempString(); }, //添加点击事件 _addEvent:function(){ for(var i = 0; i<this.TOTAL; i++) getWidgetByName(this._mainUI,"bnt_"+i).addTouchEventListener(this._inputHander.bind(this),this._mainUI); }, _inputHander:function(sender,type){ if(type == ccui.Widget.TOUCH_ENDED){ var str = sender.name.split("bnt_")[1]; if(str <= 10 ) this._addInputShow(str); else if(str == 11)this._showResult(); else if(str <= 15)this._checkResult(str); else if(str == 18)this._clearShow(); } }, _addInputShow: function(value){ value = (value==10) ? ".":value; var str = this._getLabString(); //判断是否输入的是小数点 if(str.indexOf(".") > -1 && value == ".")value =""; //判断开始字符是否是 0 if(str == "0") { if(value == ".") this._tempInput = "0."; else this._tempInput = value; } else { this._tempInput += value; } this._setLabString(this._tempInput); }, //设置文本内容 _setLabString: function(str){ this._resultlab.setString(str); }, //获取文本内容 _getLabString: function(){ return this._resultlab.getString(); }, //清除临时输入的数据 _initTempString:function(){ this._tempInput = "" }, //清除显示 _clearShow: function(){ this._initTempString(); this._setLabString("0"); this._first = 0; this._secend =0; this._tempInput = null; this._isCheck =false; }, //检查输入的结果 _checkResult: function(value){ this._first = Number(this._getLabString()); this._tempType = value; this._initTempString("0"); }, //显示结果 _showResult: function(){ if(this._isCheck == false) { this._secend = Number(this._getLabString()); this._isCheck = true; this._initTempString(); } else this._first = this._tempInput; switch (parseInt(this._tempType)){ case 12: this._tempInput =this._first + this._secend; break; case 13: this._tempInput =this._first - this._secend; break; case 14: this._tempInput =this._first * this._secend; break; case 15: this._tempInput =this._first / this._secend; break; } this._setLabString(this._tempInput); } }); getNodeByJson = function(source){ return ccs.load(source).node; }; //获取显示对象 getWidgetByName = function(par,name){ return ccui.helper.seekWidgetByName(par,name); }; var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });
计算器代码(未完善)
最新推荐文章于 2021-07-28 11:37:21 发布