Search code examples
cocos2d-x

Why do we divide by 2 getContentSize?


In sample from Cocos2d-x Tutorials when we describe actionMove

CCFiniteTimeAction* actionMove = 
CCMoveTo::create( (float)actualDuration, 
ccp(0 - target->getContentSize().width/2, actualY) );

we set a point ccp(0 - target->getContentSize().width/2, actualY). If we have target with 20 width then we have point (-10, actualY), and half target must be visible, but it's not so. Why?

Initial target position

target->setPosition( 
  ccp(winSize.width + (target->getContentSize().width/2), 
  actualY) );

Here we also divide by 2, but I understand it (winSize.width + any number and target becomes invisible, outside the screen).


Solution

  • Cocos2d-x uses the center of the object as the origin/anchor point, rather than the corner. So if you want half of your object to be visible on the edge of the screen, use:

    ccp(0, actualY)
    

    or

    ccp(winSize.width)
    

    You add half of the contentSize if you want the object to be completely off the screen.