zoukankan      html  css  js  c++  java
  • cocos2d-x 类大全及其概要

    • CCNode
    节点类是Cocos2D-x中的主要类,继承自CCObject。
    任何需要画在屏幕上的对象都是节点类。最常用的节点类包括场景类(CCScene)、布景层类(CCLayer)、人物精灵类(CCSprite)、菜单类(CCMenu)
    CCNode类包含的主要功能如下:
    每个节点都可以包含有子节点。
    节点含有周期性的毁掉方法(Schedule、Unschedule等)。
    可以含有动作(CCAction)。
    • CCDirector
    CCDirector类是Cocos2D-x游戏引擎的核心,用来创建并且控制着主屏幕的显示,同时控制场景的显示时间和方式。在整个游戏里一般只有一个导演。游戏开始、结束、暂停都会调用CCDirector类的方法。
    CCDirector类具有如下功能:
    1.初始化OpenGL回话。
    2.设置OpenGL的一些参数和方式。
    3.访问和改变场景以及访问Cocos2D-x的配置细节。
    4.访问视图。
    5.设置投影和朝向
    需要说明一下,CCDirector是单例模式,调用CCDirector的方法标准:CCDirector::sharedDirector()->函数名
    • CCCamera
    CCCamera类可以实现节点对象的缩放和旋转等。
    • CCTouchDispatcher
      1.注册的代理以优先级排序,在addTargetedDelegate()时完成插入,delegate的优先级通过在队列的位置来体现,优先级别高的位置靠前(虽然可以指定优先级数值,但内部没有任何优先级记录),相同优先级的delegates,后插入的位置靠前。
    • CCCardinalSplineBy
      1.这个类是样条曲线动作,其创建函数CCCardinalSplineBy::create(float duration, cocos2d::CCPointArray *points, float tension);中duration是时间间隔,points是控制点列表,tension是松紧程度。tension==1时,样条线是分段直线。tension<1向外松弛弯曲,tension>1向内缩紧弯曲。By动作是以当前坐标为新坐标原点。
    • CCLayer,CCScene
      这两个类最特殊的一点是m_bIgnoreAnchorPoint(2.0.4版本是这名变量名,之前的好像是m_bRelativeToAnchorPoint),其作用是表明在布置CCLayer和CCScene对象时,是否基于AnchorPoint。CCLayer和CCScene中这两个变量都是true(2.0.4的CCNode构造函数中的注释写错了,它居然说CCLayer,CCScene应该设置这个为true)。但即使m_bIgnoreAnchorPoint为true,AnchorPoint在旋转中起到轴心点作用并没有变,所以在CCLayer构造函数中调用了setAnchorPoint( 0.5, 0.5 )来保证中心旋转点。另外我之前在追究m_bIgnoreAnchorPoint的作用时,一直被一段代码困惑,后来弄明白了,这里说一下。
      [cpp]  view plain copy
       
      1. CCAffineTransform CCNode::nodeToParentTransform(void)  
      2. {  
      3.     if (m_bIsTransformDirty)   
      4.     {  
      5.   
      6.         // Translate values  
      7.         float x = m_tPosition.x;  
      8.         float y = m_tPosition.y;  
      9.   
      10.         if (m_bIgnoreAnchorPointForPosition)   
      11.         {  
      12.             x += m_tAnchorPointInPoints.x;  
      13.             y += m_tAnchorPointInPoints.y;  
      14.         }  
      15.   
      16.         // Rotation values  
      17.         float c = 1, s = 0;  
      18.         if (m_fRotation)   
      19.         {  
      20.             float radians = -CC_DEGREES_TO_RADIANS(m_fRotation);  
      21.             c = cosf(radians);  
      22.             s = sinf(radians);  
      23.         }  
      24.   
      25.         bool needsSkewMatrix = ( m_fSkewX || m_fSkewY );  
      26.   
      27.   
      28.         // optimization:  
      29.         // inline anchor point calculation if skew is not needed  
      30.         if (! needsSkewMatrix && !m_tAnchorPointInPoints.equals(CCPointZero))  
      31.         {  
      32.             x += c * -m_tAnchorPointInPoints.x * m_fScaleX + -s * -m_tAnchorPointInPoints.y * m_fScaleY;  
      33.             y += s * -m_tAnchorPointInPoints.x * m_fScaleX +  c * -m_tAnchorPointInPoints.y * m_fScaleY;  
      34.         }  
      35.   
      36.   
      37.         // Build Transform Matrix  
      38.         m_tTransform = CCAffineTransformMake( c * m_fScaleX,  s * m_fScaleX,  
      39.             -s * m_fScaleY, c * m_fScaleY,  
      40.             x, y );  
      41.   
      42.         // XXX: Try to inline skew  
      43.         // If skew is needed, apply skew and then anchor point  
      44.         if (needsSkewMatrix)   
      45.         {  
      46.             CCAffineTransform skewMatrix = CCAffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(m_fSkewY)),  
      47.                 tanf(CC_DEGREES_TO_RADIANS(m_fSkewX)), 1.0f,  
      48.                 0.0f, 0.0f );  
      49.             m_tTransform = CCAffineTransformConcat(skewMatrix, m_tTransform);  
      50.   
      51.             // adjust anchor point  
      52.             if (!m_tAnchorPointInPoints.equals(CCPointZero))  
      53.             {  
      54.                 m_tTransform = CCAffineTransformTranslate(m_tTransform, -m_tAnchorPointInPoints.x, -m_tAnchorPointInPoints.y);  
      55.             }  
      56.         }  
      57.   
      58.         m_bIsTransformDirty = false;  
      59.     }  
      60.   
      61.     return m_tTransform;  
      62. }  
      上述代码中我一直不明白为什么m_bIgnoreAnchorPoint是true的时候,将m_tAnchorPointInPoints的坐标加入了原坐标。
      [cpp]  view plain copy
       
      1. if (m_bIgnoreAnchorPointForPosition)   
      2. {  
      3.     x += m_tAnchorPointInPoints.x;  
      4.     y += m_tAnchorPointInPoints.y;  
      5. }  
      后来才明白,这是为了补偿后面旋转带来的偏差的。
      [cpp]  view plain copy
       
      1. // optimization:  
      2. // inline anchor point calculation if skew is not needed  
      3. if (! needsSkewMatrix && !m_tAnchorPointInPoints.equals(CCPointZero))  
      4. {  
      5.     x += c * -m_tAnchorPointInPoints.x * m_fScaleX + -s * -m_tAnchorPointInPoints.y * m_fScaleY;  
      6.     y += s * -m_tAnchorPointInPoints.x * m_fScaleX +  c * -m_tAnchorPointInPoints.y * m_fScaleY;  
      7. }  
    • CCAction这个类是动作的基类,有点需要注意的就是,我们不光可以通过CCSpawn让动画一起播放,我们在调用runAction的时候本身就是一种一起播放(即在调用runAction的时候如果已经有动画播放,那么新动画和旧动画即将一起播放)
      [cpp]  view plain copy
       
      1.   
    • CCMotionStreak(拖动渐隐效果类)
      这个类是个运动残影功能,拖一个影子在背后。
      static CCMotionStreak* create(float fade, float minSeg, float stroke, ccColor3B color, const char* path);
      fade:残影残存时间。
      misSeg:测试了一会发现没太多感觉。一般设置为3就可以了。
      stroke:残影的宽度。
      color:将会添加再残影纹理上的颜色。
      stroke:是其中的path是纹理路径,

      这个纹理将成为残影,color将会和纹理叠加。值得注意的是,这个类重载了setPosition并使用另外一个坐标变量,所以执行一些位置类运动会诡异的现象,如CCMoveBy,因为这些运动会通过原来的坐标变量来获取目标对象的起始坐标,但原来坐标已经被废弃。
    • CCAnimationCache
      这个类相当于简单的动画管理器,我们将动画加进去之后,以后可以方便的去取。这个函数加载动画的函数中有个比较好的函数:
      void addAnimationsWithFile(const char* plist);
      读取一个属性列表文件,然后根据里面列出的所有动画名称及其相关的序列帧就可以加载多个动画,前提是这些动画的序列帧已经存在于SpriteFrameCache中。
    • CCTouch
      这类中是对当前用户触摸点的封装,但更值得庆幸的是,在一次触摸消息流程中,你能通过这个类获得上一次坐标点,比如用户触摸屏幕,并滑动,最后松开。在这个过程中,你始终能通过getPreviousLocation()获得上一个坐标点。
    • CCRenderTexture
      这个类是个渲染目标体,我们可以通过begin,end函数组织一次绘画。在begin(),end()之间使用节点的visit()函数,即可将元素画到渲染目标体上。这里有一点很重要,所有的渲染默认情况下都是会开启颜色混合的。默认的是GL_ONE, GL_ONE_MINUS_SRC_ALPHA。颜色混合公式中也会作用于alpha值。
  • 相关阅读:
    EntityFramework 启用迁移 EnableMigrations 报异常 "No context type was found in the assembly"
    JAVA 访问FTP服务器示例(2)
    NuGet Package Manager 更新错误解决办法
    JAVA 访问FTP服务器示例(1)
    RemoteAttribute 的使用问题
    诡异的 javascript 变量
    javascript apply用法
    Babun 中文乱码
    GSM呼叫过程
    转站博客园
  • 原文地址:https://www.cnblogs.com/pangblog/p/3301858.html
Copyright © 2011-2022 走看看