我用cocos2d-x 中的CCTexture2D加载纹理之后,如何实现类似J2ME里面setClip那样的效果。参考了很多资料除了把他弄成Sprite进行裁剪外好像没其他好的办法,但是那样用绘制效率太低了。我只想显示纹理的一部分到屏幕上。顺便问下,cocos2d里面绘制基础的几何图元如何弄,比如说填充1个半透明的圆角矩形,绘制1个矩形

解决方案 »

  1.   

    用CCImage的这个方法初始化一个CCImage,bool  initWithImageFile (const char *strPath, EImageFormat imageType=kFmtPng);然后用unsigned char *  getData ()方法获得图片的像素指针,用int  getDataLen ()获得像素个数,用virtual unsigned short  getWidth (void),virtual unsigned short  getHeight (void)获得宽和高,然后算一下自己要截取的图片的位置和大小,拷入一个新的char数组里,然后用bool  initWithImageData (void *pData, int nDataLen, EImageFormat eFmt=kFmtUnKnown, int nWidth=0, int nHeight=0, int nBitsPerComponent=8)构造一个CCImage实例,用CCImage去构造一个CCTexture就OK了,看API自己想的,没实际操作过
      

  2.   

    cocos2d里面有opengl ES,用opengl的方法可以绘制图元,详细使用可以自己看看有关opengl的书
      

  3.   

    cocos2d 2.1提供了sprite新的API,指定区域的,不过还是用opengles来画吧
      

  4.   

    void PlayLayer::initClisper(CCSprite *s_hole,CCClippingNode **cliper,int tag){
        
        CCDrawNode*stencil = CCDrawNode::create();
        CCPoint rectangle[4];
        rectangle[0]= ccp(0, 0);
        rectangle[1]= ccp(s_hole->getContentSize().width,0); 
        rectangle[2]= ccp(s_hole->getContentSize().width,400);
        rectangle[3]= ccp(0, 400);
        //绘制一个矩形
        ccColor4F color = {1, 1, 1, 1};
        stencil->drawPolygon(rectangle,4, color, 1, color);    *cliper = CCClippingNode::create(stencil);
        (*cliper)->setPosition(s_hole->getPositionX()-s_hole->getContentSize().width/2,s_hole->getPositionY());
        (*cliper)->setContentSize(CCSizeMake(stencil->getContentSize().width, 200));
        (*cliper)->setTag(tag);
        this->addChild(*cliper);
    }
    代码如上,主要类CCClippingNode能够实现你所需要的功能,详细如何坐,自己google一下,很多答案,这里重点给你说一下,想要实现效果,你必须改ios目录下appcontroller.mm文件里一个地方
    EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
                                         pixelFormat: kEAGLColorFormatRGB565
                                         depthFormat: GL_DEPTH24_STENCIL8_OES
                                  preserveBackbuffer: NO
                                          sharegroup: nil
                                       multiSampling: NO
                                     numberOfSamples:0 ];
        
        cocos2d::CCDirector::sharedDirector()->setDepthTest(true);自己对应下,kEAGLColorFormatRGB565,GL_DEPTH24_STENCIL8_OES,cocos2d::CCDirector::sharedDirector()->setDepthTest(true);