Cocos2d-x_位置和锚点

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    // (position)位置和(anchorpoint)锚点,这两个东西必定是重合的,首先是根据setPosition设置好在父节点的位置,锚点再根据位置进行重合旋转
    
    CCSprite *pSpr = CCSprite::create("Icon-57.png");
    pSpr->setPosition(ccp(winSize.width/2.0,winSize.height/2.0));
    pSpr->setAnchorPoint(ccp(0.5,0.5));
    this->addChild(pSpr);
    
    CCLabelTTF *pPositonTTF = CCLabelTTF::create("","Arial",20);
    pPositonTTF->setPosition(ccp(winSize.width/2.0,winSize.height/2.0/2.0));
    this->addChild(pPositonTTF);
    
    CCLabelTTF *pAnchorPointTTF = CCLabelTTF::create("",20);
    pAnchorPointTTF->setPosition(ccp(winSize.width/2.0,winSize.height/2.0/2.0-30));
    this->addChild(pAnchorPointTTF);
    
    CCPoint position = pSpr->getPosition();
    CCString *strPosition = CCString::createWithFormat("Position: x:%.1f,y:%.1f",position.x,position.y);
    pPositonTTF->setString(strPosition->getCString());
    
    CCPoint anchor = pSpr->getAnchorPoint();
    CCString *strAnchor = CCString::createWithFormat("AnchorPoint: x:%.1f,anchor.x,anchor.y);
    pAnchorPointTTF->setString(strAnchor->getCString());
    
    return true;
}


效果图如下:





相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...