123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932 |
- /**
- * 广告sdk & 网络控制
- * ts 版本
- * 单例
- */
- import utils from "./Utils";
- export default class SdkUtil {
- public static _instance:SdkUtil = null
- isvideoend: boolean;
- checkVideo: boolean;
- private _type: any;
- isdebug = false //是测试模式 该模式 无广告 无 网络
- public static get Instance(){
- if (null == this._instance) {
- this._instance = new SdkUtil();
- }
- return this._instance
- }
- //platform:string = 'tt'
- private _platform:string
- get platform():string {
- if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
- this._platform = 'tt'
- return this._platform
- }else if(cc.sys.platform == cc.sys.WECHAT_GAME){
- this._platform = 'wx'
- return this._platform
- }
- }
- set plaform(str){
- this._platform = str
- }
- videoRecordingState:any = 0;
- video_recorder:any = null;
- videoStartTime:any = null;
- videoPath:any = null;
- videoIsExist:any = 0;//视频是否存在 默认存在
- recorderTime:any = 120; //视频录制时间
- videoAd:any = null; //激励视频
- chaPingErr:any = false; //插屏报错(触发频繁限制等)
- chaPing:any = null
- bannerAd:any = null; //横屏广告
- videoCallBack = null //激励视频成功回调
- videoFailCallBack = null //激励视频失败回调
- isshowbanner = false
- //platformSdk:any = tt //字节跳动sdk
- tt_info:any = {
- gamename:"字节游戏名称",
- shareTitle:'',
- appId:'appid',
- videoId: 'videoid',
- chapingId: 'chapingid',
- bannerId: "bannerid",
- templateId: ['','','',''],
- videoTopics:['',''],
- appSecret:'',
- }
- ks_info:any = {
- gamename:"",
- shareTitle:'',
- appId:'',
- bannerId: "",
- videoId: '',
- chapingId: '',
- templateId: ['','','',''],
- videoTopics:['',''],
- appSecret:'',
- }
- wx_info:any = {
- gamename:"微信游戏名称",//游戏名称
- shareTitle:'',
- appId:'appid', //appid
- bannerId: "bannerid",//横幅 已接入
- videoId:'videoid', //6---30s 激励
- videoId2:'videoid', //6---60s 激励 // 主要用这个 已接入
- chapingId:'chapingid', //插屏已接入
- gezi:'geziid', //格子未接入
- NativeAd:{ // 原生 格子未接入
- juzhen:'juzhenid',
- duogezi:'duogeziid',
- dangezi:'dangeziid'
- },
- templateId: ['','','',''],
- videoTopics:['','']
- }
- url:string = 'https://www.xxxxx.top/sgame'
- web_url = ' https://www.xxxxx.top/pv'
- /**
- * 获取平台 类
- */
- getPlatformClass(){
- var p_class = null
- switch(this.platform){
- case 'tt':
- //@ts-ignore
- p_class = tt
- break
- case 'op':
- //@ts-ignore
- p_class = qg
- break
- case 'wx':
- //@ts-ignore
- p_class = wx
- break
- case 'ks':
- //@ts-ignore
- p_class = ks
- break
- }
- return p_class
- }
- /**
- * 获取分享标题
- */
- getShareTitle(){
- var title = ''
- switch(this.platform){
- case 'tt':
- title = this.tt_info.shareTitle
- break
- case 'ks':
- title = this.ks_info.shareTitle
- break
- case 'wx':
- title = this.wx_info.shareTitle
- break
- }
- return title
- }
- /**
- * 获取名字
- */
- getName(){
- var name = ''
- switch(this.platform){
- case 'tt':
- name = this.tt_info.gamename
- break
- case 'ks':
- name = this.ks_info.gamename
- break
- case 'wx':
- name = this.wx_info.gamename
- break
- }
- return name
- }
- /**
- * 获取appid
- */
- getAppId(){
- var appid = ''
- switch(this.platform){
- case 'tt':
- appid = this.tt_info.appId
- break
- case 'ks':
- appid = this.ks_info.appId
- break
- case 'wx':
- appid = this.wx_info.appId
- break
- }
- return appid
- }
- /**
- * 获取AppSecret
- */
- getAppSecret(){
- var appSecret = ''
- switch(this.platform){
- case 'tt':
- appSecret = this.tt_info.appSecret
- break
- case 'ks':
- break
- }
- return appSecret
- }
-
- /**
- * 获取bannerid
- */
- getBannerId(){
- var bannerID = ''
- switch(this.platform){
- case 'tt':
- bannerID = this.tt_info.bannerId
- break
- case 'ks':
- bannerID = this.ks_info.bannerId
- break
- case 'wx':
- bannerID = this.wx_info.bannerId
- break
- }
- return bannerID
- }
- /**
- * 获取videoid
- */
- getVideoId(){
- var videoId = ''
- switch(this.platform){
- case 'tt':
- videoId = this.tt_info.videoId
- break
- case 'ks':
- videoId = this.ks_info.videoId
- break
- case 'wx':
- videoId = this.wx_info.videoId
- break
- }
- return videoId
- }
- getVideoId2(){
- if(this.platform == 'wx'){
- var videoId = ''
- videoId = this.wx_info.videoId2
- return videoId
- }
- }
- /**
- * 获取id
- * @param {*012} type
- */
- getNativeAd(type){
- var id = ''
- switch(this.platform){
- case 'tt':
-
- break
- case 'wx':
- if(type == 0){
- id = this.wx_info.NativeAd.juzhen
- }else if(type == 1){
- id = this.wx_info.NativeAd.duogezi
- }else if(type == 2){
- id = this.wx_info.NativeAd.dangezi
- console.log('广告id', id)
- }
- break
- }
- return id
- }
- /**
- * 获取插屏广告id
- */
- getChaPingId(){
- var chapingId = ''
- switch(this.platform){
- case 'tt':
- chapingId = this.tt_info.chapingId
- break
- case 'ks':
- chapingId = this.ks_info.chapingId
- break
- case 'wx':
- chapingId = this.wx_info.chapingId
- break
- }
- return chapingId
- }
- /**
- * 获取分享审核id
- */
- getTemplateId(){
- var templateId = ''
- var id_lenth = this.tt_info.templateId.length
- var randIndex = this.getRangeRandom(0, id_lenth)
- switch(this.platform){
- case 'tt':
- templateId = this.tt_info.templateId[randIndex]
- break
- case 'wx':
- templateId = this.wx_info.templateId[randIndex]
- break
- }
- return templateId
- }
- /**
- * 游戏分享
- */
- game_share(callBack){
- if(this.isdebug) return callBack()
- if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- //console.log('游戏分享')
- var title = this.getShareTitle()
- var templateId = this.getTemplateId()
- this.getPlatformClass().shareAppMessage({
- templateId: templateId, // 替换成通过审核的分享ID
- query: "",
- success() {
- //console.log("分享成功");
- callBack()
- },
- fail(e) {
- //console.log("分享失败");
- }
- });
- }else if(this.platform == 'ks'){
- }
- }
- /**
- * 分享到好友
- * @param callback
- */
- shareGameToFriend(callback, note?:string){
- if(this.isdebug) return callback()
- let notestr = '你的好友邀请你一起战斗'
- if(note) notestr = note
- var self = this
- if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- let title = this.getName()
- this.getPlatformClass().shareAppMessage({
- channel: "invite", // 拉起邀请面板分享游戏好友
- title: title,
- desc: notestr,
- imageUrl: "",
- query: "",
- success() {
- //console.log("分享成功");
- },
- fail(e) {
- //console.log("分享失败");
- },
- });
- }
-
- }
-
- /**
- * banner 广告显示
- */
- bannerAds_Show(){
- if(this.isdebug) return
- var self = this
- if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- if (self.bannerAd == null) {
- let winSize = this.getPlatformClass().getSystemInfoSync();
- let bannerWidth = winSize.windowWidth;
- let bannerHeight = 100;
- // // 广告
- let bannerID = this.getBannerId()
- self.bannerAd = this.getPlatformClass().createBannerAd({
- adUnitId: bannerID,
- style: {
- left: (winSize.windowWidth - bannerWidth) / 2,
- top: winSize.windowHeight - bannerHeight ,
- width: bannerWidth,
- height: bannerHeight,
- }
- })
- self.bannerAd.onResize(res => {
- self.bannerAd.style.left = (winSize.windowWidth - res.width) / 2;
- self.bannerAd.style.top = winSize.windowHeight - res.height ;
- })
- self.bannerAd.onError(function (res) {
- //console.log(res);
- })
- }
- if (self.bannerAd != null) {
- self.bannerAd.show();
- }
- }else if(cc.sys.platform === cc.sys.WECHAT_GAME){
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.brand == 'devtools') return console.log('开发工具不操作')
- if (self.bannerAd == null) {
- self.isshowbanner = false
- let winSize = this.getPlatformClass().getSystemInfoSync();
- let bannerWidth:number = winSize.windowWidth;
- let bannerHeight = 100;
- // // 广告
- let bannerID = this.getBannerId()
- self.bannerAd = this.getPlatformClass().createBannerAd({
- adUnitId: bannerID,
- adIntervals:30, //自动刷新时间 最少30 。 无,不自动
- style: {
- left: (winSize.windowWidth - bannerWidth) / 2,
- top: winSize.windowHeight - bannerHeight ,
- width: bannerWidth,
- height: bannerHeight,
- }
- })
- self.bannerAd.onResize(res => {
- self.bannerAd.style.left = (winSize.windowWidth - res.width) / 2;
- self.bannerAd.style.top = winSize.windowHeight - res.height ;
- })
- self.bannerAd.onError(function (res) {
- console.log('banner失败',res);
- self.bannerAd = null
- //self.bannerAds_Load()
- })
- self.bannerAd.onLoad(() => {
- console.log('banner 广告加载成功')
- //gameConfig.isshowbanner = true
- })
- }
- if (self.bannerAd != null) {
- self.bannerAd.show();
- }
- }
- }
- /**
- * banner 广告隐藏
- */
- bannerAds_Hide(){
- if(this.isdebug) return
- var self = this
- if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.brand == 'devtools' || sysData.appName == 'devtools') return console.log('开发工具不操作')
- if (self.bannerAd != null) {
- self.bannerAd.hide();
- }
- }else if(this.platform == 'ks'){
- }
- }
- /**
- * 插屏广告加载
- */
- chaPingAds_Load(){
- if(cc.sys.platform == cc.sys.WECHAT_GAME){
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.brand == 'devtools') return console.log('开发工具不操作')
- var chapingId = this.getChaPingId()
- this.chaPing = this.getPlatformClass().createInterstitialAd({ adUnitId: chapingId })
- this.chaPing.onError(err => {
- console.log('插屏广告拉取失败',err)
- //gameConfig.chaPing = wx.createInterstitialAd({ adUnitId: chapingId })
- })
- }
- }
- /**
- * 插屏广告显示
- */
- chaPingAds_Show(callback?){
- if(this.isdebug) return
- var self = this
- if(cc.sys.platform == cc.sys.WECHAT_GAME){
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.brand == 'devtools') return console.log('开发工具不操作')
- if(self.chaPingErr == true){
- if(callback){
- this.showToast('资源未准备好...')
- return callback()
- }
-
- }
- self.chaPingErr = true
- setTimeout(()=>{
- self.chaPingErr = false
- },45000)
- self.chaPing.show()
- .then(()=>{
- if(callback){
- callback()
- }
-
- })
- .catch((err) => {
- console.error(err)
- self.showToast('资源未准备好...')
- console.log('展示广告失败')
- if(callback){
- callback()
- }
-
- //gameConfig.chaPing = wx.createInterstitialAd({ adUnitId: this.getChaPingId() })
- })
- }
- else if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.appName == 'devtools') return console.log('插屏开发工具不操作')
- if(self.chaPingErr == true) return
- self.chaPingErr = true
- setTimeout(()=>{
- self.chaPingErr = false
- },60000)
- var chapingId = this.getChaPingId()
- if (this.getPlatformClass().createInterstitialAd) {
- const interstitialAd = this.getPlatformClass().createInterstitialAd({
- adUnitId: chapingId
- });
- interstitialAd
- .load()
- .then(() => {
- interstitialAd.show();
- })
- .catch(err => {
- console.log(err);
- setTimeout(() => {
- interstitialAd.load().then(()=>{interstitialAd.show})
- }, 5);
-
- });
- interstitialAd.onClose(()=>{
- if(interstitialAd){
- interstitialAd.destroy()
- }
-
- })
- }
- }else if(this.platform == 'ks'){
- }
- }
- /**
- * 激励广告加载
- */
- motivational_Video_Load(){
- if(this.isdebug) return
- if(cc.sys.platform == cc.sys.WECHAT_GAME){
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.brand == 'devtools') return console.log('开发工具不操作')
- var self = this
- var videoId = this.getVideoId2()
- self.videoAd = this.getPlatformClass().createRewardedVideoAd({
- adUnitId: videoId,
- //multiton: true
- })
- self.videoAd.onError((res)=>{
- console.log('激励视频错误',res)
- })
- self.videoAd.load()
- var endCallBack = function(res){
- if (res.isEnded == true) {
- if(self.videoCallBack != null){
- console.log('获取奖励')
- self.videoCallBack()
- }
-
- if(this._type != -1){
- self.showToast('已获得')
- }
- } else {
- if (self.videoFailCallBack != null) self.videoFailCallBack()
- }
-
- self.isvideoend = true
- setTimeout(()=>{
- self.checkVideo = false
- },60000)
-
- }
- self.videoAd.onClose(endCallBack); //绑定结束回调
- }
- else if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.appName == 'devtools') return console.log('开发工具不操作')
- var self = this
- var videoId = this.getVideoId()
- this.videoAd = this.getPlatformClass().createRewardedVideoAd({
- adUnitId: videoId,
- //multiton: true
- })
- this.videoAd.onError((res)=>{
- //console.log('激励视频错误',res)
- })
- this.videoAd.load()
- var endCallBack = function(res){
- if (res.isEnded == true) {
- if(self.videoCallBack != null){
- //console.log('获取奖励')
- self.videoCallBack()
- }
-
- if(this._type != -1){
- self.showToast('已获得')
- }
- } else {
- if (self.videoFailCallBack != null) self.videoFailCallBack()
- }
-
- self.isvideoend = true
- }
- self.videoAd.onClose(endCallBack); //绑定结束回调
- }else if(this.platform == 'ks'){
- }
- }
- /**
- * 展示激励视频
- * 参数1 成功回调
- * 参数2 失败回调 可以省略
- */
- motivational_Video_Show(success, faile, type?){
- if(this.isdebug) return success()
- var self = this
- if(this.checkVideo == true) return this.showToast('勿频繁点击')
-
- setTimeout(()=>{
- self.checkVideo = false
- },1000)
- this.videoCallBack = success
- this.videoFailCallBack = faile
- this._type = type
- if(cc.sys.platform == cc.sys.WECHAT_GAME){ //微信
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.brand == 'devtools') return console.log('开发工具不操作')
- this._type = type
- if(self.videoAd == null || self.videoAd == undefined){
- return this.videoCallBack()
- }
-
- self.videoAd.onError((res)=>{
- console.log('广告出错监听', res)
- })
- // /**展示 */
- self.videoAd
- .show()
- .then(() => {
- })
- .catch(err => {
- self.showToast('激励视频播放失败')
- console.log('打印激励视频错误', err)
- // 可以手动加载一次
- self.videoAd.load().then(() => {
- // 加载成功后需要再显示广告
- return self.videoAd.show();
- });
- });
- }
- else if(cc.sys.platform == cc.sys.BYTEDANCE_GAME){
- this.checkVideo = true
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.appName == 'devtools') return success(),console.log('开发工具不操作')
- if(this.videoAd == null || this.videoAd == undefined){
- return this.videoCallBack()
- }
- this.videoAd.onError((res)=>{
- //console.log('广告出错监听', res)
- })
- // /**展示 */
- this.videoAd
- .show()
- .then(() => {
- })
- .catch(err => {
- self.showToast('激励视频播放失败')
- //console.log('打印激励视频错误', err)
- // 可以手动加载一次
- self.videoAd.load().then(() => {
- // 加载成功后需要再显示广告
- return self.videoAd.show();
- });
- });
- }else if(this.platform == 'ks'){
-
- }
- else{
- self.videoCallBack()
- }
- }
-
- /**
- * 录制视频开始
- */
- recording_Video_Start(){
- if(this.isdebug) return
- var self = this
- if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.appName == 'devtools') return console.log('开发工具不操作')
- self.videoRecordingState = 1;
- self.videoIsExist = 0;
- self.video_recorder = this.getPlatformClass().getGameRecorderManager();
-
- self.videoStartTime = Date.parse(new Date().toString()) //Date.parse(new Date());
- //开始回调
- self.video_recorder.onStart(res => {
- //console.log('录屏开始');
- //console.log(res);
- });
- //开始
- self.video_recorder.start({
- duration: self.recorderTime, //录制时长
- });
- //录制结束回调
- self.video_recorder.onStop(res => {
- self.videoRecordingState = 2;
- self.videoPath = res.videoPath;
- //console.log('录屏结束', self.videoPath)
- // do somethine;
- });
- //录制错误回调
- self.video_recorder.onError(res => {
- //console.log("录屏error", res)
- });
- }
- }
- /**
- * 录制视频结束
- */
- recording_Video_End(){
- if(this.isdebug) return
- if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.appName == 'devtools') return console.log('开发工具不操作')
- var self = this
- //console.log('录屏结束');
- //gameConfig.shareVideoTips = 0;
- if (self.video_recorder) {
- var endTime = Date.parse(new Date().toString()) //Date.parse(new Date());
- if ((endTime - self.videoStartTime) / 1000 <= 5) {
- self.videoIsExist = 1;
- }else{
- self.videoIsExist = 0;
- }
- self.video_recorder.stop();
- }
- }
- }
- /**
- * 录制视频分享
- * 参数1 成功回调方法
- * 参数2 失败回调方法
- */
- recording_Video_Share(callBack, failCallBack){
- if(this.isdebug) return callBack()
- if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- let sysData = this.getPlatformClass().getSystemInfoSync()
- if(sysData.appName == 'devtools') return console.log('开发工具不操作')
- var self = this
- if(self.videoIsExist == 1){
- self.showToast('录制时间过短')
- return;
- }
- var result = 200;
- self.videoRecordingState = 3;
- this.getPlatformClass().shareAppMessage({
- channel: 'video',
- title: '',
- imageUrl: '',
- query: '',
- extra: {
- videoPath: self.videoPath, // 可用录屏得到的视频地址
- videoTopics: self.tt_info.videoTopics,
- createChallenge: true
- },
- success() {
- self.showToast('分享视频成功')
- callBack()
- //self.recording_Video_Start()
- },
- fail(e) {
- result = 201;
- self.showToast('分享视频失败')
- //console.log('分享失败', e)
- failCallBack()
- //self.recording_Video_Start()
- }
- })
- }else{
- callBack()
- }
- }
- //录屏是否存在
- getVideoIsExist(){
- return this.videoIsExist
- }
- /**
- * 添加桌面
- * @param scb
- * @param fcb
- */
- addShortcut(scb:Function, fcb:Function){
- this.getPlatformClass().addShortcut({
- success() {
- this.showToast("添加桌面成功");
- scb()
- },
- fail(err) {
- this.showToast("添加桌面失败");
- fcb()
- },
- });
- }
- /**
- * 检测是否添加到桌面
- * @returns true 不支持 或者 已添加
- */
- checkShortcut(){
- if(this.isdebug) return
- if((cc.sys.platform == cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) && cc.sys.os == cc.sys.OS_ANDROID){
- const version = this.getPlatformClass().getSystemInfoSync().SDKVersion
- if (utils.compareVersion(version, '2.46.0') >= 0) {
- this.getPlatformClass().checkShortcut({
- success(res) {
- let exist = res.status.exist
- //console.log("检查快捷方式", res.status);
- return exist
- },
- fail(res) {
- return true
- //console.log("检查快捷方式失败", res.errMsg);
- },
- });
- }else{
- return true
- }
- }else{
- return true
- }
- }
- /**
- * 提示框
- */
- showToast(str){
- if (cc.sys.platform === cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
- this.getPlatformClass().showToast({
- title: str,
- duration: 1000,
- success(res) {
- //console.log(`${res}`);
- },
- fail(res) {
- //console.log(`showToast调用失败`);
- }
- });
- }else{
- console.log(str)
- }
- }
- /**
- * 获取不重复的随机数
- * @param minValue 最小值
- * @param maxValue 最大值
- * @param valueNum 随机个数
- */
- getRandomValueDif(minValue, maxValue, valueNum) {
- // 全部随机数值
- let allNums = new Array;
- // 判断获取随机数个数
- let size = valueNum ? (valueNum > maxValue - minValue + 1 ? maxValue - minValue + 1 : valueNum) : 1;
- // 生成随机数值区间数组
- for (let i = minValue, k = 0; i <= maxValue; i++ , k++) {
- allNums[k] = i;
- }
- let arr = []
- // 随机从数组里面取值
- allNums.sort(function () { return 0.5 - Math.random(); });
- for (let j = 0; j < size; j++) {
- let index = Math.floor(Math.random() * allNums.length);
- arr.push(allNums[index]);
- let tmp = allNums[index];
- allNums[index] = allNums[allNums.length - 1];
- allNums[allNums.length - 1] = tmp;
- allNums.pop();
- }
- return arr;
- }
- /**
- * 获取范围内的随机数
- * @param minValue 最小值
- * @param maxValue 最大值
- */
- getRangeRandom(minValue, maxValue) {
- // 获取数组从第一个开始到指定个数的下标区间
- return this.getRandomValueDif(minValue, maxValue, 1)[0];
- }
- vibrateShort(){
- if(cc.sys.platform == cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME){
- this.getPlatformClass().vibrateShort()
- }
- }
- }
|