锤子剪子布游戏

来源:互联网 发布:ubuntu分区方案 编辑:程序博客网 时间:2024/04/30 11:22
var HelloWorldLayer = cc.LayerColor.extend({
    sprite:null,
    randomNum:null,
    jiLulabel:null,
    resultlabel:null,
    TollogSelectNum:null,
    resultOb:null,
    ctor:function () {
        this._super(cc.color(0,100,125,255));
        //  tag  1 剪子  2 锤子  3布
        //that.TollogSelectNum    // 0-普通  1-必胜  2-必败
        // this.resultOb.win[tag]
        this.resultOb = {
            win:{
                1:3,
                2:1,
                3:2
            },
            lose:{
                1:2,
                2:3,
                3:1
            }
        }


        var menuArr = []
        for(var i = 1;i<4;i++){
            var menuItem = new cc.MenuItemImage(res['t'+i+'_png'],res['t'+i+'_png'],this.callBack,this);
            menuItem.setTag(i);
            menuArr.push(menuItem)
        }
        var menu=new cc.Menu(menuArr);
        menu.alignItemsHorizontallyWithPadding(20);
        menu.y -= 50
        this.addChild(menu);


        this.jiLulabel = this.creatLabel(cc.p(cc.winSize.width/2,cc.winSize.height*0.25))
        this.resultlabel = this.creatLabel(cc.p(cc.winSize.width/2,cc.winSize.height*0.8))


        var Item1=new cc.MenuItemFont("普通",30);
        var Item2=new cc.MenuItemFont("必胜",30);
        var Item3=new cc.MenuItemFont("必败",30);
        var that=this;
          var menuTollog=new cc.MenuItemToggle(
                 Item1,Item2,Item3,function(){
                    that.TollogSelectNum=menuTollog.getSelectedIndex();
            },this);
        this.TollogSelectNum = 0
        menuTollog.setPosition(cc.winSize.width/2,cc.winSize.height*0.9);
        var menu1=new cc.Menu(menuTollog);
        menu1.setPosition(0,0);
        this.addChild(menu1)
    },
    creatLabel:function(position){
        var label = new cc.LabelTTF("","",20);
        label.setPosition(position)
        label.setColor(cc.color(0,0,0,255));
        this.addChild(label)
        return label
    },
    callBack:function(sender) {
        var tag = sender.tag
        this.showResult(tag)
    },
    showResult:function(tag){
        //  tag  1 剪子  2 锤子  3布
        //that.TollogSelectNum    // 0-普通  1-必胜  2-必败
        var rand = parseInt(Math.random()*3+1)
        if(this.TollogSelectNum != 0){
            rand = this.TollogSelectNum == 1?this.resultOb.win[tag]:this.resultOb.lose[tag]
        }
        if(this.sprite)  this.sprite.removeFromParent(true)
        this.sprite=new cc.Sprite(res["t"+rand+"_png"]);
        this.sprite.setPosition(cc.winSize.width/2,cc.winSize.height*0.6);
        this.addChild(this.sprite);
        this.sprite.scaleY=-1;
        this.sprite.scaleX=-1;
        this.checkResult(tag, rand)
    },
    checkResult:function(player, computer){
        var end = ""
        if(player == computer){
            end = "平"
        }else{
            end = this.resultOb.win[player] == computer?"胜":"负"
        }


        this.resultlabel .setString(end)
        this.jiLulabel.setString(this.jiLulabel.getString()+end)
    },
});


var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new HelloWorldLayer();
        this.addChild(layer);
    }
});
0 0
原创粉丝点击