Cocos2d-x學(xué)習(xí)筆記之CCScene、CCLayer、CCSprite的默認(rèn)坐標(biāo)和默認(rèn)錨點實驗
結(jié)論:實踐證明這三個東西的默認(rèn)坐標(biāo)都是0,0 默認(rèn)錨點都是0.5,0.5。

bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCSprite * sprite = CCSprite::create("image1.png");
this->addChild(sprite);
//獲得CCSprite的默認(rèn)坐標(biāo),CCSprite設(shè)置坐標(biāo)的時候是用自己的錨點位置占據(jù)在坐標(biāo)上的。
CCLog("CCSprite.x=%f,CCSprite.y=%f",sprite->getPosition().x,sprite->getPosition().y);
//獲得CCSprite的默認(rèn)錨點
CCLog("sprite1Anchor.x=%f,sprite1Anchor.y=%f",sprite->getAnchorPoint().x,sprite->getAnchorPoint().y);
//獲得CCLayer的默認(rèn)坐標(biāo)
CCLog("CCLayer.x=%f,CCLayer.y=%f",this->getPosition().x,this->getPosition().y);
//獲得CCLayer的默認(rèn)錨點
CCPoint point = this->getAnchorPoint();
CCLog("layerAnchor.x=%f,layerAnchor.y=%f",point.x,point.y);
bRet = true;
} while (0);
return bRet;
}
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
layer->show();
} while (0);
// return the scene
return scene;
}
void HelloWorld::show()
{
//獲得CCScene的默認(rèn)坐標(biāo)
CCScene * scene = (CCScene *)this->getParent();
CCLog("CCScene.x=%f,CCScene.y=%f",scene->getPosition().x,scene->getPosition().y);
//獲得CCScene的默認(rèn)錨點
CCPoint point = scene->getAnchorPoint();
CCLog("SceneAnchor.x=%f,SceneAnchor.y=%f",point.x,point.y);
}
這里有一點非常值得注意,就是函數(shù)ignoreAchorPointForPosition(),這個函數(shù)的意 思是在設(shè)置節(jié)點(包括CCScene,CCLayer,CCSprite)的坐標(biāo)的時候是否忽略錨點,我們都知道在設(shè)置坐標(biāo)的時候就是用錨點去占據(jù)坐標(biāo)的位置,如果忽略了錨點,那么就是以左下角為占據(jù)坐標(biāo)的那個位置,或者說錨點就是左下角的點。 查詢官方的API得到的結(jié)論是:This is an internal method, only used by CCLayer and CCScene. Don't call it outside framework. The default value is false, while in CCLayer and CCScene are true。這個函數(shù)的默認(rèn)值是false,但是對于CCScene和CCLayer它的值是true。如果是這樣的話,我們在設(shè)置CCScene和CCLayer的坐標(biāo)的時候就是以 左下角為錨點的,而不是(0.5,0.5)。但是對于CCSprite來說正好是相反的,它對于這個函數(shù)的默認(rèn)值就是false,也就是錨點就是它 本身的默認(rèn)錨點(0.5,0.5)。
相關(guān)文章
淺談C結(jié)構(gòu)和C++結(jié)構(gòu)之間的區(qū)別
這篇文章主要介紹了淺談C結(jié)構(gòu)和C++結(jié)構(gòu)之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
C++通過msxml調(diào)用webservice示例分享
這篇文章主要介紹了C++通過msxml調(diào)用webservice示例分享,需要的朋友可以參考下2014-03-03
C++實現(xiàn)LeetCode(154.尋找旋轉(zhuǎn)有序數(shù)組的最小值之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(154.尋找旋轉(zhuǎn)有序數(shù)組的最小值之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

