利用CCClippingNode创建裁剪区域以及Stencil buffer is not enabled错误解决

感谢点评与关注,欢迎转载与分享。
勤奋努力,持之以恒!


首先,如果程序报 Stencil buffer is not enabled的错误,修改如下:
IOS:
找到AppController.mm类,按下面修改
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
                                     pixelFormat: kEAGLColorFormatRGBA8
                                     depthFormat: GL_DEPTH24_STENCIL8_OES
                              preserveBackbuffer: NO
                                      sharegroup: nil
                                   multiSampling: NO
                                 numberOfSamples:0 ];

Android:如下图
//在Cocos2dxGLSurfaceView.java类中
public Cocos2dxGLSurfaceView(final Context context) {
    super(context);
    this.setEGLConfigChooser(5, 6, 5, 0, 16, 8); // 添加此句
    
    this.initView();
}


利用CCClippingNode创建裁剪区域以及Stencil buffer is not enabled错误解决_第1张图片

下面是创建裁剪区域的代码实现:
.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
USING_NS_CC;

enum{
    clippingNodeTag,
    spriteTag,
};

class HelloWorld : public CCLayer
{
public:
    virtual bool init();
    static CCScene* scene();
    CREATE_FUNC(HelloWorld);
    
    CCPoint beganPoint;
    
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    virtual void onEnter();
    virtual void onExit();
};

#endif // __HELLOWORLD_SCENE_H__

.cpp
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    
    //添加一背景
    CCSprite* background = CCSprite::create("background3.png");
    background->setScale(2);
    background->setPosition(ccp(size.width/2, size.height/2));
    this->addChild(background,0);
    
    //创建一个200x200大小的裁剪区域
    CCClippingNode* clippingNode = CCClippingNode::create();
    //设置裁剪区域大小
    clippingNode->setContentSize(CCSizeMake(200, 200));
    clippingNode->setAnchorPoint(ccp(0.5, 0.5));
    clippingNode->setPosition(ccp(size.width/2, size.height/2));
    this->addChild(clippingNode,1,clippingNodeTag);
    
    //向裁剪区域中加入内容
    CCSprite* sprite = CCSprite::create("HelloWorld.png");
    sprite->setPosition(ccp(clippingNode->getContentSize().width/2, clippingNode->getContentSize().height/2));
    clippingNode->addChild(sprite,1,spriteTag);
    
    
    //创建裁剪模板,裁剪节点将按照这个模板来裁剪区域
    CCDrawNode *stencil = CCDrawNode::create();
    CCPoint rectangle[4];
    rectangle[0] = ccp(0, 0);
    rectangle[1] = ccp(clippingNode->getContentSize().width, 0);
    rectangle[2] = ccp(clippingNode->getContentSize().width, clippingNode->getContentSize().height);
    rectangle[3] = ccp(0, clippingNode->getContentSize().height);
    
    ccColor4F white = {1, 1, 1, 1};
    //画一个多边形 这画一个200x200的矩形作为模板
    stencil->drawPolygon(rectangle, 4, white, 1, white);
    clippingNode->setStencil(stencil);
    
    //用来设置显示裁剪区域还是非裁剪区域的
    clippingNode->setInverted(false);//在裁剪区域内显示加入的内容
    
    return true;
}

bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    beganPoint = pTouch->getLocation();
    return true;
}
void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
    CCPoint distance = ccpSub(pTouch->getLocation(),beganPoint);
    CCClippingNode* clippingNode = (CCClippingNode*)this->getChildByTag(clippingNodeTag);
    CCSprite* sprite = (CCSprite*)clippingNode->getChildByTag(spriteTag);
    sprite->setPosition(ccpAdd(sprite->getPosition(), distance));
    
    beganPoint = pTouch->getLocation();
}
void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
}

void HelloWorld::onEnter()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);
    CCLayer::onEnter();
}
void HelloWorld::onExit()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
    CCLayer::onExit();
}

利用CCClippingNode创建裁剪区域以及Stencil buffer is not enabled错误解决_第2张图片


你可能感兴趣的:(利用CCClippingNode创建裁剪区域以及Stencil buffer is not enabled错误解决)