计算器代码(未完善)

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);
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值