Cocos2d-x_位置和锚点

前端之家收集整理的这篇文章主要介绍了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;
}


效果图如下:





原文链接:https://www.f2er.com/cocos2dx/346708.html

猜你在找的Cocos2d-x相关文章