JCLibrary.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**custom method set */
  2. export class CMS {
  3. /**
  4. * find a childNode or childComponent
  5. * @param startNode the root Node
  6. * @param path childPath without root
  7. * @param component return component? if yes,input the type of component
  8. */
  9. public static findChild(startNode:cc.Node,path:string,component?:new()=>cc.Component):any{
  10. let childNames:string[] = path.split('/');
  11. let node:cc.Node = startNode;
  12. childNames.forEach(childName => {
  13. node = node.getChildByName(childName);
  14. });
  15. if(component){
  16. return node.getComponent(component);
  17. }else{
  18. return node;
  19. }
  20. };
  21. }
  22. export class Tool{
  23. /**time format for minute and second */
  24. public static timeFormatMS(millis:number):string{
  25. let minute = Math.floor(millis/1000/60);
  26. let second = Math.floor(millis/1000%60);
  27. return (minute<10?'0':'')+minute+':'+(second<10?'0':'')+second;
  28. }
  29. public static timeFormatHMS(millis:number):string{
  30. let hour = Math.floor(millis/1000/60/60);
  31. let minute = Math.floor(millis/1000/60%60);
  32. let second = Math.floor(millis/1000%60);
  33. return (hour<10?'0':'')+hour+':'+(minute<10?'0':'')+minute+':'+(second<10?'0':'')+second;
  34. }
  35. public static decimal(value:number,count:number):number {
  36. let str = value.toString();
  37. let pointIndex = str.indexOf('.');
  38. if(pointIndex>-1){
  39. return parseFloat(str.substring(0,pointIndex+count+1));
  40. }else{
  41. return value;
  42. }
  43. }
  44. public static openInWebview () {
  45. let ua = navigator.userAgent.toLowerCase()
  46. if (ua.match(/MicroMessenger/i) != null) { // 微信浏览器判断
  47. return false
  48. } else if (ua.match(/QQ/i) != null) { // QQ浏览器判断
  49. return false
  50. } else if (ua.match(/WeiBo/i) != null) {
  51. return false
  52. } else {
  53. if (ua.match(/Android/i) != null) {
  54. return ua.match(/browser/i) == null
  55. } else if (ua.match(/iPhone/i) != null) {
  56. return ua.match(/safari/i) == null
  57. } else {
  58. return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null)
  59. }
  60. }
  61. }
  62. }