如果Node的實際類型是Layer或者其派生類,
setPosition是不是有貓膩?
std::string menuImage = "menu.png"; auto menuItem = MenuItemImage::create(menuImage,menuImage,NULL,this); auto menu = Menu::create(menuItem,NULL); auto menuItemPoint = Point(...,...); //具體按鈕位置是由menuItem的setPosition調用決定。而不是Menu的setPosition. menuItem->setPosition(menuItemPoint); //貌似設置menu的anchor沒作用 //menu->setAnchorPoint(Point::ZERO); //這個setPosition ....打醬油?。可又是,去掉不顯示. //但又不是按menu的anchor點顯示在父Layer的ZERO位置 menu->setPosition(Point::ZERO); //添加到父Layer中 addChild(menu,1);
新建一個Layer對象,調用其 getContentSize(),返回是(0,0)
auto label = LabelTTF::create( "left_bottom", "Arial", 24); label->setPosition(Point::ZERO); label->setAnchorPoint(Point::ZERO); addChild(label);
label 卻會按anchor正常顯示在左下角..
----------
在一個Layer的anchor放置一個精靈,將這個Layer設置到父Layer的ZERO位置,精靈卻不在ZERO位置
auto layer = Layer::create(); auto layersize = layer->getContentSize(); auto layerAnchor = layer->getAnchorPoint(); auto sprite = Sprite::create(s_pathClose); auto anchorPoint = Point(layersize.width * layerAnchor.x,layersize.height * layerAnchor.y); sprite->setPosition(anchorPoint); layer->addChild(sprite); layersize = layer->getContentSize(); //Point layPoint = Point::ZERO; layer->setPosition(Point::ZERO); addChild(layer);
這篇博文 :http://blog.csdn.net/xuguangsoft/article/details/8425623
有講Layer的anchor是左下角,一切就都對上了,但是Layer::getAnchorPoint()返回的是(0.5,0.5)..