o0CC.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var o0 = require('o0');
  2. module.exports = {
  3. /*
  4. destroyNode: function(node) {
  5. node.active = false;
  6. var components = node.getComponents();
  7. for(var i = 0;i<components.length;++i){
  8. components[i].destroy();
  9. }
  10. node.destroy();
  11. },/* */
  12. addScriptNode: function(parentNode,script) {
  13. var zIndex;
  14. switch(arguments.length) {
  15. default:
  16. case 2:
  17. zIndex = 0;
  18. break;
  19. case 3:
  20. zIndex = arguments[2];
  21. break;
  22. }
  23. var node = new cc.Node();
  24. var scriptComponent = node.addComponent(script);
  25. parentNode.addChild(node,zIndex);
  26. return scriptComponent;
  27. },
  28. addSpriteNode: function(parentNode,imageNameInResourceFile) {
  29. var zIndex;
  30. switch(arguments.length) {
  31. default:
  32. case 2:
  33. zIndex = 0;
  34. break;
  35. case 3:
  36. zIndex = arguments[2];
  37. break;
  38. }
  39. var node = new cc.Node();
  40. parentNode.addChild(node,zIndex);
  41. var sprite = node.addComponent(cc.Sprite);
  42. cc.loader.loadRes(imageNameInResourceFile, cc.SpriteFrame, function (err, spriteFrame) {
  43. sprite.spriteFrame = spriteFrame;
  44. });
  45. return sprite;
  46. },
  47. addGraphicNode: function(parentNode) {
  48. var zIndex;
  49. switch(arguments.length) {
  50. default:
  51. case 1:
  52. zIndex = 0;
  53. break;
  54. case 2:
  55. zIndex = arguments[1];
  56. break;
  57. }
  58. var node = new cc.Node();
  59. parentNode.addChild(node,zIndex);
  60. var component = node.addComponent(cc.Graphics);
  61. return component;
  62. },
  63. rotationFromVector: function(modVector){//rotation between 0-360
  64. var rotation = modVector.angle(new o0.Vector2(1,0));
  65. if(modVector.y > 0)
  66. rotation = 360-rotation;
  67. return rotation;
  68. },
  69. vectorFromRotation: function(rotation){//rotation between 0-360
  70. var radian = rotation/180*Math.PI;
  71. if(rotation<90)
  72. return new o0.Vector2(Math.cos(radian),-Math.sin(radian));
  73. if(rotation<180){
  74. radian = Math.PI - radian;
  75. return new o0.Vector2(-Math.cos(radian),-Math.sin(radian));
  76. }
  77. if(rotation<270){
  78. radian -= Math.PI;
  79. return new o0.Vector2(-Math.cos(radian),Math.sin(radian));
  80. }
  81. radian = Math.PI*2 - radian;/**/
  82. return new o0.Vector2(Math.cos(radian),Math.sin(radian));
  83. },
  84. nextRotation: function(currentRotation, targetRotation, turningSpeed){//rotation between 0-360
  85. var rotationDifference = currentRotation - targetRotation;
  86. if(Math.abs(rotationDifference) <= turningSpeed
  87. ||Math.abs(rotationDifference+360) <= turningSpeed
  88. ||Math.abs(rotationDifference-360) <= turningSpeed)
  89. currentRotation = targetRotation;
  90. else if((rotationDifference > 0 && rotationDifference < 180)
  91. ||(rotationDifference > - 360 && rotationDifference < -180))
  92. currentRotation -= turningSpeed;
  93. else
  94. currentRotation += turningSpeed;
  95. if(currentRotation >= 360)
  96. currentRotation -= 360;
  97. if(currentRotation < 0)
  98. currentRotation += 360;
  99. return currentRotation;
  100. },
  101. setGroup:function(component,groupIndex){
  102. if(component.node.groupIndex==groupIndex){
  103. return;
  104. }
  105. component.node.active = false;
  106. component.scheduleOnce(() => {
  107. if(component!=null&&component.node!=null){
  108. component.node.groupIndex = groupIndex;
  109. component.node.active = true;
  110. }
  111. }, 0.1);/*用来解决cocos动态改变group的bug */
  112. },
  113. randomBrightColor:function(){
  114. var c = new cc.Color(Math.random()*255, Math.random()*255, Math.random()*255, 255);
  115. var rate = 255.0 / Math.max(c.getR(),c.getG(),c.getB(),c.getA());
  116. return new cc.Color(c.getR()*rate,c.getG()*rate,c.getB()*rate,255);
  117. },
  118. };