`
7090
  • 浏览: 274136 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

cocos2d-x初学习

阅读更多
1:字符串绘制
cocos2dx的字符串绘制使用的是Label,cocos2dx并不直接支持在屏幕中绘制字符串(这是有道理的,因为我们不能直接把一个string做成一个节点,那样很难理解),如果要直接绘制的话,可以自己封装opengl函数(网上有很多例子,一般是用texture做)。

其实最简单的绘制例子就是最开始的那个Helloworld。核心代码如下:

// Create a label and initialize with string "Hello World". 
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Thonburi", 64); 
CC_BREAK_IF(! pLabel); 
 
// Get window size and place the label upper. 
CCSize size = CCDirector::sharedDirector()->getWinSize(); 
pLabel->setPosition(ccp(size.width / 2, size.height - 20)); 
 
// Add the label to HelloWorld layer as a child layer. 
this->addChild(pLabel, 1); 


2:Menu绘制
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
										"CloseNormal.png",
										"CloseSelected.png",
										this,							menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
CCMenu* eMenu = CCMenu::menuWithItems(pCloseItem,NULL);
eMenu->setPosition( CCPointZero );
this->addChild(eMenu);

CCMenuItem* newGameItem=CCMenuItemFont::itemFromString("1234", this, NULL);
CCMenuItem* startGameItem=CCMenuItemFont::itemFromString("start Game",this,NULL);
CCMenuItemToggle* itemToggle=CCMenuItemToggle::itemWithTarget(this, NULL,CCMenuItemFont::itemFromString("开"));
CCMenu* pMenu=CCMenu::menuWithItems(newGameItem,startGameItem,itemToggle,NULL);
pMenu->alignItemsVertically();
this->addChild(pMenu, 1);


3:绘制图片
cocos2dx中并没有直接绘制图片的概念,我们一般是使用CCSprite。核心代码如下:

//Add add a splash screen, show the cocos2d splash image. 
CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png"); 
CC_BREAK_IF(! pSprite); 
 
// Place the sprite on the center of the screen 
pSprite->setFlipX(true); //可以手动设置图形旋转和镜像,而不是使用Action,因为有许多Action是个过程,而不是直接显示结果 
pSprite->setRotation(90); 
pSprite->setPosition(ccp(size.width/2, size.height/2)); 
 
// Add the sprite to HelloWorld layer as a child layer. 
this->addChild(pSprite, 0); 


4:基本图形绘制
cocos2dx封装了大量opengl函数,用于快速绘制基本图形,这些代码的例子在,tests\DrawPrimitivesTest目录下

注意,该方法是重载node的draw方法实现的,在智能机上,并不推荐直接绘制几何图形,因为大量的坐标编码会极大降低工作效率,应尽量使用Image。而且cocos2dx的渲染机制会造成前后遮挡问题,尤其是几何图形与图片等其他node混合绘制时。
void DrawPrimitivesTest::draw() 
{ 
        CCLayer::draw(); 
 
    CCSize s = CCDirector::sharedDirector()->getWinSize(); 
         
        // draw a simple line 
        // The default state is: 
        // Line Width: 1 
        // color: 255,255,255,255 (white, non-transparent) 
        // Anti-Aliased 
        glEnable(GL_LINE_SMOOTH); 
        ccDrawLine( CCPointMake(0, 0), CCPointMake(s.width, s.height) ); 
         
        // line: color, width, aliased 
        // glLineWidth > 1 and GL_LINE_SMOOTH are not compatible 
          //注意:线宽>1 则不支持GL_LINE_SMOOTH 
        // GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone 
        glDisable(GL_LINE_SMOOTH); 
        glLineWidth( 5.0f ); 
        /*glColor4ub(255,0,0,255);*/ 
        glColor4f(1.0, 0.0, 0.0, 1.0); 
        ccDrawLine( CCPointMake(0, s.height), CCPointMake(s.width, 0) ); 
 
        // TIP: 
        // If you are going to use always the same color or width, you don't 
        // need to call it before every draw 
        // 
        // Remember: OpenGL is a state-machine. 
         
        // draw big point in the center 
        // 注意:cocos2dx绘制的是方块点 
        glPointSize(64); 
        /*glColor4ub(0,0,255,128);*/ 
        glColor4f(0.0, 0.0, 1.0, 0.5); 
        ccDrawPoint( CCPointMake(s.width / 2, s.height / 2) ); 
         
        // draw 4 small points 
        // 注意:cocos2dx绘制的是方块点 
        CCPoint points[] = { CCPointMake(60,60), CCPointMake(70,70), CCPointMake(60,70), CCPointMake(70,60) }; 
        glPointSize(4); 
        /*glColor4ub(0,255,255,255);*/ 
        glColor4f(0.0, 1.0, 1.0, 1.0); 
        ccDrawPoints( points, 4); 
         
        // draw a green circle with 10 segments 
        glLineWidth(16); 
        /*glColor4ub(0, 255, 0, 255);*/ 
        glColor4f(0.0, 1.0, 0.0, 1.0); 
        //参数依次是:中心点,半径,角度,分段数,是否连接中心点 
        ccDrawCircle( CCPointMake(s.width/2,  s.height/2), 100, 0, 10, false); 
 
        // draw a green circle with 50 segments with line to center 
        glLineWidth(2); 
        /*glColor4ub(0, 255, 255, 255);*/ 
        glColor4f(0.0, 1.0, 1.0, 1.0); 
        ccDrawCircle( CCPointMake(s.width/2, s.height/2), 50, CC_DEGREES_TO_RADIANS(90), 50, true);   
         
        // open yellow poly 
        /*glColor4ub(255, 255, 0, 255);*/ 
        glColor4f(1.0, 1.0, 0.0, 1.0); 
        glLineWidth(10); 
        CCPoint vertices[] = { CCPointMake(0,0), CCPointMake(50,50), CCPointMake(100,50), CCPointMake(100,100), CCPointMake(50,100) }; 
        //参数依次是:点数组,点数量,是否封闭 
        ccDrawPoly( vertices, 5, false); 
         
        // closed purple poly 
        /*glColor4ub(255, 0, 255, 255);*/ 
        glColor4f(1.0, 0.0, 1.0, 1.0); 
        glLineWidth(2); 
        CCPoint vertices2[] = { CCPointMake(30,130), CCPointMake(30,230), CCPointMake(50,200) }; 
        ccDrawPoly( vertices2, 3, true); 
         
        // draw quad bezier path 
          //绘制有一个控制点的贝塞尔曲线 
        ccDrawQuadBezier(CCPointMake(0,s.height), CCPointMake(s.width/2,s.height/2), CCPointMake(s.width,s.height), 50); 
 
        // draw cubic bezier path 
          //绘制有两个控制点的贝塞尔曲线 
        ccDrawCubicBezier(CCPointMake(s.width/2, s.height/2), CCPointMake(s.width/2+30,s.height/2+50), CCPointMake(s.width/2+60,s.height/2-50),CCPointMake(s.width, s.height/2),100); 
 
        //恢复opengl的正常参数 
        // restore original values 
        glLineWidth(1); 
        /*glColor4ub(255,255,255,255);*/ 
        glColor4f(1.0, 1.0, 1.0, 1.0); 
        glPointSize(1); 
} 


参考资料:
http://4137613.blog.51cto.com/4127613/754729
分享到:
评论

相关推荐

    Cocos2d-x实战:JS卷——Cocos2d-JS开发

    资源名称:Cocos2d-x实战:JS卷——Cocos2d-JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台...

    cocos2d-x事件类

    在使用cocos2d-x开发游戏的过程中,为了实现逻辑和显示相分离。 在下通宵了一个晚上,写出了该事件类。 谨记,该事件只能用于cocos2d-x中。 事件发送者需要继承EventDispatcher类 事件接收者需要继承EventHandle类...

    cocos2d-x-2.1.5

    cocos2d-x-2.1.5

    大富翁手机游戏开发实战基于Cocos2d-x3.2引擎

    资源名称:大富翁手机游戏开发实战基于Cocos2d-x3.2引擎内容简介:李德国编著的《大富翁手机游戏开发实战(基于 Cocos2d-x3.2引擎)》使用Cocos2d-x游戏引擎技术,带领读者一步一步从零开始进行大富翁移动游戏的开发...

    Cocos2d-x高级开发教程

    Cocos2d-x是移动跨平台开发最流行的游戏引擎,而本书是一本很全面的、比较‘接地气’的游戏开发教程。书中汇聚了热门手机游戏《捕鱼达人》开发的实战经验,作者从最基础的内容开始,逐步深入地介绍了Cocos2d-x的相关...

    cocos2d-x-3.2旧版引擎下载

    cocos2d-x-3.2下载,不多说。或者可以下载另一个资源 cocos引擎老版本集合(cocos2d-x-2.2.1 - 3.5) http://download.csdn.net/download/crazymagicdc/9982656

    cocos2d-x实战项目

    cocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML文件读取与骨骼动画.rarcocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML...

    Cocos2D-X游戏开发技术精解

    资源名称:Cocos2D-X游戏开发技术精解内容简介:Cocos2D-X是一款支持多平台的 2D手机游戏引擎,支持iOS、Android、BlackBerry等众多平台。当前,很多移动平台流行的游戏,都是基于Cocos2D-X开发的。 《Cocos2D-X...

    cocos2d-x-3.0 类图

    这是我重新弄的cocos2d-x-3.0的类图.之前别人兄台弄的,有些不全面,有些地方错误.我这个可以说是最新的了.每个类添加了中文的详细注解,同时也添加了中文的类名称翻译.这样对cocos2d-x-3.0的框架比较好上手. 有兴趣的...

    Cocos2D-X游戏开发技术精解.pdf

    《Cocos2D-X游戏开发技术精解》详细介绍如何使用Cocos2D-X引擎开发自己的移动平台游戏。全书共15章,主要内容包括:Cocos2D-X引擎简介;如何建立跨平台的开发环境;引擎的核心模块——渲染框架;如何实现动态画面和...

    Cocos2d-x 3.x游戏开发实战pdf含目录

    Cocos2d-x 3.x游戏开发实战pdf含目录,内容详细,强烈推荐给大家。

    精通COCOS2D-X游戏开发 基础卷_2016.4-P399-13961841.pdf

    精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发

    cocos2d-x windows vs2010配置

    Cocos2d-x windows vs2010 配置图文详解

    Cocos2d-x实战 JS卷 Cocos2d-JS开发

    Cocos2d-x实战 JS卷 Cocos2d-JS开发 PDF 电子书完整版本

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    cocos2d-x 3.0

    cocos2d-x 3.0 人物行走 . 包里有代码和 图片资源.

    Cocos2d-x游戏编程——C++篇 .iso

    Cocos2d-x游戏编程——C++篇(电子工业出版社,徐飞 著)书本配套的光盘代码,

    Cocos2d-x学习笔记

    自己记录的Cocos2d-x学习笔记,希望能够帮助新手,快速入门,掌握cocos2d-x的开发

    经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip

    经典版本 方便下载 源码 旧版本 3.8 官网找不到了 cocos2d-x-3.8.zip

    Cocos2d-x-3.x游戏开发之旅

    Cocos2d-x-3.x游戏开发之旅-钟迪龙著 全新pdf版和附书代码(代码为工程文件,可复制) 附带目录标签

Global site tag (gtag.js) - Google Analytics