Library.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. },
  45. indexOf (arr,item) {
  46. for (let i = 0; i < arr.length; i++) {
  47. if (arr[i] == item) return i;
  48. }
  49. return -1;
  50. },
  51. remove(arr, item) {
  52. let index = arr.indexOf(item);
  53. if (index > -1) {
  54. arr.splice(index, 1);
  55. }
  56. },
  57. setImageBase64(base64Url,callback){
  58. let img = new Image();
  59. img.src = base64Url;
  60. img.onload = function(){
  61. let texture = new cc.Texture2D();
  62. texture.initWithElement(img);
  63. texture.handleLoadedTexture();
  64. if (callback)
  65. callback(texture);
  66. }
  67. },
  68. };
  69. module.exports = library;