锤子剪子布游戏
2016-07-24 22:29
330 查看
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);
}
});
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);
}
});
相关文章推荐
- 【杭电 5363】Key Set
- 【杭电2004】成绩转换
- ActivityManifest里面windowSoftInputMode属性简介
- 技术干货的选择性问题
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- 整合SSH框架 (Struts2.x+Spring3.x+hibernate3.x+oracle)
- 【Linux】进程等待&程序替换
- 释迦牟尼:最经典和灵性的4句话
- 如何合理利用业余时间
- 【Android】使用http的Post和Get方式获取网络数据
- Linux下Perl的安装 此博文包含图片
- 加速npm下载的方法
- JavaWeb核心编程之(三.5)HTTP请求和接受表单数据
- Android多线程之HandlerThread
- 背景图片每次刷新以后随机切换和背景图片隔一段时间换一个
- hihocoder 1121
- 工业4.0提出者孔翰宁详解工业4.0
- 操作系统
- hdu 5475 An easy problem (线段树)
- FreeMarker与Velocity的区别