CocoStudio中所有UI控件的基类都是Widget,我想在这里分析一下这个类的
一些常用方法:
1、继承class CC_EX_DLL Widget : public CCNodeRGBA
2、addChild和addNode方法:
这连个方法不同,addChild只能添加继承自Widget的控件,如Button等,而addNode可以添加不是继承自
Widget的空间,如Node,layer等等。
而且这里这几到两个成员变量CCArray* _widgetChildren(用于保存Widget*)和
CCArray* _nodes(用于保存Node),但是最终都会添加到Widget继承的的Node上。因为可以添加
两种类型的UI控件,所以很多方法都需要分别处理。所以我们在使用的时经常需要区别对待。
void Widget::addChild(CCNode* child, int zOrder, int tag)
{
CCAssert(dynamic_cast<Widget*>(child) != NULL, "Widget only supports Widgets as children");
CCNode::addChild(child, zOrder, tag);
_widgetChildren->addObject(child);
}
void Widget::addNode(CCNode* node, int zOrder, int tag)
{
CCAssert(dynamic_cast<Widget*>(node) == NULL, "Widget only supports Nodes as renderer");
CCNode::addChild(node, zOrder, tag);
_nodes->addObject(node);
}
3、
CCNode* Widget::getChildByTag(int aTag)
{
CCAssert( aTag != kCCNodeTagInvalid, "Invalid tag");
//只能获取_widgetChildren中保存的UI控件,也即是通过
//addChild方法添加的控件。
if(_widgetChildren && _widgetChildren->count() > 0)
{
CCObject* child;
CCARRAY_FOREACH(_widgetChildren, child)
{
CCNode* pNode = (CCNode*) child;
if(pNode && pNode->getTag() == aTag)
return pNode;
}
}
return NULL;
}
CCNode* Widget::getNodeByTag(int tag)
{
CCAssert( tag != kCCNodeTagInvalid, "Invalid tag");
//只能获取_nodes-中保存的UI控件,也即是通过
//addNode方法添加的控件。
if(_nodes && _nodes->count() > 0)
{
CCObject* renderer;
CCARRAY_FOREACH(_nodes, renderer)
{
CCNode* pNode = (CCNode*) renderer;
if(pNode && pNode->getTag() == tag)
return pNode;
}
}
return NULL;
}
4、
void Widget::removeChild(CCNode *child, bool cleanup)
{
CCNode::removeChild(child, cleanup);
_widgetChildren->removeObject(child);
}
void Widget::removeNode(CCNode* node)
{
CCNode::removeChild(node);
_nodes->removeObject(node);
}
void Widget::removeAllChildren()
void Widget::removeAllNodes()
5、移除全部,包括添加的Widget和Node
/**
* Removes this node itself from its parent node with a cleanup.
* If the node orphan, then nothing happens.
* @see `removeFromParentAndCleanup(bool)`
*/
virtual void removeFromParent();
6、触摸响应相关:
virtual bool onTouchBegan(CCTouch *touch, CCEvent *unused_event);
virtual void onTouchMoved(CCTouch *touch, CCEvent *unused_event);
virtual void onTouchEnded(CCTouch *touch, CCEvent *unused_event);
virtual void onTouchCancelled(CCTouch *touch, CCEvent *unused_event);版权声明:本文为tianxiawuzhei原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。