代码改变世界

cocos2d-x JS 显示系统时间与电量

2017-07-03 14:54  罗任德  阅读(263)  评论(0编辑  收藏  举报

mb.RoomUIView = cc.Layer.extend({
ctor: function () {
this._super();
this.isStartGame = false;

this.emojiPos = [
cc.p(cc.winSize.width / 2, 200),
cc.p(cc.winSize.width - 200, cc.winSize.height / 2),
cc.p(cc.winSize.width / 2, cc.winSize.height - 200),
cc.p(200, cc.winSize.height / 2)
];

this.initBatteryAndClock();
this.initButtons();
this.initVoiceUI();
},

setStartGameSign : function(isStart)
{
this.isStartGame = isStart;
},

initBatteryAndClock : function()
{
var uiBatteryPos = cc.p(25, (cc.winSize.height - 105));
var uiClockPos = cc.p(85, (cc.winSize.height - 105));

//添加电池ui
this.imgBatteryBG = new ccui.ImageView("lwui_img_battery0.png", ccui.Widget.PLIST_TEXTURE);
this.addChild(this.imgBatteryBG);
this.imgBatteryBG.setPosition(uiBatteryPos);

var step = 9;
this.imgBatteryList = [];
for(var i = 0; i < 4; i ++)
{
var imgBattery = new ccui.ImageView("lwui_img_battery1.png", ccui.Widget.PLIST_TEXTURE);
this.imgBatteryBG.addChild(imgBattery);
imgBattery.setAnchorPoint(0, 0.5);
imgBattery.setPosition((1 + step * i), (this.imgBatteryBG.height / 2));

this.imgBatteryList.push(imgBattery);
}

//添加时间ui
this.tfClock = new ccui.Text("", lb.FONT, 26);
this.tfClock.ignoreContentAdaptWithSize(true);
this.tfClock.setPosition(uiClockPos);
this.tfClock.setColor(cc.color(166, 166, 166));
this.addChild(this.tfClock);
},

initButtons : function()
{
// this.btnSetting = new ccui.ImageView("lwui_btn_setting.png", ccui.Widget.PLIST_TEXTURE);
// this.addChild(this.btnSetting);
// this.btnSetting.setPosition((cc.winSize.width - 60), (cc.winSize.height * 0.82));
// jx.utils.img2btn(this.btnSetting, this.onSetting.bind(this));

// this.btnReview = new ccui.ImageView("lwui_btn_review.png", ccui.Widget.PLIST_TEXTURE);
// this.addChild(this.btnReview);
// this.btnReview.setPosition((cc.winSize.width - 60), (cc.winSize.height * 0.72));
// jx.utils.img2btn(this.btnReview, this.onReview.bind(this));

// this.countenanceBtn = new ccui.ImageView("lwui_btn_countenance.png", ccui.Widget.PLIST_TEXTURE);
// this.addChild(this.countenanceBtn);
// this.countenanceBtn.setPosition((cc.winSize.width - 60), (cc.winSize.height * 0.35));
// jx.utils.img2btn(this.countenanceBtn, this.onCountenance.bind(this));

// this.btnVoice = new ccui.ImageView("lwui_btn_voice.png", ccui.Widget.PLIST_TEXTURE);
// this.addChild(this.btnVoice);
// this.btnVoice.setPosition((cc.winSize.width - 60), (cc.winSize.height * 0.25));
// jx.utils.img2btn(this.btnVoice, this.onVoice.bind(this));
},

initVoiceUI : function()
{
// var bgWidth = 280;
// var bgHeight = 210;
// this.voiceUIBG = new ccui.ImageView("lwui_voice_bg.png", ccui.Widget.PLIST_TEXTURE);
// this.voiceUIBG.setScale9Enabled(true);
// this.voiceUIBG.setCapInsets(cc.rect(20, 20, 20, 20));
// this.voiceUIBG.setAnchorPoint(1, 0);
// this.voiceUIBG.setPosition((cc.winSize.width - 130), (cc.winSize.height * 0.2 - 45));
// this.voiceUIBG.setContentSize(280, 210);
// this.voiceUIBG.setVisible(false);
// this.addChild(this.voiceUIBG);

// var imgTips = new ccui.ImageView("lwui_voice_record.png", ccui.Widget.PLIST_TEXTURE);
// imgTips.ignoreContentAdaptWithSize(true);
// imgTips.setAnchorPoint(0.5, 0.5);
// imgTips.setPosition((bgWidth * 0.5), (bgHeight * 0.68));
// imgTips.setName("imgTips");
// this.voiceUIBG.addChild(imgTips);

// var tfTips = new ccui.Text("", lb.FONT, 30);
// tfTips.ignoreContentAdaptWithSize(true);
// tfTips.setPosition((bgWidth * 0.5), (bgHeight * 0.20));
// tfTips.setColor(cc.color(255, 255, 255));
// tfTips.setName("tfTips");
// this.voiceUIBG.addChild(tfTips);

// setTimeout(function(){
// this.vc = new gb.VoiceController(this.btnVoice, this.voiceUIBG, this);
// }.bind(this), 100);
},

initVoicePlayingUI:function()
{
// if(this.cPlayVoiceList == null)
// {
// //语音播放显示位置
// this.cPlayVoiceList = [];
// var container = this.getParent();
// var ptOffsets = [
// [80, 114],
// [-80, 105],
// [-94, -66],
// [72, 108]
// ];
// for(var posLocal = 0; posLocal < 4; ++posLocal)
// {
// var cPlayVoice = jx.uiutils.seekWidgetByName(container, "cPlayVoice" + posLocal);
// var cPlayerFrame = jx.uiutils.seekWidgetByName(container, "playerFrame" + posLocal);

// //修改层次,以免挡住麻将牌
// var ptPlayerFrame = cPlayerFrame.getPosition();
// var ptOffset = ptOffsets[posLocal];
// var ptPlayVoice = cc.p(ptPlayerFrame.x + ptOffset[0], ptPlayerFrame.y + ptOffset[1]);
// cPlayVoice.setPosition(ptPlayVoice);

// var pt = this.convertToNodeSpace(cPlayVoice.convertToWorldSpace(cc.p(0, 0)));
// cPlayVoice.retain();
// cPlayVoice.removeFromParent();
// this.addChild(cPlayVoice);
// cPlayVoice.release();
// cPlayVoice.setPosition(pt);
// // cPlayVoice.setVisible(true);

// this.cPlayVoiceList [posLocal] = cPlayVoice;
// }

// }
},

//*-----------------------------------实现VoiceController接口--------------------------------------*//
onVoiceRecorded:function(voiceId, duration)
{
// mb.netHandler.sendTalk(null, voiceId, duration);
},

onStopPlay:function()
{
// if(this.playVoiceIntervalId != null)
// {
// clearInterval(this.playVoiceIntervalId);
// this.playVoiceIntervalId = null;
// }

// if(this.playingVoiceTray != null)
// {
// this.playingVoiceTray.setVisible(false);
// this.playingVoiceTray = null;
// }

},

onPlayRecord:function(data)
{
// this.onStopPlay();
// this.showVoicePlayingEffect(data["pos"], data["duration"]);
},
//*-----------------------------------实现VoiceController接口 end--------------------------------------*//

// onSetting : function()
// {
// gc.Sound.clickButton();

// var btnFun = function()
// {
// gc.Sound.clickButton();

// //发送解散房间申请
// jx.alert(gb.getText("ds_ask"), jx.AlertButtonType.OK_Cancel, function(confirm){
// if(confirm)
// {
// settingView.hide();

// mb.netHandler.sendDissolve();
// }
// });

// }.bind(this);

// var settingView = new mb.SettingView(this.isStartGame, btnFun);
// settingView.show();
// },

// onReview : function()
// {
// gc.Sound.clickButton();

// var setData = mb.lastSetUserDatas;

// if(setData != null)
// {
// var setDataClone = jx.utils.cloneObject(setData);
// mb.showSetBalance(setDataClone, null);
// }
// else
// jx.alert(gb.getText("no_lastSetUserData"));
// },

// onCountenance : function()
// {
// gc.Sound.clickButton();

// var pos = this.emojiPos[0];

// if(this.emojiHandler == null)
// {
// this.emojiHandler = new mb.Emoji();
// }
// this.emojiHandler.showPanel(this, cc.p(100, 100), (function(idx){
// this.emojiHandler.showEmoji(idx, this, pos);

// //发送广播协议
// mb.netHandler.sendTalk(idx);
// }).bind(this));
// },

// onVoice : function()
// {
// gc.Sound.clickButton();
// },

// receivedTalk : function(data)
// {
// var posLocal = mb.getLocalPos(data["talkSide"]);
// var idx = data["emoticons"];
// var voiceId = data["voice"];
// if(idx != null)
// {
// if(posLocal != 0) {
// var ptEmoji = this.emojiPos[posLocal];
// if (this.emojiHandler == null) {
// this.emojiHandler = new mb.Emoji();
// }
// this.emojiHandler.showEmoji(idx, this, ptEmoji);
// }
// }
// else if(voiceId != null)
// {
// //收到语音处理
// var voiceData = {
// voiceId : voiceId,
// pos : posLocal,
// duration : data["duration"]
// };
// this.vc.playRecord(voiceData);
// }
// },

// showVoicePlayingEffect:function(posPlayVoice, duration)
// {
// var cPlayVoice = this.cPlayVoiceList[posPlayVoice];
// var tfTime = cPlayVoice.getChildByName("tfTime");
// tfTime.setString(duration + "s");
// cPlayVoice.setVisible(true);
// this.playingVoiceTray = cPlayVoice;

// this.playVoiceIntervalId = setInterval(function(){
// --duration;
// if(duration == 0)
// {
// this.onStopPlay();
// return;
// }
// tfTime.setString(duration + "s");
// }.bind(this), 1000);
// },

onEnter : function()
{
this._super();

this.talkListener = this.receivedTalk.bind(this);
mb.netHandler.addMsgListener(S_C_TALK, this.talkListener);

this.initVoicePlayingUI();

//定时刷新电池电量和时间
this.timeCt = 0;
this.batteryCt = 0;
this.scheduleUpdate();
},

update : function(dt)
{
this.timeCt -= dt;
this.batteryCt -= dt;

if(this.timeCt < 0) {
//十五秒刷新一次
this.timeCt = 15;
this.timeUpdate();
}

if(this.batteryCt < 0) {
//三分钟一刷新
this.batteryCt = 180;
this.batteryUpdate();
}
},

timeUpdate : function()
{
var t = new Date();
var hours = (t.getHours() > 9) ? t.getHours() : ("0" + t.getHours());
var minutes = (t.getMinutes() > 9) ? t.getMinutes() : ("0" + t.getMinutes());

var str = "" + hours + ":" + minutes;
this.tfClock.setString(str);
},

batteryUpdate : function()
{
var pct = lb.nativeCalls.getBatteryPct();
var num = 4;
var showNum = Math.floor(pct / 0.2);

for(var i = 0; i < num ; i ++)
{
var imgBattery = this.imgBatteryList[i];
imgBattery.setVisible(i < showNum);
}
},

onExit : function()
{
this.vc.dispose();
mb.netHandler.removeMsgListener(S_C_TALK, this.talkListener);

this._super();
}
});

mb.RoomUIView.show = function(container)
{
var view = new mb.RoomUIView();
container.addChild(view, 999);
return view;
};