用于处理点而不是像素的CCDirector/CCNode类别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于处理点而不是像素的CCDirector/CCNode类别相关的知识,希望对你有一定的参考价值。

  1. #pragma mark CCDirector Category for Point Handling
  2.  
  3.  
  4. #import "cocos2d.h"
  5.  
  6.  
  7. @interface CCDirector (Points)
  8.  
  9. - (CGSize) ptWinSize;
  10.  
  11. - (CGPoint) convertToPt:(CGPoint)point;
  12. - (CGPoint) convertToPx:(CGPoint)point;
  13.  
  14. @end
  15.  
  16.  
  17. @implementation CCDirector (Points)
  18.  
  19. - (CGSize) ptWinSize {
  20. CGSize s = [self winSize];
  21. CGPoint p = [self convertToPt:ccp(s.width, s.height)];
  22. return CGSizeMake(p.x, p.y);
  23. }
  24.  
  25.  
  26. - (CGPoint) convertToPt:(CGPoint)point {
  27. return ccpMult(point, 1.0/self.contentScaleFactor);
  28. }
  29.  
  30. - (CGPoint) convertToPx:(CGPoint)point {
  31. return ccpMult(point, self.contentScaleFactor);
  32. }
  33.  
  34. @end
  35.  
  36.  
  37. #pragma mark CCNode Category for Point Handling
  38.  
  39.  
  40. @interface CCNode (Points)
  41.  
  42. @property (assign) CGPoint ptPosition;
  43. @property (assign) CGSize ptContentSize;
  44.  
  45. - (CGPoint) ptConvertToNodeSpace:(CGPoint)point;
  46. - (CGPoint) ptConvertToWorldSpace:(CGPoint)point;
  47.  
  48. @end
  49.  
  50.  
  51. @implementation CCNode (Points)
  52.  
  53. - (void) setPtPosition:(CGPoint)pos {
  54. self.position = [[CCDirector sharedDirector] convertToPx:pos];
  55. }
  56.  
  57. - (CGPoint) ptPosition {
  58. return [[CCDirector sharedDirector] convertToPt:self.position];
  59. }
  60.  
  61. - (void) setPtContentSize:(CGSize)size {
  62. CGPoint p = [[CCDirector sharedDirector] convertToPx:ccp(size.width, size.height)];
  63. self.contentSize = CGSizeMake(p.x, p.y);
  64. }
  65.  
  66. - (CGSize) ptContentSize {
  67. CGPoint p = [[CCDirector sharedDirector] convertToPt:ccp(self.contentSize.width, self.contentSize.height)];
  68. return CGSizeMake(p.x, p.y);
  69. }
  70.  
  71. - (CGPoint) ptConvertToNodeSpace:(CGPoint)point {
  72. point = [[CCDirector sharedDirector] convertToPx:point];
  73. point = [self convertToNodeSpace:point];
  74. point = [[CCDirector sharedDirector] convertToPt:point];
  75. return point;
  76. }
  77.  
  78. - (CGPoint) ptConvertToWorldSpace:(CGPoint)point {
  79. point = [[CCDirector sharedDirector] convertToPx:point];
  80. point = [self convertToWorldSpace:point];
  81. point = [[CCDirector sharedDirector] convertToPt:point];
  82. return point;
  83. }
  84.  
  85. @end

以上是关于用于处理点而不是像素的CCDirector/CCNode类别的主要内容,如果未能解决你的问题,请参考以下文章

C++ %f 点而不是逗号

C++ %f 点而不是逗号

围绕一个点而不是中心旋转位图到位图

如何在 UIButton 上设置宽度约束

为啥 WPF 定义它自己的类型来表示二维空间中的点而不是使用 System.Numerics.Vector2

无论语言环境如何,我如何强制使用点而不是逗号的 Swift Decimal Pad?