var vigameBridge = require("./bridge.js"); import KurumiAD from './KurumiAD'; /** * 单利工具类 */ class ZYGameTool{ /** * 坐标数组查找 * @param vec * @param pt * @returns {string|number} */ posIndexOf (vec,pt) { for(var i in vec){ if(vec[i].x==pt.x&&vec[i].y==pt.y) return i; } return -1; } //预加载精灵 loadSpriteFrame (func) { var tempIndex = 0; for (var i = 0; i < resources.length; i++) { cc.resources.load(resources[i], cc.SpriteFrame, function (err, spFrame) { ++tempIndex; var num = tempIndex / resources.length * 100; // console.log("---num--: ", parseInt(num)); if (tempIndex == resources.length - 1) func(); if (err) { cc.log(err.message || err); return; } }.bind(this)); } } /** * 存储int类型数据 * @param {*} index 存储名 * @param {*} key 值 */ setIntegerForKey (index,key) { key = key.toString(); cc.sys.localStorage.setItem(index,key); } /** * 获取int类型数据 * @param {*} index 存储名 * @param {*} key 值 */ getIntegerForKey (index,defaultkey) { defaultkey = defaultkey.toString(); var str = cc.sys.localStorage.getItem(index); if (str == "" || str == null || str == undefined) { this.setIntegerForKey(index,defaultkey); return parseInt(defaultkey); } else { return parseInt(str); } } /** * 存储string类型数据 * @param {*} index 存储名 * @param {*} key 值 */ setStringForKey (index,key) { console.log("---key--: " , key); key = key.toString(); cc.sys.localStorage.setItem(index,key); } /** * 获取string类型数据 * @param {*} index 存储名 * @param {*} key 值 */ getStringForKey (index,defaultkey) { defaultkey = defaultkey.toString(); var str = cc.sys.localStorage.getItem(index); if (str == "" || str == null || str == undefined) { this.setStringForKey(index, defaultkey); return defaultkey; } else { return str; } } //获取金币数量 getCoin () { coinNum = this.getIntegerForKey(localDataItem.coinNum, 10); return coinNum; } useCoin (num){ var key=this.getCoin(); key+=num; coinNum = num; this.setIntegerForKey(localDataItem.coinNum, key); } //打开广告 OpenAd(adPositionName, adType, callback = null, failCallback = null) { // console.log("OpenAd adPositionName=======>" + adPositionName.toString() + " adType====>" + adType.toString()); console.log("--OpenAd---OpenAd--: " , adType.toString()); if (cc.sys.os === cc.sys.OS_IOS) { if(adType=="video"){ if (playeMusic === 1) cc.audioEngine.pauseMusic(); vigameBridge.openAdC(adPositionName,function(openState){ if (playeMusic === 1) cc.audioEngine.resumeMusic(); console.log("open State = " + openState); if(1 == openState){ if(callback){ callback(); } } else { if(failCallback){ failCallback(); } } }.bind(this)); }else{ vigameBridge.openAd(adPositionName); } } else if (cc.sys.os === cc.sys.OS_ANDROID) { if (adType == "video") { // 视频广告播放 KurumiAD.instance.ShowRewarded( (data) => { //播放成功(可以获得奖励) if (callback) { callback(); } console.log('ShowRewarded success'); }, (data) => { //播放失败 if (failCallback) { failCallback(); } console.log('ShowRewarded fail'); } ); // console.log('====google===pppp='); // // jsb.reflection.callStaticMethod("com/ysxq/SDKManager", "showRewardedAd", "()V"); // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AdManage", "showRewardedVideo", "()V"); } else { // 插屏广告播放 KurumiAD.instance.ShowInterstitial( (data) => { //播放成功 }, (data) => { //播放失败 } ); } } } //开始渲染游戏 startDrawGame (callback){ vigameBridge.drawGame(function () { if(callback){ callback(); } }) } }; let inistance = null; module.exports.getInstance = function(){ if(!inistance){ inistance = new ZYGameTool(); } return inistance; }