达达麻将开房间
达达麻将版图
达达麻将大厅
1: 大厅挂载了Hall.js
2: onLoad 代码:
// use this for initialization
onLoad: function () {
cc.vv.utils.setFitSreenMode();
this.initLabels();
if(cc.vv.gameNetMgr.roomId == null){
this.btnJoinGame.active = true;
this.btnReturnGame.active = false;
}
else{
this.btnJoinGame.active = false;
this.btnReturnGame.active = true;
}
//var params = cc.vv.args;
var roomId = cc.vv.userMgr.oldRoomId
if( roomId != null){
cc.vv.userMgr.oldRoomId = null;
cc.vv.userMgr.enterRoom(roomId);
}
var imgLoader = this.sprHeadImg.node.getComponent("ImageLoader");
imgLoader.setUserID(cc.vv.userMgr.userId);
cc.vv.utils.addClickEvent(this.sprHeadImg.node,this.node,"Hall","onBtnClicked");
this.addComponent("UserInfoShow");
this.initButtonHandler("Canvas/right_bottom/btn_shezhi");
this.initButtonHandler("Canvas/right_bottom/btn_help");
this.initButtonHandler("Canvas/right_bottom/btn_xiaoxi");
this.helpWin.addComponent("OnBack");
this.xiaoxiWin.addComponent("OnBack");
if(!cc.vv.userMgr.notice){
cc.vv.userMgr.notice = {
version:null,
msg:"数据请求中...",
}
}
if(!cc.vv.userMgr.gemstip){
cc.vv.userMgr.gemstip = {
version:null,
msg:"数据请求中...",
}
}
this.lblNotice.string = cc.vv.userMgr.notice.msg;
this.refreshInfo();
this.refreshNotice();
this.refreshGemsTip();
cc.vv.audioMgr.playBGM("bgMain.mp3");
cc.vv.utils.addEscEvent(this.node);
},
initLabels: 同步显示玩家的信息;
initLabels:function(){
this.lblName.string = cc.vv.userMgr.userName;
this.lblMoney.string = cc.vv.userMgr.coins;
this.lblGems.string = cc.vv.userMgr.gems;
this.lblID.string = "ID:" + cc.vv.userMgr.userId;
},
如果当前这个玩家已经开了房间了,有roomID了,就是返回返回房间,否则是加入房间;
如果开了残局,那么直接进入房间
var roomId = cc.vv.userMgr.oldRoomId
if( roomId != null){
cc.vv.userMgr.oldRoomId = null;
cc.vv.userMgr.enterRoom(roomId);
}
3:ImageLoader.js 图像加载,设置了玩家的UID,然后控件加载玩家的图像;
var imgLoader = this.sprHeadImg.node.getComponent("ImageLoader");
function loadImage(url,code,callback){
/*
if(cc.vv.images == null){
cc.vv.images = {};
}
var imageInfo = cc.vv.images[url];
if(imageInfo == null){
imageInfo = {
image:null,
queue:[],
};
cc.vv.images[url] = imageInfo;
}
cc.loader.load(url,function (err,tex) {
imageInfo.image = tex;
var spriteFrame = new cc.SpriteFrame(tex, cc.Rect(0, 0, tex.width, tex.height));
for(var i = 0; i < imageInfo.queue.length; ++i){
var itm = imageInfo.queue[i];
itm.callback(itm.code,spriteFrame);
}
itm.queue = [];
});
if(imageInfo.image != null){
var tex = imageInfo.image;
var spriteFrame = new cc.SpriteFrame(tex, cc.Rect(0, 0, tex.width, tex.height));
callback(code,spriteFrame);
}
else{
imageInfo.queue.push({code:code,callback:callback});
}*/
cc.loader.load(url,function (err,tex) {
var spriteFrame = new cc.SpriteFrame(tex, cc.Rect(0, 0, tex.width, tex.height));
callback(code,spriteFrame);
});
};
function getBaseInfo(userid,callback){
if(cc.vv.baseInfoMap == null){
cc.vv.baseInfoMap = {};
}
if(cc.vv.baseInfoMap[userid] != null){
callback(userid,cc.vv.baseInfoMap[userid]);
}
else{
cc.vv.http.sendRequest('/base_info',{userid:userid},function(ret){
var url = null;
if(ret.headimgurl){
url = cc.vv.http.master_url + '/image?url=' + encodeURIComponent(ret.headimgurl) + ".jpg";
}
var info = {
name:ret.name,
sex:ret.sex,
url:url,
}
cc.vv.baseInfoMap[userid] = info;
callback(userid,info);
},cc.vv.http.master_url);
}
};
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// default: null, // The default value will be used only when the component attaching
// to a node for the first time
// url: cc.Texture2D, // optional, default is typeof default
// serializable: true, // optional, default is true
// visible: true, // optional, default is true
// displayName: 'Foo', // optional
// readonly: false, // optional, default is false
// },
// ...
},
// use this for initialization
onLoad: function () {
this.setupSpriteFrame();
},
setUserID:function(userid){
if(!userid){
return;
}
if(cc.vv.images == null){
cc.vv.images = {};
}
var self = this;
getBaseInfo(userid,function(code,info){
if(info && info.url){
loadImage(info.url,userid,function (err,spriteFrame) {
self._spriteFrame = spriteFrame;
self.setupSpriteFrame();
});
}
});
},
setupSpriteFrame:function(){
if(this._spriteFrame){
var spr = this.getComponent(cc.Sprite);
if(spr){
spr.spriteFrame = this._spriteFrame;
}
}
}
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
4: 添加了头像点击的回掉函数: 如果点击头像,就显示用户信息,用户信息在脚本:UserinfoShow
imgLoader.setUserID(cc.vv.userMgr.userId);
cc.vv.utils.addClickEvent(this.sprHeadImg.node,this.node,"Hall","onBtnClicked");
5: this.addComponent("UserInfoShow");的初始化 --> cc.vv.userinfoShow = this;
this.addComponent("UserInfoShow");
onBtnClicked:function(event){
if(event.target.name == "btn_shezhi"){
this.settingsWin.active = true;
}
else if(event.target.name == "btn_help"){
this.helpWin.active = true;
}
else if(event.target.name == "btn_xiaoxi"){
this.xiaoxiWin.active = true;
}
else if(event.target.name == "head"){
cc.vv.userinfoShow.show(cc.vv.userMgr.userName,cc.vv.userMgr.userId,this.sprHeadImg,cc.vv.userMgr.sex,cc.vv.userMgr.ip);
}
},
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// default: null, // The default value will be used only when the component attaching
// to a node for the first time
// url: cc.Texture2D, // optional, default is typeof default
// serializable: true, // optional, default is true
// visible: true, // optional, default is true
// displayName: 'Foo', // optional
// readonly: false, // optional, default is false
// },
// ...
_userinfo:null,
},
// use this for initialization
onLoad: function () {
if(cc.vv == null){
return;
}
this._userinfo = cc.find("Canvas/userinfo");
this._userinfo.active = false;
cc.vv.utils.addClickEvent(this._userinfo,this.node,"UserInfoShow","onClicked");
cc.vv.userinfoShow = this;
},
show:function(name,userId,iconSprite,sex,ip){
if(userId != null && userId > 0){
this._userinfo.active = true;
this._userinfo.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = iconSprite.spriteFrame;
this._userinfo.getChildByName("name").getComponent(cc.Label).string = name;
this._userinfo.getChildByName("ip").getComponent(cc.Label).string = "IP: " + ip.replace("::ffff:","");
this._userinfo.getChildByName("id").getComponent(cc.Label).string = "ID: " + userId;
var sex_female = this._userinfo.getChildByName("sex_female");
sex_female.active = false;
var sex_male = this._userinfo.getChildByName("sex_male");
sex_male.active = false;
if(sex == 1){
sex_male.active = true;
}
else if(sex == 2){
sex_female.active = true;
}
}
},
onClicked:function(){
this._userinfo.active = false;
}
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
6: 设置帮助和消息按钮的代码关联,和消息时间响应;
this.addComponent("UserInfoShow");
this.initButtonHandler("Canvas/right_bottom/btn_shezhi");
this.initButtonHandler("Canvas/right_bottom/btn_help");
this.initButtonHandler("Canvas/right_bottom/btn_xiaoxi");
this.helpWin.addComponent("OnBack");
this.xiaoxiWin.addComponent("OnBack");
7: 同步房卡的数目:refreshInfo,同步游戏通知;
this.refreshInfo();
this.refreshNotice();
this.refreshGemsTip();
refreshInfo:function(){
var self = this;
var onGet = function(ret){
if(ret.errcode !== 0){
console.log(ret.errmsg);
}
else{
if(ret.gems != null){
this.lblGems.string = ret.gems;
}
}
};
var data = {
account:cc.vv.userMgr.account,
sign:cc.vv.userMgr.sign,
};
cc.vv.http.sendRequest("/get_user_status",data,onGet.bind(this));
},
refreshGemsTip:functio