• cocos2d-x 几何绘制: DrawingPrimitives 和 CCDrawNode


      在看书的时候只提到了DrawingPrimitives,然后我去搜索这个类,结果没搜到。心想难道是类名改了,那我搜方法名吧,搜了下DrawLine,果然被我搜到了。结果发现原来这些各方法都是全局函数,而没有被封装到类里做静态函数,囧,果然不能全相信书上的。

      同时看到源代码中有注释指名建议使用CCDrawNode来代替,于是顺便看了下CCDrawNode的源代码,发现函数比较少,要传递的参数比较多。当时心想那我为什么不直接使用函数名称简单易懂的DrawingPrimitives系列函数呢,然后去做了下实验,发现在helloworldscene函数里调用ccDrawLine竟然没有画出来。好吧,再仔细看了下说明,原来DrawingPrimitives系列的函数是必须在draw函数中调用才生效的,并且它们不是批处理的,效率比不上CCDrawNode,怪不得推荐使用CCDrawNode呢。

      

      吸取教训后,就去尝试使用CCDrawNode画了点几何图形。最终想到将来可能会需要显示某个结点对象的框架位置,来查看是否出错等。于是补上两个函数来做辅助,代码如下:

     1 static int giFrameTag = 1012;
     2 static const float gfFrameWidth = 1.0f;
     3 static const ccColor4F gdFrameColor = ccc4f(0.0f, 0.0f, 1.0f, 1.0f);
     4 void DrawNodeFrame(CCNode* npNode)
     5 {
     6     CCAssert(NULL != npNode, "DrawNodeFrame function can`t accept null attribute");
     7 
     8     // 画框架
     9     CCDrawNode* lpoDrawNode = CCDrawNode::create();
    10     CCPoint ldLeftBottomPt = ccp(npNode->getPositionX() + gfFrameWidth,
    11         npNode->getPositionY() + gfFrameWidth);
    12     CCPoint ldRightBottomPt = ccp(npNode->getPositionX() + npNode->getContentSize().width - gfFrameWidth,
    13         npNode->getPositionY() + gfFrameWidth);
    14     CCPoint ldLeftTopPt = ccp(npNode->getPositionX() + gfFrameWidth,
    15         npNode->getPositionY() + npNode->getContentSize().height - gfFrameWidth);
    16     CCPoint ldRightTop = ccp(npNode->getPositionX() + npNode->getContentSize().width - gfFrameWidth, 
    17         npNode->getPositionY() + npNode->getContentSize().height - gfFrameWidth);
    18 
    19     CCPoint ldPoints[] = {ldLeftBottomPt, ldRightBottomPt, ldRightTop, ldLeftTopPt};
    20     lpoDrawNode->drawPolygon(ldPoints, sizeof(ldPoints)/sizeof(ldPoints[0]), 
    21         ccc4f(0.0f, 0.0f, 0.0f, 0.0f), gfFrameWidth, gdFrameColor);
    22 
    23     // 获取当前子节点中最大的zorder
    24     int liMaxZorder = 0;
    25     if(npNode->getChildrenCount() > 0)
    26     {
    27         CCArray* lpoChildrenArray = npNode->getChildren();
    28         int liCurrentZorder = 0;
    29         for(int i = 0; i < lpoChildrenArray->count(); ++i)
    30         {
    31             liCurrentZorder = ((CCNode*)lpoChildrenArray->objectAtIndex(i))->getZOrder();
    32             if(liMaxZorder < liCurrentZorder)
    33                 liMaxZorder = liCurrentZorder;
    34         }
    35     }
    36     liMaxZorder += 1;
    37 
    38     // 获取一个不存在的Tag
    39     while(NULL != npNode->getActionByTag(giFrameTag))
    40     {
    41         ++giFrameTag;
    42     }
    43 
    44     npNode->addChild(lpoDrawNode, liMaxZorder, giFrameTag);
    45 }
    46 void UndrawNodeFrame(CCNode* npNode)
    47 {
    48     if(NULL != npNode && NULL != npNode->getActionByTag(giFrameTag))
    49     {
    50         npNode->removeChildByTag(giFrameTag, true);
    51     }
    52 }

    功能并不复杂,在需要显示的结点对象中调用DrawNodeFrame并传入对应的指针即可(最好是在增加完所有的子对象之后才调用),不需要显示了则调用UndrawNodeFrame即可。

  • 相关阅读:
    [Leetcode] Convert Sorted List to Binary Search Tree
    [Leetcode] Sqrt(x)
    [Leetcode] Pow(x, n)
    [Leetcode] Balanced Binary Tree
    [Leetcode] Convert Sorted Array to Binary Search Tree
    [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
    [Leetcode] Remove Element
    [Leetcode] Letter Combinations of a Phone Number
    [Leetcode] Generate Parentheses
    [Leetcode] Valid Parentheses
  • 原文地址:https://www.cnblogs.com/monotone/p/3405743.html
Copyright © 2020-2023  润新知