cocos2d convertToWorldSpace

后端 未结 2 940
庸人自扰
庸人自扰 2021-01-01 03:02

I\'m not sure convertToWorldSpace works.

  • I have spriteA which is the parent.

  • I set it to position 40,40 for example and add it to the scene.

2条回答
  •  生来不讨喜
    2021-01-01 03:57

    ConvertToWorldSpace takes LOCAL node coordinates, and converts them to the world coordinates. ConvertToNodeSpace takes WORLD coordinates, converts them to the calling node's coordinates. (If so call [nodeA convertToWorldSpace:ccp(10,10)], it assumes that the (10,10) position is for a child of nodeA)

    Basically, To get the world position of any node (in cocos2d 3 or later) use this code:

    CGPoint worldPosition=[node.parent convertToWorldSpace:node.positionInPoints];
    

    I personally made a function so I can use this over and over (add it to the top of any .m/.h file and you will see it)

    static inline CGPoint
    worldPosOfNode(CCNode *node){
        return [node.parent convertToWorldSpace:node.positionInPoints];
    }
    

    And I use it like this: (myNode can be any cocos2d sprite, label or whatever)

    CGPoint worldPosition=worldPosOfNode(myNode);
    

提交回复
热议问题