如何获取某个在层容器中的ui控件在屏幕上的绝对坐标
1个回答
推荐于2016-07-20
展开全部
不嫌弃的话把下面这两个函数放到你的UIWidget里面。试用了一下好像可以,没把握=。=……[code]CCPoint UIWidget::getAbsolutePosition(){
CCPoint ret = getPosition();
CCNode* cobj;
UIWidget* uobj = this;
UIWidget* tuobj;
bool lastIsUI = true;
while(true){
if(lastIsUI){
tuobj = dynamic_cast(uobj->getWidgetParent());
if(tuobj != NULL){
uobj = tuobj;
ret.x += uobj->getPosition().x;
ret.y += uobj->getPosition().y;
lastIsUI = true;
}else{
cobj = dynamic_cast(uobj->getUILayer());
if(cobj == NULL)break;
ret.x += cobj->getPosition().x;
ret.y += cobj->getPosition().y;
lastIsUI = false;
}
}else{
cobj = dynamic_cast(cobj->getParent());
if(cobj == NULL)break;
ret.x += cobj->getPosition().x;
ret.y += cobj->getPosition().y;
}
}
return ret;
}[/code][code]UILayer* UIWidget::getUILayer(){
return m_pUILayer;
}[/code]然后通过getAbsolutePosition就可以获得一个表示坐标的ccpoint了。
CCPoint ret = getPosition();
CCNode* cobj;
UIWidget* uobj = this;
UIWidget* tuobj;
bool lastIsUI = true;
while(true){
if(lastIsUI){
tuobj = dynamic_cast(uobj->getWidgetParent());
if(tuobj != NULL){
uobj = tuobj;
ret.x += uobj->getPosition().x;
ret.y += uobj->getPosition().y;
lastIsUI = true;
}else{
cobj = dynamic_cast(uobj->getUILayer());
if(cobj == NULL)break;
ret.x += cobj->getPosition().x;
ret.y += cobj->getPosition().y;
lastIsUI = false;
}
}else{
cobj = dynamic_cast(cobj->getParent());
if(cobj == NULL)break;
ret.x += cobj->getPosition().x;
ret.y += cobj->getPosition().y;
}
}
return ret;
}[/code][code]UILayer* UIWidget::getUILayer(){
return m_pUILayer;
}[/code]然后通过getAbsolutePosition就可以获得一个表示坐标的ccpoint了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询