• ----------------------------------------

  • 入口类main.cpp

  • 主要控制类AppDelegate.cpp

  • ----------------------------------------

  • 对象类CCObject

  • 节点类CCNode

  • ----------------------------------------

  • 导演类CCDirector

  • 场景类CCScene

  • 图层类CCLayer

  • 精灵类CCSprite

  • ----------------------------------------

  • 坐标类CCPoint

  • 尺寸大小类CCSize

  • 矩形类CCRect

  • ----------------------------------------

  • 数组类CCArray

  • ----------------------------------------

入口类main.cpp

这是应用程序的入口类,用于创建cocos2dx的AppDelegate实例、窗口大小、以及运行程序。

主要代码如下:

  1. // 创建一个主控制类AppDelegate
  2. AppDelegate app;
  3. // 使用OpenGL进行图形渲染
  4. CCEGLView* eglView = CCEGLView::sharedOpenGLView();
  5. // 窗口名
  6. eglView->setViewName("CocosStudy");
  7. // 窗口大小
  8. eglView->setFrameSize(480, 320);
  9. // 运行
  10. return CCApplication::sharedApplication()->run();

主要控制类AppDelegate.cpp

游戏的入口,用于游戏的初始化,并创建第一个游戏界面。

里面有3个方法:

  1. // 初始化
  2. virtual bool applicationDidFinishLaunching();
  3. // 切换到后台
  4. virtual void applicationDidEnterBackground();
  5. // 切换到前台
  6. virtual void applicationWillEnterForeground();

源码:

  1. // 初始化
  2. bool AppDelegate::applicationDidFinishLaunching() {
  3. // 创建导演
  4. CCDirector* pDirector = CCDirector::sharedDirector();
  5. // 使用OpenGLView
  6. pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
  7. // 设置游戏的设计分辨率
  8. CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionShowAll);
  9. // 关闭帧数显示
  10. pDirector->setDisplayStats(false);
  11. // 刷新频率,每秒60帧
  12. pDirector->setAnimationInterval(1.0 / 60);
  13. // 创建一个场景HelloWorld,游戏程序的第一个界面
  14. CCScene *pScene = HelloWorld::scene();
  15. // 运行场景
  16. pDirector->runWithScene(pScene);
  17. return true;
  18. }
  19. // 切换到后台
  20. void AppDelegate::applicationDidEnterBackground() {
  21. // 暂停游戏
  22. CCDirector::sharedDirector()->stopAnimation();
  23. // 暂停音乐,需要时打开
  24. //SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  25. }
  26. // 切换到前台
  27. void AppDelegate::applicationWillEnterForeground() {
  28. // 游戏恢复
  29. CCDirector::sharedDirector()->startAnimation();
  30. // 音乐恢复,需要时打开
  31. //SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  32. }

在main.cpp中,我们设置了分辨率“eglView->setFrameSize(480, 320)”(1),在这里我们又设置了分辨率“CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480,320,kResolutionShowAll)”(2),但是它们的意义不同。(1)是根据我们的期望屏幕大小设置的,(2)是设计时的游戏分辨率,我们最初的设计是在电脑上完成的,这个大小是在电脑上显示的大小,但是游戏移植到手机后,手机型号不同,所以后面的参数kResolutionShowAll表示按照原比例(480x320)进行缩放来适配手机屏幕。

如果图片的大小为1x1,setFrameSize(1, 1),那么setDesignResolutionSize(1,1)后,即使没有设置kResolutionShowAll,图片也能铺满整个屏幕。但是,如果setFrameSize(2, 2),如果setDesignResolutionSize(2,2)后,如果没有设置kResolutionShowAll,那么图片只会铺满屏幕的1/4,只有设置kResolutionShowAll,屏幕才会缩放到整个屏幕。

对象类CCObject

这个类是所有类的开始,主要包含了节点的内存管理机制,内存的释放、保留、复制等操作。

有兴趣的可以自己去找资料学一下内存管理机制。

主要函数如下:

  1. void release(void); //释放资源
  2. void retain(void); //保留资源,不被回收
  3. CCObject* autorelease(void); //设置实例对象的释放由内存管理器进行管理,实现自动释放
  4. CCObject* copy(void); //拷贝对象
  5. bool isSingleReference(void) const; //判断对象是否只有一个使用者
  6. unsigned int retainCount(void) const; //返回内存计数器,即对象的使用者个数
  7. virtual bool isEqual(const CCObject* pObject); //判断是否与pObject对象相同
  8. virtual void update(float dt) {CC_UNUSED_PARAM(dt);}; //更新函数,与scheduleUpdate()对应
  9. friend class CCAutoreleasePool; //友元类,管理对象的内存使用情况
  10. //

节点类CCNode

CCNode类是绝大部分类的父类(并不是所有的类,例如CCDirector类是直接继承CCObject类的),如CCScene、CCLayer、CCSprite以及精灵集合CCSpriteBatchNode等等等等的父类都是CCNode。

CCNode类包含了一些基本的属性、节点相关、Action动作的执行、以及定时器等相关的操作。

当然CCNode也有父类,其父类为CCObject。

继承关系如下:

主要函数如下:

  1. /**
  2. * 属性相关函数
  3. * Visible , ContentSize , Position , AnchorPoint ,
  4. * ZOrder , Scale , Skew , Rotation
  5. */
  6. //设置节点是否可见.
  7. virtual void setVisible(bool visible);
  8. virtual bool isVisible();
  9. //设置节点的尺寸大小.即节点的容器大小.
  10. virtual void setContentSize(const CCSize& contentSize);
  11. virtual const CCSize& getContentSize() const;
  12. //设置节点的坐标(x,y).在OpenGL中的坐标
  13. virtual void setPosition(const CCPoint &position); //传参为坐标类CCPoint
  14. virtual const CCPoint& getPosition();
  15. virtual void setPosition(float x, float y);
  16. virtual void getPosition(float* x, float* y);
  17. virtual void setPositionX(float x);
  18. virtual float getPositionX(void);
  19. virtual void setPositionY(float y);
  20. virtual float getPositionY(void);
  21. //设置节点的锚点.
  22. //锚点就像一枚图钉,将图片钉在屏幕上.而锚点就是图片的坐标.
  23. //当然图钉可以钉在图片的左下角,右上角,或者中心都可以.
  24. //一般由CCNode继承的子类大多锚点都在中心,也有些是在左下角.
  25. virtual void setAnchorPoint(const CCPoint& anchorPoint);
  26. virtual const CCPoint& getAnchorPoint();
  27. virtual const CCPoint& getAnchorPointInPoints();
  28. //设置节点的Z轴.
  29. //当有多个节点在Z轴显示时,引擎会根据它们Z轴的大小决定绘制顺序,Z轴大的会遮盖Z轴小的
  30. virtual void setZOrder(int zOrder);
  31. virtual int getZOrder();
  32. //设置节点的放缩比例.对X轴或Y轴进行放缩
  33. //例如一张图片. 放缩它的宽X,和高Y
  34. virtual void setScaleX(float fScaleX); //放缩宽X
  35. virtual float getScaleX();
  36. virtual void setScaleY(float fScaleY); //放缩高Y
  37. virtual float getScaleY();
  38. virtual void setScale(float scale); //同时放缩X与Y
  39. virtual float getScale();
  40. virtual void setScale(float fScaleX,float fScaleY); //X放缩fScaleX倍,Y放缩fScaleY倍
  41. //设置节点的倾斜角度.与平面的倾斜角度
  42. //如一张图片. X轴倾斜fSkewX角度,Y轴倾斜fSkewY角度
  43. virtual void setSkewX(float fSkewX);
  44. virtual float getSkewX();
  45. virtual void setSkewY(float fSkewY);
  46. virtual float getSkewY();
  47. //设置节点旋转角度.
  48. virtual void setRotation(float fRotation);
  49. virtual float getRotation();
  50. virtual void setRotationX(float fRotaionX);
  51. virtual float getRotationX();
  52. virtual void setRotationY(float fRotationY);
  53. virtual float getRotationY();
  54. /**
  55. * 节点相关函数
  56. * addChild , removeChild , setParent , removeFromParent ,
  57. * reorderChild , sortAllChildren , setTag ,
  58. * getCamera , isRunning , cleanup ,
  59. * draw , visit , boundingBox ,
  60. * onEnter , onEnterTransitionDidFinish , onExit
  61. */
  62. //添加子节点.zOrder默认为0.
  63. //tag为节点编号,可以通过tag获取子节点.
  64. virtual void addChild(CCNode * child);
  65. virtual void addChild(CCNode * child, int zOrder);
  66. virtual void addChild(CCNode* child, int zOrder, int tag);
  67. virtual CCNode * getChildByTag(int tag);
  68. virtual CCArray* getChildren(); //获得所有子节点,并以CCArray数组返回
  69. virtual unsigned int getChildrenCount(void) const; //子节点个数
  70. //删除子节点.
  71. virtual void removeChild(CCNode* child);
  72. virtual void removeChild(CCNode* child, bool cleanup);
  73. virtual void removeChildByTag(int tag);
  74. virtual void removeChildByTag(int tag, bool cleanup);
  75. virtual void removeAllChildren(); //删除所有节点
  76. virtual void removeAllChildrenWithCleanup(bool cleanup); //cleanup为true则删除子节点的所有动作
  77. //设置父节点.
  78. virtual void setParent(CCNode* parent);
  79. virtual CCNode* getParent();
  80. //从父节点中移除该节点.
  81. //Cleanup为true则删除当前节点的所有动作及回调函数.
  82. virtual void removeFromParent();
  83. virtual void removeFromParentAndCleanup(bool cleanup);
  84. //重新设定节点的zOrder
  85. virtual void reorderChild(CCNode * child, int zOrder);
  86. //重新排序所有子节点
  87. virtual void sortAllChildren();
  88. //设置节点的tag编号
  89. virtual void setTag(int nTag);
  90. virtual int getTag() const;
  91. //获取节点的CCCamera摄像机
  92. virtual CCCamera* getCamera();
  93. //判断节点是否在运行
  94. virtual bool isRunning();
  95. //停止所有运行的动作和回调函数
  96. virtual void cleanup(void);
  97. //绘制节点.
  98. //draw里有好多绘制方法.如直线,曲线,矩形,圆等
  99. virtual void draw(void);
  100. //递归访问所有子节点,并重新绘制
  101. virtual void visit(void);
  102. //返回节点的矩形边界框
  103. CCRect boundingBox(void);
  104. //节点开始进入舞台时调用.即创建时调用.
  105. virtual void onEnter();
  106. //节点进入舞台后调用.即创建完后调用.
  107. virtual void onEnterTransitionDidFinish();
  108. //节点离开舞台时调用.即移除时调用
  109. virtual void onExit();
  110. /**
  111. * Action动作相关
  112. * runAction , stopAction , getActionByTag , numberOfRunningActions
  113. */
  114. //执行动作
  115. CCAction* runAction(CCAction* action);
  116. //暂停动作
  117. void stopAllActions(void);
  118. void stopAction(CCAction* action);
  119. void stopActionByTag(int tag);
  120. CCAction* getActionByTag(int tag); //根据tag标记获取动作
  121. unsigned int numberOfRunningActions(void); //获取正在运行的动作数量
  122. /**
  123. * 定时器相关函数
  124. * scheduleUpdate , schedule , update
  125. */
  126. //开启默认定时器.刷新次数为60次/秒.即每秒60帧.
  127. //与update(float delta)回调函数相对应.
  128. //给予定时器优先级priority.其中priority越小,优先级越高
  129. void scheduleUpdate(void);
  130. void scheduleUpdateWithPriority(int priority);
  131. void unscheduleUpdate(void); //取消默认定时器
  132. virtual void update(float delta); //update为scheduleUpdate定时器的回调函数.
  133. //设置自定义定时器.默认为每秒60帧.
  134. //interval : 每隔interval秒,执行一次.
  135. //repeat : 重复次数.
  136. //delay : 延迟时间,即创建定时器delay后开始执行.
  137. void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
  138. void schedule(SEL_SCHEDULE selector, float interval);
  139. void scheduleOnce(SEL_SCHEDULE selector, float delay); //只执行一次,delay秒后执行
  140. void schedule(SEL_SCHEDULE selector); //默认为每秒60帧
  141. void unschedule(SEL_SCHEDULE selector); //取消定时器
  142. void unscheduleAllSelectors(void); //取消所有定时器
  143. void pauseSchedulerAndActions(void); //暂停所有定时器和动作
  144. void resumeSchedulerAndActions(void); //恢复所有定时器和动作
  145. //

导演类CCDirector

就和现实中的导演一样,这里的导演也是起到指导的作用的。导演在这里负责的就是让不同的场景切换,控制整个游戏的流程,包括开始,继续,暂停等。以及设置、获取系统信息,比如调整OpenGL相关的设置,获取屏幕的大小等。

和CCScene、CCLayer、CCSprite等不同的是,导演类CCDirector是直接继承CCObject类的,而不是CCNode类。

继承关系如下:

主要函数如下:

  1. class CC_DLL CCDirector : public CCObject, public TypeInfo
  2. {
  3. //获取全局唯一的CCDirector实例
  4. //使用方法:CCDirector::sharedDirector()->replaceScene(scene);
  5. static CCDirector* sharedDirector(void);
  6. /**
  7. * 场景管理相关
  8. * runWithScene , pushScene , popScene , popToRootScene , replaceScene ,
  9. * pause , resume , end ,
  10. */
  11. //指定进入Director的主循环运行的场景.
  12. //ps:仅在运行第一个场景时调用,如果已经存在运行中的场景,不能调用本方法.
  13. //本方法调用后将调用pushScene方法,然后调用startAnimation.
  14. void runWithScene(CCScene *pScene);
  15. //将运行中的场景暂停,并push到场景堆栈中,运行新的场景.
  16. void pushScene(CCScene *pScene);
  17. //从场景堆栈中pop出一个场景,替换现在正运行的场景,而运行中的场景将被删除.
  18. void popScene(void);
  19. //从场景堆栈中pop出所有场景,最后一个栈底的场景将替换现在正运行的场景,而运行中的场景将被删除.
  20. void popToRootScene(void);
  21. //使用新场景替换当前场景,而运行中的场景将被删除.
  22. //PS:旧场景不压入堆栈,而是直接删除.
  23. void replaceScene(CCScene *pScene);
  24. void pause(void); //暂停场景
  25. void resume(void); //恢复被暂停的场景
  26. void end(void); //终止执行,释放运行中的场景. 而OpenGL view需要手动移除.
  27. //获取当前运行的场景. 导演在某一时刻只能运行一个场景
  28. inline CCScene* getRunningScene(void) { return m_pRunningScene; }
  29. //是否暂停
  30. inline bool isPaused(void) { return m_bPaused; }
  31. //场景替换时是否接收到Cleanup事件.即是否清除场景.
  32. //若新场景是push进来的,旧场景不会接收到Cleanup事件
  33. //若新场景是replace进来的,旧场景会接收到Cleanup事件
  34. inline bool isSendCleanupToScene(void) { return m_bSendCleanupToScene; }
  35. /**
  36. * 刷新帧数FPS相关
  37. * setAnimationInterval , setDisplayStats
  38. */
  39. //设置程序的FPS值. 即刷新频率,相连两帧的时间间隔.
  40. //如dValue = 1.0/60.0 表示每秒60帧.
  41. virtual void setAnimationInterval(double dValue) = 0;
  42. inline double getAnimationInterval(void) { return m_dAnimationInterval; }
  43. //是否在程序屏幕的左下角显示FPS值
  44. inline void setDisplayStats(bool bDisplayStats) { m_bDisplayStats = bDisplayStats; }
  45. //判断是否有显示FPS值
  46. inline bool isDisplayStats(void) { return m_bDisplayStats; }
  47. //获取每帧间隔的秒数
  48. inline float getSecondsPerFrame() { return m_fSecondsPerFrame; }
  49. //从CCDirector开机后,总共已经渲染了多少帧
  50. inline unsigned int getTotalFrames(void) { return m_uTotalFrames; }
  51. /**
  52. * OpenGL图形渲染相关
  53. */
  54. //设置CCEGLView.即OpenGL图形渲染
  55. inline CCEGLView* getOpenGLView(void) { return m_pobOpenGLView; }
  56. void setOpenGLView(CCEGLView *pobOpenGLView);
  57. //设置OpenGL的Projection
  58. void setProjection(ccDirectorProjection kProjection);
  59. inline ccDirectorProjection getProjection(void) { return m_eProjection; }
  60. //设置OpenGL的glViewport
  61. void setViewport();
  62. /**
  63. * OpenGL View视图相关
  64. */
  65. //获取OpenGL view的大小,单位为点.
  66. //类似手机屏幕的大小.参照"主要控制类AppDelegate.cpp"中的图片.
  67. CCSize getWinSize(void);
  68. //获取OpenGL view的大小,单位为像素.
  69. CCSize getWinSizeInPixels(void);
  70. //获取OpenGL View可视区域大小,单位为点.
  71. //类似程序的游戏区域.参照"主要控制类AppDelegate.cpp"中的图片.
  72. CCSize getVisibleSize();
  73. //获取可视区域的原点坐标.一般为程序游戏区域的左下角坐标.
  74. CCPoint getVisibleOrigin();
  75. //将UIKit坐标与OpenGL坐标的相互转换
  76. //UIKit坐标:原点在屏幕的左上角. 从左到右,从上到下.
  77. //OpenGL坐标:原点在屏幕的左下角.从左到右,从下到上.
  78. CCPoint convertToGL(const CCPoint& obPoint); //转为GL坐标
  79. CCPoint convertToUI(const CCPoint& obPoint); //转为UI坐标
  80. /**
  81. * 其他
  82. */
  83. //开始动画
  84. virtual void startAnimation(void) = 0;
  85. //停止动画
  86. virtual void stopAnimation(void) = 0;
  87. //绘制场景,每帧都会自动调用,无需手动.
  88. void drawScene(void);
  89. //删除缓存数据。包括CCTextureCache、CCSpriteFrameCache、CCLabelBMFont缓存数据
  90. void purgeCachedData(void);
  91. }
  92. //

场景类CCScene

CCScene是继承与CCNode类的。作为场景类,它却只有两个函数init和create。因为场景就像是一个容器,将不同的图层(CCLayer)组合在一起,方便管理。

一个游戏会有很多的场景,比如,主界面,游戏界面,载入界面等等都是一个场景。而每一个场景都是由多个图层组合在一起,形成一个完整的游戏画面。

其实在 cocos2dx基础篇(3)——第一个程序HelloWorld 中就出现了CCScene的创建,以及将HelloWorld图层放入该CCScene中。

继承关系如下:

以下为CCScene的源码:

  1. class CC_DLL CCScene : public CCNode
  2. {
  3. public:
  4. CCScene();
  5. virtual ~CCScene();
  6. bool init();
  7. static CCScene *create(void);
  8. };
  9. bool CCScene::init()
  10. {
  11. bool bRet = false;
  12. do {
  13. CCDirector * pDirector;
  14. CC_BREAK_IF( ! (pDirector = CCDirector::sharedDirector()) );
  15. this->setContentSize(pDirector->getWinSize());
  16. // success
  17. bRet = true;
  18. } while (0);
  19. return bRet;
  20. }
  21. CCScene *CCScene::create()
  22. {
  23. CCScene *pRet = new CCScene();
  24. if (pRet && pRet->init()) {
  25. pRet->autorelease();
  26. return pRet;
  27. }
  28. else {
  29. CC_SAFE_DELETE(pRet);
  30. return NULL;
  31. }
  32. }
  33. //

图层类CCLayer

CCLayer继承于四个父类: CCNode, CCTouchDelegate, CCAccelerometerDelegate, CCKeypadDelegate。

CCLayer不仅继承了CCNode的所有操作,还附加触屏、重力加速度计、支持输入功能。

一个图层(CCLayer)可以包含多个元素,如标签(CCLabel)、菜单(CCMenu)、精灵(CCSprite)等等。

注意:CCLayer的锚点默认为(0,0),即左下角。并且忽略锚点的设置,即使你setAnchorPoint了锚点,CCLayer的锚点也不会改变,依然是(0,0)。

继承关系如下:

主要函数如下:

  1. class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate
  2. {
  3. //创建一个静态图层对象
  4. static CCLayer *create(void);
  5. virtual void onEnter(); //进入图层回调函数
  6. virtual void onExit(); //退出图层回调函数
  7. virtual void onEnterTransitionDidFinish(); //场景转换后的回调函数
  8. /**
  9. * 触屏事件相关
  10. * 分为单点触屏、多点触屏
  11. * ccTouchBegan , ccTouchMoved , ccTouchEnded , ccTouchCancelled ,
  12. * registerWithTouchDispatcher , TouchEnabled , TouchMode , TouchPriority
  13. */
  14. //单点触屏接口的回调函数
  15. virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); //触屏开始
  16. virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); //触屏移动
  17. virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); //触屏结束
  18. virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); //触屏取消
  19. //多点触屏接口的回调函数
  20. virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
  21. virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
  22. virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
  23. virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
  24. //注册触屏侦听事件
  25. //默认是:CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0);
  26. //例如:
  27. // void CCLayer::registerWithTouchDispatcher() {
  28. // CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,INT_MIN+1,true); }
  29. virtual void registerWithTouchDispatcher(void);
  30. //注册脚本触屏事件
  31. virtual void registerScriptTouchHandler(int nHandler, bool bIsMultiTouches = false, int nPriority = INT_MIN, bool bSwallowsTouches = false);
  32. //注销脚本触屏事件
  33. virtual void unregisterScriptTouchHandler(void);
  34. //设置是否接受触屏
  35. virtual void setTouchEnabled(bool value);
  36. virtual bool isTouchEnabled();
  37. //设置触屏模式. 同时响应,还是逐个响应
  38. virtual void setTouchMode(ccTouchesMode mode);
  39. virtual int getTouchMode();
  40. //设置触屏的优先级. 默认为0,且priority越小优先级越高.
  41. virtual void setTouchPriority(int priority);
  42. virtual int getTouchPriority();
  43. /**
  44. * 键盘输入相关
  45. */
  46. //设置是否接受键盘输入
  47. virtual bool isKeypadEnabled();
  48. virtual void setKeypadEnabled(bool value);
  49. //注册,注销 脚本键盘输入
  50. void registerScriptKeypadHandler(int nHandler);
  51. void unregisterScriptKeypadHandler(void);
  52. //返回键和菜单键的回调函数,需要设置接收键盘事件
  53. virtual void keyBackClicked(void);
  54. virtual void keyMenuClicked(void);
  55. /**
  56. * 加速度计相关. 即重力感应.
  57. * 类似重力加速,不同的是加速度计分别在X轴,Y轴,Z轴都有一个相应的加速度.
  58. * didAccelerate , registerScriptAccelerateHandler ,
  59. * AccelerometerEnabled , AccelerometerInterval
  60. */
  61. //加速度计信息
  62. virtual void didAccelerate(CCAcceleration* pAccelerationValue); //加速度计信息
  63. void registerScriptAccelerateHandler(int nHandler); //注册加速度计
  64. void unregisterScriptAccelerateHandler(void); //注销加速度计
  65. //设置是否接受加速度计的信息
  66. virtual bool isAccelerometerEnabled();
  67. virtual void setAccelerometerEnabled(bool value);
  68. virtual void setAccelerometerInterval(double interval); //设置加速度计的时间间隔
  69. }
  70. //

精灵类CCSprite

精灵说简单一点,其实就是一个2D的图片。并赋予图片各种属性以及特性。如大小、颜色、放缩、旋转、动作等。精灵一般都是放在图层(CCLayer)上面的,即一个图层(CCLayer)应当有许多的精灵存在。精灵可以用来当做背景、人物、鸟、白云等内容。

CCSprite不仅继承了CCNode,还继承两个协议类:CCNodeRGBAProtocol和 CCTextureProtocol。

其中CCNodeRGBAProtocol协议类主要负责颜色的管理;而CCTextureProtocol协议类主要负责纹理图片的管理。

注意:精灵的锚点默认为(0.5,0.5),即中心点。

继承关系如下:

主要函数如下:

  1. class CC_DLL CCSprite : public CCNodeRGBA, public CCTextureProtocol
  2. {
  3. /**
  4. * 创建精灵相关create
  5. * create , createWithTexture ,
  6. * createWithSpriteFrame , createWithSpriteFrameName
  7. */
  8. //注意事项:
  9. //从大图中截取某一区域的图片的CCRect rect的构造应该是这样的:
  10. // CCRect("小图左上角坐标x", "小图左上角坐标y", 小图宽, 小图高)
  11. //这与cocos2dx的坐标系是不一样的
  12. static CCSprite* create(); //默认创建空精灵对象
  13. static CCSprite* create(const char *pszFileName); //图片文件
  14. static CCSprite* create(const char *pszFileName, const CCRect& rect); //截取图片文件中某一区域图片
  15. static CCSprite* createWithTexture(CCTexture2D *pTexture); //纹理图片
  16. static CCSprite* createWithTexture(CCTexture2D *pTexture, const CCRect& rect); //截取纹理图片中某一区域图片
  17. static CCSprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame); //精灵帧. 精灵帧一般是从plist中读取的
  18. static CCSprite* createWithSpriteFrameName(const char *pszSpriteFrameName); //精灵帧的名字
  19. /**
  20. * 初始化精灵相关init
  21. * 一般在精灵create的时候,就会调用相对应的init函数.
  22. * init , initWithTexture ,
  23. * initWithSpriteFrame , initWithSpriteFrameName ,
  24. * initWithFile
  25. */
  26. virtual bool init(void); //默认初始化
  27. virtual bool initWithTexture(CCTexture2D *pTexture); //纹理图片
  28. virtual bool initWithTexture(CCTexture2D *pTexture, const CCRect& rect); //截取纹理图片中某一区域图片
  29. virtual bool initWithTexture(CCTexture2D *pTexture, const CCRect& rect, bool rotated); //截取纹理图片中某一区域图片,是否旋转
  30. virtual bool initWithSpriteFrame(CCSpriteFrame *pSpriteFrame); //精灵帧. 精灵帧一般是从plist中读取的
  31. virtual bool initWithSpriteFrameName(const char *pszSpriteFrameName); //精灵帧的名字
  32. virtual bool initWithFile(const char *pszFilename); //图片文件
  33. virtual bool initWithFile(const char *pszFilename, const CCRect& rect); //截取图片文件中某一区域图片
  34. /**
  35. * 继承于节点类CCNode的函数
  36. * Scale , Position , Rotation , Skew , VertexZ
  37. * addChild , removeChild , reorderChild , sortAllChildren ,
  38. * AnchorPoint , Visible , draw
  39. */
  40. virtual void setScale(float fScale);
  41. virtual void setScaleX(float fScaleX);
  42. virtual void setScaleY(float fScaleY);
  43. virtual void setPosition(const CCPoint& pos);
  44. virtual void setRotation(float fRotation);
  45. virtual void setRotationX(float fRotationX);
  46. virtual void setRotationY(float fRotationY);
  47. virtual void setSkewX(float sx);
  48. virtual void setSkewY(float sy);
  49. virtual void setVertexZ(float fVertexZ);
  50. virtual void addChild(CCNode *pChild);
  51. virtual void addChild(CCNode *pChild, int zOrder);
  52. virtual void addChild(CCNode *pChild, int zOrder, int tag);
  53. virtual void removeChild(CCNode* pChild, bool bCleanup);
  54. virtual void removeAllChildrenWithCleanup(bool bCleanup);
  55. virtual void reorderChild(CCNode *pChild, int zOrder);
  56. virtual void sortAllChildren();
  57. virtual void setAnchorPoint(const CCPoint& anchor);
  58. virtual void ignoreAnchorPointForPosition(bool value);
  59. virtual void setVisible(bool bVisible);
  60. virtual void draw(void);
  61. /**
  62. * 继承于颜色协议类CCNodeRGBA的函数
  63. * Color , Opacity
  64. */
  65. //RGB颜色
  66. virtual void setColor(const ccColor3B& color3); //设置颜色
  67. virtual void updateDisplayedColor(const ccColor3B& parentColor); //传递颜色
  68. //透明度
  69. virtual void setOpacity(GLubyte opacity); //设置透明度
  70. virtual void setOpacityModifyRGB(bool modify); //设置透明度是否随RGB颜色的变化而变化
  71. virtual bool isOpacityModifyRGB(void); //判断透明度是否随RGB颜色的变化而变化
  72. virtual void updateDisplayedOpacity(GLubyte parentOpacity); //传递透明度
  73. /**
  74. * 继承于纹理协议类CCTextureProtocol的函数
  75. * Texture , BlendFunc
  76. */
  77. //设置精灵的纹理图片
  78. virtual void setTexture(CCTexture2D *texture);
  79. virtual CCTexture2D* getTexture(void);
  80. //设置颜色混合方式
  81. inline void setBlendFunc(ccBlendFunc blendFunc) { m_sBlendFunc = blendFunc; }
  82. inline ccBlendFunc getBlendFunc(void) { return m_sBlendFunc; }
  83. /**
  84. * 批节点CCSpriteBatchNode相关的函数
  85. * CCSpriteBatchNode是精灵集合类,都使用同一张纹理图片.
  86. * 故将这些精灵成批进行渲染,以提高渲染速度.
  87. */
  88. virtual void updateTransform(void); //更新四个值:position(x,y), rotation, scale
  89. virtual CCSpriteBatchNode* getBatchNode(void); //如果精灵是由批节点渲染的,则返回批节点
  90. virtual void setBatchNode(CCSpriteBatchNode *pobSpriteBatchNode); //设置批节点,不推荐使用
  91. /**
  92. * 纹理Texture相关的函数
  93. */
  94. //设置纹理区域
  95. virtual void setTextureRect(const CCRect& rect);
  96. virtual void setTextureRect(const CCRect& rect, bool rotated, const CCSize& untrimmedSize);
  97. virtual void setVertexRect(const CCRect& rect);
  98. /**
  99. * 精灵帧SpriteFrames & 动画Animation相关的函数
  100. *
  101. */
  102. virtual void setDisplayFrame(CCSpriteFrame *pNewFrame); //设置新的显示精灵帧
  103. virtual bool isFrameDisplayed(CCSpriteFrame *pFrame); //返回精灵帧是否正在显示
  104. virtual CCSpriteFrame* displayFrame(void); //返回当前显示的精灵帧
  105. //通过动画帧的第frameIndex那一帧来设置显示精灵帧
  106. //动画帧是从CCAnimationCache中读取的
  107. virtual void setDisplayFrameWithAnimationName(const char *animationName, int frameIndex);
  108. /**
  109. * 属性相关的函数
  110. */
  111. //设置精灵是否需要更新
  112. inline virtual void setDirty(bool bDirty) { m_bDirty = bDirty; }
  113. inline virtual bool isDirty(void) { return m_bDirty; }
  114. //返回四个值的信息:坐标(x,y),顶点,颜色
  115. inline ccV3F_C4B_T2F_Quad getQuad(void) { return m_sQuad; }
  116. //判断纹理是否被旋转
  117. inline bool isTextureRectRotated(void) { return m_bRectRotated; }
  118. //设置精灵在地图集TextureAtlas中的索引
  119. inline void setAtlasIndex(unsigned int uAtlasIndex) { m_uAtlasIndex = uAtlasIndex; }
  120. inline unsigned int getAtlasIndex(void) { return m_uAtlasIndex; }
  121. //返回精灵区域,单位为点
  122. inline const CCRect& getTextureRect(void) { return m_obRect; }
  123. //如果采用批渲染,设置纹理地图集
  124. inline void setTextureAtlas(CCTextureAtlas *pobTextureAtlas) { m_pobTextureAtlas = pobTextureAtlas; }
  125. inline CCTextureAtlas* getTextureAtlas(void) { return m_pobTextureAtlas; }
  126. //获取偏移值
  127. inline const CCPoint& getOffsetPosition(void) { return m_obOffsetPosition; }
  128. //设置是否翻转
  129. void setFlipX(bool bFlipX);
  130. void setFlipY(bool bFlipY);
  131. bool isFlipX(void);
  132. bool isFlipY(void);
  133. };
  134. //

坐标类CCPoint

CCPoint既可以表示坐标点,又可以表示一个坐标向量。

同时CCPoint对运算符进行的重载,可以很方便的完成CCPoint的赋值、加减乘除等操作。另外还有与坐标向量相关的:距离、角度、点积、叉积、投影、标准化等操作。

当然cocos2dx也提供了许多有关CCPoint运算的宏定义与常量,如CCPointZero,CCPointMake,ccp,ccpAdd,ccpSub等。

CCPoint可以使一个坐标点,也可以是一个坐标向量。

主要函数如下:

  1. class CC_DLL CCPoint
  2. {
  3. public:
  4. float x; //X坐标
  5. float y; //Y坐标
  6. /**
  7. * 构造函数
  8. */
  9. CCPoint();
  10. CCPoint(float x, float y);
  11. CCPoint(const CCPoint& other);
  12. CCPoint(const CCSize& size);
  13. /**
  14. * 运算符重载
  15. * 直接像int型一样相加减.如 p3 = p1 + p2
  16. */
  17. CCPoint& operator= (const CCPoint& other); //(other.x , other.y)
  18. CCPoint& operator= (const CCSize& size); //(size.width , size.height)
  19. CCPoint operator+(const CCPoint& right) const; //(x1+x2 , y1+y2)
  20. CCPoint operator-(const CCPoint& right) const; //(x1-x2 , y1-y2)
  21. CCPoint operator-() const; //(-x , -y)
  22. CCPoint operator*(float a) const; //(x*a , y*a)
  23. CCPoint operator/(float a) const; //(x/a , y/a)
  24. /**
  25. * CCPoint的相关函数
  26. * setPoint , forAngle , equals , fuzzyEquals ,
  27. * getLength , getDistance , getAngle , getPerp , rotateByAngle ,
  28. * dot , cross , project , normalize ,
  29. * rotate , unrotate , lerp
  30. */
  31. //设置坐标
  32. void setPoint(float x, float y);
  33. //根据角度,计算向量坐标x=cos(a) , y=sin(a)
  34. //这是一个static静态函数
  35. static inline CCPoint forAngle(const float a) { return CCPoint(cosf(a), sinf(a)); }
  36. //判断是否与target相等
  37. bool equals(const CCPoint& target) const;
  38. //判断target是否在坐标点模糊偏差为var的范围内.
  39. //if( (x - var <= target.x && target.x <= x + var) &&
  40. // (y - var <= target.y && target.y <= y + var) )
  41. // return true;
  42. bool fuzzyEquals(const CCPoint& target, float variance) const;
  43. //与原点的距离
  44. //与原点的距离平方,即x*x + y*y.
  45. //与other的距离
  46. //与other的距离平方
  47. inline float getLength() const { return sqrtf(x*x + y*y); };
  48. inline float getLengthSq() const { return dot(*this); };
  49. inline float getDistance(const CCPoint& other) const { return (*this - other).getLength(); };
  50. inline float getDistanceSq(const CCPoint& other) const { return (*this - other).getLengthSq(); };
  51. //与X轴的夹角; 与other向量的夹角. 单位为:弧度
  52. inline float getAngle() const { return atan2f(y, x); };
  53. float getAngle(const CCPoint& other) const;
  54. //Perp逆时针旋转90度; RPerp顺时针旋转90度
  55. inline CCPoint getPerp() const { return CCPoint(-y, x); };
  56. inline CCPoint getRPerp() const { return CCPoint(y, -x); };
  57. //以pivot为圆心,将坐标逆时针旋转angle度
  58. CCPoint rotateByAngle(const CCPoint& pivot, float angle) const;
  59. //计算两点的 "点积dot" 和 "叉积cross"
  60. inline float dot(const CCPoint& other) const { return x*other.x + y*other.y; };
  61. inline float cross(const CCPoint& other) const { return x*other.y - y*other.x; };
  62. //向量在other上的投影向量
  63. //公式参考: http://www.cnblogs.com/graphics/archive/2010/08/03/1791626.html
  64. inline CCPoint project(const CCPoint& other) const { return other * (dot(other)/other.dot(other)); };
  65. //向量标准化,即长度为1. PS: 如果是零向量,返回(1,0);
  66. inline CCPoint normalize() const {
  67. float length = getLength();
  68. if(length == 0.0) return CCPoint(1.0f, 0);
  69. return *this / getLength();
  70. };
  71. /**
  72. * 未知函数
  73. */
  74. //复合乘法???
  75. //angle = this.getAngle() + other.getAngle()
  76. //length = this.getLength() * other.getLength()
  77. inline CCPoint rotate(const CCPoint& other) const {
  78. return CCPoint(x*other.x - y*other.y, x*other.y + y*other.x);
  79. };
  80. //反复合乘法???
  81. //angle = this.getAngle() - other.getAngle()
  82. //length = this.getLength() * other.getLength()
  83. inline CCPoint unrotate(const CCPoint& other) const {
  84. return CCPoint(x*other.x + y*other.y, y*other.x - x*other.y);
  85. };
  86. //线性内插法???
  87. inline CCPoint lerp(const CCPoint& other, float alpha) const {
  88. return *this * (1.f - alpha) + other * alpha;
  89. };
  90. };
  91. //

尺寸大小类CCSize

CCSize比较简单,只是一个用来表示尺寸大小的类。宽为width,高为height。

和CCPoint一样,也对运算符进行了重载。

目前好像就找到两个宏定义与常量:CCSizeMake 和 CCSizeZero。

主要函数如下:

  1. class CC_DLL CCSize
  2. {
  3. public:
  4. float width; //宽度
  5. float height; //高度
  6. /**
  7. * 构造函数
  8. */
  9. CCSize(); //(0 , 0)
  10. CCSize(float width, float height); //(width , height)
  11. CCSize(const CCSize& other); //(other.width , other.height)
  12. CCSize(const CCPoint& point); //(point.x , point.y)
  13. /**
  14. * 运算符重载
  15. * 直接像int型一样相加减.如 p3 = p1 + p2
  16. */
  17. CCSize& operator= (const CCSize& other); //this = other
  18. CCSize& operator= (const CCPoint& point); //width=point.x , height=point.y
  19. CCSize operator+(const CCSize& right) const; //width+right.width , height+right.height
  20. CCSize operator-(const CCSize& right) const; //width-right.width , height-right.height
  21. CCSize operator*(float a) const; //width*a , height*a
  22. CCSize operator/(float a) const; //width/a , height/a
  23. /**
  24. * CCSize的相关函数
  25. * setSize , equals
  26. */
  27. //设置尺寸大小
  28. void setSize(float width, float height);
  29. //判断两尺寸是否相等
  30. bool equals(const CCSize& target) const;
  31. };
  32. //

矩形类CCRect

CCRect是一个矩形类。包含:起始坐标(左下角坐标)CCPoint、矩阵的尺寸大小CCSize两个属性。

CCRect只对“=”运算符进行了重载。目前好像就找到两个宏定义与常量:CCRectMake,CCRectZero。

值得注意的是CCRect类中:

        intersectsRect函数,可以用作两个CCRect矩形是否相交,即碰撞检测。

        containsPoint 函数,可以用作判断点CCPoint是否在CCRect矩形中。

若用CCRect来作为创建CCSprite精灵的参数,需要注意,从大图中截取某一区域的图片的CCRect rect的构造应该是这样的:

CCRect("小图左上角坐标x", "小图左上角坐标y", 小图宽, 小图高);

这与cocos2dx的坐标系是不一样的。

如下图所示:

  主要函数如下:

  1. class CC_DLL CCRect
  2. {
  3. public:
  4. CCPoint origin; //起始坐标: 左下角坐标
  5. CCSize size; //尺寸大小
  6. /**
  7. * 构造函数
  8. */
  9. CCRect();
  10. CCRect(float x, float y, float width, float height);
  11. CCRect(const CCRect& other);
  12. /**
  13. * 运算符重载
  14. * 只重载了 “=” 运算符
  15. */
  16. CCRect& operator= (const CCRect& other);
  17. /**
  18. * CCRect的相关函数
  19. * setRect , getMinX , getMidX , getMaxX
  20. * equals , containsPoint , intersectsRect
  21. */
  22. //设置矩形
  23. void setRect(float x, float y, float width, float height);
  24. //
  25. float getMinX() const; //origin.x
  26. float getMidX() const; //origin.x + size.width/2
  27. float getMaxX() const; //origin.x + size.width
  28. float getMinY() const; //origin.y
  29. float getMidY() const; //origin.y + size.height/2
  30. float getMaxY() const; //origin.y + size.height
  31. //判断是否与rect相同. 原点相同,尺寸相同.
  32. bool equals(const CCRect& rect) const;
  33. //判断point是否包含在矩形内或四条边上
  34. bool containsPoint(const CCPoint& point) const;
  35. //判断矩形是否相交. 常常用作碰撞检测.
  36. bool intersectsRect(const CCRect& rect) const;
  37. };
  38. //

数组类CCArray

继承于CCObject类,本质是将ccArray相关的函数操作进行了封装处理。对于ccArray有兴趣的自己了解一下。

宏定义:

  1. CCARRAY_FOREACH(CCArray* arr, CCObject* obj); //遍历CCArrray数组
  2. CCARRAY_FOREACH_REVERSE(CCArray* arr, CCObject* obj); //逆序遍历

CCArray类终点数据类型为ccArray:

  1. typedef struct _ccArray {
  2. unsigned int num; //元素个数
  3. unsigned int max; //数组容量. 和num不一样,一般max>=num.
  4. //二重指针,相当于是指向 CCobject*数组 的指针.
  5. //每个元素用arr[i]来读取,这是一个指向第i个索引的CCObject*
  6. CCObject** arr;
  7. } ccArray;
  8. //

当然在我们添加元素的时候,不一定非是CCObject类型不可,可以是其他类型,如CCNode。

CCArray类的主要函数如下:

  1. class CC_DLL CCArray : public CCObject
  2. {
  3. public:
  4. ccArray* data; //元素是一个ccArray类型
  5. public:
  6. /**
  7. * 构造、创建、初始化函数
  8. * CCArray , create , init
  9. */
  10. //构造函数
  11. CCArray();
  12. CCArray(unsigned int capacity);
  13. //创建函数
  14. static CCArray* create();
  15. static CCArray* create(CCObject* pObject, ...);
  16. static CCArray* createWithObject(CCObject* pObject);
  17. static CCArray* createWithCapacity(unsigned int capacity);
  18. static CCArray* createWithArray(CCArray* otherArray);
  19. //通过plist文件导入数组
  20. static CCArray* createWithContentsOfFile(const char* pFileName);
  21. //同上. 但不设置autorelease自动释放内存,需要手动调用release()释放
  22. static CCArray* createWithContentsOfFileThreadSafe(const char* pFileName);
  23. //初始化函数
  24. bool init();
  25. bool initWithObject(CCObject* pObject);
  26. bool initWithObjects(CCObject* pObject, ...);
  27. bool initWithCapacity(unsigned int capacity);
  28. bool initWithArray(CCArray* otherArray);
  29. /**
  30. * 查询
  31. * count , capacity ,
  32. * indexOfObject , objectAtIndex , lastObject , randomObject ,
  33. * containsObject , isEqualToArray
  34. */
  35. unsigned int count() const; //元素个数
  36. unsigned int capacity() const; //数组容量
  37. //用元素查找的索引(下标从0开始).
  38. //若不存在,返回无符号整形最大值UINT_MAX=0xFFFFFFFF
  39. unsigned int indexOfObject(CCObject* object) const;
  40. //用索引查找元素
  41. CCObject* objectAtIndex(unsigned int index);
  42. CCObject* lastObject(); //返回最后一个元素
  43. CCObject* randomObject(); //返回随机一个元素
  44. bool containsObject(CCObject* object) const; //判断object是否存在于CCArray中
  45. bool isEqualToArray(CCArray* pOtherArray); //两数组每个索引位置上的元素是否全部相同
  46. /**
  47. * 添加删除元素
  48. * addObject , insertObject
  49. * removeObject , fastRemoveObject , removeAllObjects
  50. */
  51. //添加元素
  52. void addObject(CCObject* object);
  53. void addObjectsFromArray(CCArray* otherArray);
  54. void insertObject(CCObject* object, unsigned int index);
  55. //删除元素. bReleaseObj表示是否释放资源
  56. //被删除的元素后面的元素都往前挪动一个位置
  57. void removeLastObject(bool bReleaseObj = true); //删除最后一个元素
  58. void removeObject(CCObject* object, bool bReleaseObj = true); //删除object元素
  59. void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true); //删除索引为index的元素
  60. void removeObjectsInArray(CCArray* otherArray); //删除与otherArray数组中相同的所有元素
  61. //快速删除元素.
  62. //和普通删除不同的是: 只将最后一个元素覆盖被删除的元素,不进行元素挪动
  63. void fastRemoveObject(CCObject* object); //快速删除object元素
  64. void fastRemoveObjectAtIndex(unsigned int index); //快速删除索引为index的元素
  65. //删除所有元素
  66. void removeAllObjects();
  67. /**
  68. * 重排数组
  69. * exchangeObject , replaceObjectAtIndex
  70. * reverseObjects , reduceMemoryFootprint
  71. */
  72. //交换两个元素的位置
  73. void exchangeObject(CCObject* object1, CCObject* object2);
  74. void exchangeObjectAtIndex(unsigned int index1, unsigned int index2);
  75. //用pObject替换索引为index的元素
  76. void replaceObjectAtIndex(unsigned int uIndex, CCObject* pObject, bool bReleaseObject = true);
  77. //将数组元素反序, a b c d --> d c b a.
  78. void reverseObjects();
  79. //缩小内存,将内存缩小为 max = num
  80. void reduceMemoryFootprint();
  81. };
  82. //

cocos2dx基础篇(3) 常用重要类的更多相关文章

  1. SQL Server调优系列基础篇(常用运算符总结)

    原文:SQL Server调优系列基础篇(常用运算符总结) 前言 上一篇我们介绍了如何查看查询计划,本篇将介绍在我们查看的查询计划时的分析技巧,以及几种我们常用的运算符优化技巧,同样侧重基础知识的掌握 ...

  2. SQL Server调优系列基础篇(常用运算符总结——三种物理连接方式剖析)

    前言 上一篇我们介绍了如何查看查询计划,本篇将介绍在我们查看的查询计划时的分析技巧,以及几种我们常用的运算符优化技巧,同样侧重基础知识的掌握. 通过本篇可以了解我们平常所写的T-SQL语句,在SQL ...

  3. 【Unity|C#】基础篇(21)——常用类

  4. 【Cocos2d入门教程二】Cocos2d-x基础篇

    上一章已经学习了环境的搭建.这一章对基础概念进行掌握.内容大概有: 1.导演 2.场景 3.节点 4.层 4.精灵 1.导演(Director) 导演存在的主要作用: a.环境设定(帧率 初始化ope ...

  5. cocos2dx基础篇(15) 列表视图CCTableView

    [3.x] (1)去掉 "CC" (2)TableViewCell 中: > unsigned int getIdx() 返回类型改为 ssize_t(类型为 long) ( ...

  6. cocos2dx基础篇(23) 粒子系统CCParticleSystem

    [3.x]     (1)去掉"CC"     (2)粒子位置模式 tPositionType 改为强枚举类型 ParticleSystem::PositionType:: // ...

  7. cocos2dx基础篇(22) 基本动画CCAnimation/CCAnimate

    [小知识] CCSpriteFrame     :精灵帧.    它是相对动画而产生的,其实就是一张纹理图片. CCAnimationFrame  :动画帧.    由精灵帧与间隔帧数组成,是动画CC ...

  8. cocos2dx基础篇(7) 触碰事件

    cocos2dx游戏引擎的重点是在于移动设备的跨平台开发,而移动设备上的游戏大部分都是通过屏幕触碰来进行的.比如主菜单的按钮触碰,打飞机中飞机的触碰移动,都需要用到触碰操作.想一想之前讲的菜单按钮CC ...

  9. cocos2dx基础篇(5) 按钮

    这篇是直接复制的别人的,太多了,难得写... [本节内容] CCMenu.CCMenuItem其具体的六个子类 [菜单CCMenu] 菜单CCMenu是用来装载菜单按钮的图层,图层中的子节点只能够是菜 ...

随机推荐

  1. 局部处理的边缘连接(python+opencv)

    rt import cv2 import numpy as np path = "_lo.png" img = cv2.imread(path) gray = cv2.cvtCol ...

  2. maven生成jar包编码问题

    要做一个jar文件供外部调用,此jar的源代码中注释为中文,用maven打包后在其它工程中导入后总不能正常显示中文,记录解决方法如下: 在pom.xml中设置默认编码类型为UTF-8: <pro ...

  3. jquery 自定义右键菜单

    如果要自定义右键菜单,那么就需要禁止原本的右键菜单,代码如下 document.oncontextmenu = new Function("return false;");//禁止 ...

  4. 本机的IP地址无法打开(Vue项目)

    1, 首先找到使用vue脚手架建立项目config文件中的index.js文件2, 修改dev里面的host属性值:改成 host: ‘0.0.0.0’3, 最后在局域网下,使用自己的ip进行连接,同 ...

  5. Matlab的基本矩阵运算

    (1)加减.数乘 >> a=[1,2;3,4];b=[5,6;7,8]; >> a+b ans = 6 8 10 12 >> a.*2 ans = 2 4 6 8 ...

  6. py脚本修改后自动重启

    在用socket.io, pika之类启动一个脚本死循环做server或者client的时候: 1脚本被编辑之后,是不会自动重启 2当代码报错的时候,会立即退出, 只能手动重新运行 python ap ...

  7. FJOI2017前做题记录

    FJOI2017前做题记录 2017-04-15 [ZJOI2017] 树状数组 问题转化后,变成区间随机将一个数异或一,询问两个位置的值相等的概率.(注意特判询问有一个区间的左端点为1的情况,因为题 ...

  8. sklearn pca降维

    PCA降维 一.原理 这篇文章总结的不错PCA的数学原理. PCA主成分分析是将原始数据以线性形式映射到维度互不相关的子空间.主要就是寻找方差最大的不相关维度.数据的最大方差给出了数据的最重要信息. ...

  9. AtCoder AGC022C Remainder Game (图论)

    题目链接 https://atcoder.jp/contests/agc022/tasks/agc022_c 题解 大水题一道 就他给的这个代价,猜都能猜到每个数只能用一次 仔细想想,我们肯定是按顺序 ...

  10. java中 在一个异常处理中什么语句块是可多个的

    MM们 异常处理一般格式:捕获异常:try{//代码块}catch(异常类型,例如:Exception e){//需要抛出的异常,例如:e.printStackTrace();}catch(异常类型) ...