【问题标题】:Cocos2d: how can I find the center point of a CCSprite?Cocos2d:如何找到 CCSprite 的中心点?
【发布时间】:2012-11-02 17:25:16
【问题描述】:

如何找到 CCSprite 的中心点?

我的伪代码是:

A get sprite bounding box
B compute center point of the sprite
C move center point relative to sprite position and anchor point (this will be the center point of the sprite)

到目前为止,我的伪解决方案是这样的:

A [精灵边界框] 乙How to find the Center Coordinate of Rectangle? C 我不知道:)

有什么建议吗?

【问题讨论】:

  • 看不懂C点下的意思。
  • @Morion 我只是假设要获得精灵的精确中心,我需要相对于它的锚点计算它(比如锚点是 (0,0) 而不是传统的 (0.5 , 0.5) ,但我不确定。

标签: ios math cocos2d-iphone sprite


【解决方案1】:

精灵的中心点:

sprite.position;

这是假设 anchorPoint 是默认值,即 0.5,0.5,强烈建议不要更改它,因为它会让你处于你现在所处的这种情况。

不要更改精灵的锚点,而是将精灵作为子对象添加到 CCNode 对象,并从 CCNode 偏移精灵。从那时起,通过更改节点的位置属性来移动或重新定位精灵。

【讨论】:

    【解决方案2】:
    CGPoint spriteCenter = CGPointApplyAffineTransform(ccp(sprite.contentSize.width/2, sprite.contentSize.height/2),
                                                       sprite.nodeToParentTransform);
    

    【讨论】:

      【解决方案3】:

      默认情况下,任何精灵的锚点都是它的中心点,只需编写 mySprite.position 即可根据 CGPoint 给出中心点。您可以使用 mySprite.position.x 和 mySprite.position 找出 x 和 y 坐标.y.还可以使用代码将任何精灵的锚点从其中心移动到任何所需的点

      mySprite.setAnchorPoint=ccp(0,0);// left down corner as anchor point here
      

      您可以获取并打印任何精灵的中心点

      NSLog("x=%d  ,y=%d",mySprite.position.x,mySprite.position.y);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多