Library.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. let library = {
  2. //数组元素位置替换
  3. swapArray: function(arr, index1, index2) {
  4. arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  5. return arr;
  6. },
  7. swapLeft : function(arr) {
  8. let element = arr.shift();
  9. arr.push(element);
  10. },
  11. //生成指定范围随机整数
  12. randomFromIntRange: function(min, max) {
  13. return Math.floor(Math.random() * (max - min)) + min;
  14. },
  15. happenRate: function(rate) {
  16. let result = this.randomFromIntRange(0,100);
  17. if(result<rate)
  18. {
  19. return true;
  20. }
  21. else
  22. {
  23. return false;
  24. }
  25. return;
  26. },
  27. openInWebview () {
  28. // let ua = navigator.userAgent.toLowerCase()
  29. // if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信浏览器判断
  30. // return false
  31. // } else if (ua.match(/QQ/i) == 'qq') { // QQ浏览器判断
  32. // return false
  33. // } else if (ua.match(/WeiBo/i) == "weibo") {
  34. // return false
  35. // } else {
  36. // if (ua.match(/Android/i) != null) {
  37. // return ua.match(/browser/i) == null
  38. // } else if (ua.match(/iPhone/i) != null) {
  39. // return ua.match(/safari/i) == null
  40. // } else {
  41. // return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null)
  42. // }
  43. // }
  44. let sUserAgent = navigator.userAgent.toLowerCase()
  45. let bIsMobile = /AppleWebKit.*Mobile/i.test(navigator.userAgent)
  46. let bIsIpad = sUserAgent.match(/ipad/i) !== null
  47. let bIsIphoneOs = sUserAgent.match(/iphone os/i) !== null
  48. let bIsMidp = sUserAgent.match(/midp/i) !== null
  49. let bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) !== null
  50. let bIsUc = sUserAgent.match(/ucbrowser/i) !== null
  51. let bIsAndroid = sUserAgent.match(/android/i) !== null
  52. let bIsCE = sUserAgent.match(/windows ce/i) !== null
  53. let bIsWM = sUserAgent.match(/windows mobile/i) !== null
  54. if (
  55. bIsMobile ||
  56. bIsIpad ||
  57. bIsIphoneOs ||
  58. bIsMidp ||
  59. bIsUc7 ||
  60. bIsUc ||
  61. bIsAndroid ||
  62. bIsCE ||
  63. bIsWM
  64. ) {
  65. return false;
  66. } else {
  67. return true;
  68. }
  69. },
  70. indexOf (arr,item) {
  71. for (let i = 0; i < arr.length; i++) {
  72. if (arr[i] == item) return i;
  73. }
  74. return -1;
  75. },
  76. remove(arr, item) {
  77. let index = arr.indexOf(item);
  78. if (index > -1) {
  79. arr.splice(index, 1);
  80. }
  81. },
  82. setImageBase64(base64Url,callback){
  83. let img = new Image();
  84. img.src = base64Url;
  85. img.onload = function(){
  86. let texture = new cc.Texture2D();
  87. texture.initWithElement(img);
  88. texture.handleLoadedTexture();
  89. if (callback)
  90. callback(texture);
  91. }
  92. },
  93. };
  94. module.exports = library;