閱讀914 返回首頁    go 阿裏雲 go 技術社區[雲棲]


cocos2d-x中Node中重要的屬性

Node還有兩個非常重要的屬性position和anchorPoint。

position(位置)屬性是Node對象的實際位置。position屬性往往還要配合使用anchorPoint屬性為了將一個Node對象標準矩形圖形精準的放置在屏幕某一個位置上需要設置該矩形的錨點anchorPoint是相對於position的比例默認是(0.5,0.5)。我們看看下麵的幾種情況

以anchorPoint為(0.5,0.5)為例這是默認情況。


下麵是anchorPoint為(0.0,0.0)情況。


下麵是anchorPoint為(1.0,1.0)情況。


下麵是anchorPoint為(0.5,0.66)情況。


為了進一步了解anchorPoint使用我們修改HelloWorld實例修改HelloWorldScene.cpp的HelloWorld::init()函數如下其中加粗字體顯示的是我們添加的代碼。

bool HelloWorld::init()
{
   … …
 
   auto label = LabelTTF::create("Hello World","Arial", 24);                                                 
   label->setPosition(Point(origin.x + visibleSize.width/2,
                            origin.y +visibleSize.height - label->getContentSize().height));      
   
label->setAnchorPoint( Point(1.0, 1.0) );
 
   this->addChild(label, 1);                                                                                                               
 
   auto sprite = Sprite::create("HelloWorld.png");                                                                          
   sprite->setPosition(Point(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));    
   this->addChild(sprite, 0);                                                                                                             
   
   return true;
}


Hello World設置了anchorPoint為(1.0,1.0)。


更多內容請關注最新Cocos圖書《Cocos2d-x實戰 C++卷》
本書交流討論網站https://www.cocoagame.net
更多精彩視頻課程請關注智捷課堂Cocos課程https://v.51work6.com
歡迎加入Cocos2d-x技術討論群257760386


《Cocos2d-x實戰 C++卷》現已上線各大商店均已開售

京東https://item.jd.com/11584534.html

亞馬遜https://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

當當https://product.dangdang.com/23606265.html

互動出版網https://product.china-pub.com/3770734

《Cocos2d-x實戰 C++卷》源碼及樣章下載地址

源碼下載地址https://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

樣章下載地址https://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

歡迎關注智捷iOS課堂微信公共平台

最後更新:2017-04-03 12:56:32

  上一篇:go 如何等待java線程池中所有任務完成
  下一篇:go iOS如何把導航默認的返回按鈕設置成“返回”