Cocos2d-x CCClippingNode

首先,如果程序报 Stencil buffer is not enabled的错误,修改如下:
IOS:
找到AppController.mm类,按下面修改
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];  
  2.     EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]  
  3.                                      pixelFormat: kEAGLColorFormatRGBA8  
  4.                                      depthFormat: GL_DEPTH24_STENCIL8_OES  
  5.                               preserveBackbuffer: NO  
  6.                                       sharegroup: nil  
  7.                                    multiSampling: NO  
  8.                                  numberOfSamples:0 ];  

Android:如下图
找到Cocos2dxGLSurfaceView.java
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //在Cocos2dxGLSurfaceView.java类中  
  2. public Cocos2dxGLSurfaceView(final Context context) {  
  3.     super(context);  
  4.     this.setEGLConfigChooser(5, 6, 5, 0, 16, 8); // 添加此句  
  5.       
  6.     this.initView();  
  7. }  


下面是创建裁剪区域的代码实现:
.h
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5. USING_NS_CC;  
  6.   
  7. enum{  
  8.     clippingNodeTag,  
  9.     spriteTag,  
  10. };  
  11.   
  12. class HelloWorld : public CCLayer  
  13. {  
  14. public:  
  15.     virtual bool init();  
  16.     static CCScene* scene();  
  17.     CREATE_FUNC(HelloWorld);  
  18.       
  19.     CCPoint beganPoint;  
  20.       
  21.     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  22.     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);  
  23.     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  
  24.     virtual void onEnter();  
  25.     virtual void onExit();  
  26. };  
  27.   
  28. #endif // __HELLOWORLD_SCENE_H__  

.cpp
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "HelloWorldScene.h"  
  2. #include "SimpleAudioEngine.h"  
  3.   
  4. using namespace cocos2d;  
  5. using namespace CocosDenshion;  
  6.   
  7. CCScene* HelloWorld::scene()  
  8. {  
  9.     CCScene *scene = CCScene::create();  
  10.     HelloWorld *layer = HelloWorld::create();  
  11.     scene->addChild(layer);  
  12.     return scene;  
  13. }  
  14.   
  15. bool HelloWorld::init()  
  16. {  
  17.     if ( !CCLayer::init() )  
  18.     {  
  19.         return false;  
  20.     }  
  21.       
  22.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  23.       
  24.       
  25.     //添加一背景  
  26.     CCSprite* background = CCSprite::create("background3.png");  
  27.     background->setScale(2);  
  28.     background->setPosition(ccp(size.width/2, size.height/2));  
  29.     this->addChild(background,0);  
  30.       
  31.     //创建一个200x200大小的裁剪区域  
  32.     CCClippingNode* clippingNode = CCClippingNode::create();  
  33.     //设置裁剪区域大小  
  34.     clippingNode->setContentSize(CCSizeMake(200, 200));  
  35.     clippingNode->setAnchorPoint(ccp(0.5, 0.5));  
  36.     clippingNode->setPosition(ccp(size.width/2, size.height/2));  
  37.     this->addChild(clippingNode,1,clippingNodeTag);  
  38.       
  39.     //向裁剪区域中加入内容  
  40.     CCSprite* sprite = CCSprite::create("HelloWorld.png");  
  41.     sprite->setPosition(ccp(clippingNode->getContentSize().width/2, clippingNode->getContentSize().height/2));  
  42.     clippingNode->addChild(sprite,1,spriteTag);  
  43.       
  44.       
  45.     //创建裁剪模板,裁剪节点将按照这个模板来裁剪区域  
  46.     CCDrawNode *stencil = CCDrawNode::create();  
  47.     CCPoint rectangle[4];  
  48.     rectangle[0] = ccp(0, 0);  
  49.     rectangle[1] = ccp(clippingNode->getContentSize().width, 0);  
  50.     rectangle[2] = ccp(clippingNode->getContentSize().width, clippingNode->getContentSize().height);  
  51.     rectangle[3] = ccp(0, clippingNode->getContentSize().height);  
  52.       
  53.     ccColor4F white = {1, 1, 1, 1};  
  54.     //画一个多边形 这画一个200x200的矩形作为模板  
  55.     stencil->drawPolygon(rectangle, 4, white, 1, white);  
  56.     clippingNode->setStencil(stencil);  
  57.       
  58.     //用来设置显示裁剪区域还是非裁剪区域的  
  59.     clippingNode->setInverted(false);//在裁剪区域内显示加入的内容  
  60.       
  61.     return true;  
  62. }  
  63.   
  64. bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  65. {  
  66.     beganPoint = pTouch->getLocation();  
  67.     return true;  
  68. }  
  69. void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)  
  70. {  
  71.     CCPoint distance = ccpSub(pTouch->getLocation(),beganPoint);  
  72.     CCClippingNode* clippingNode = (CCClippingNode*)this->getChildByTag(clippingNodeTag);  
  73.     CCSprite* sprite = (CCSprite*)clippingNode->getChildByTag(spriteTag);  
  74.     sprite->setPosition(ccpAdd(sprite->getPosition(), distance));  
  75.       
  76.     beganPoint = pTouch->getLocation();  
  77. }  
  78. void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)  
  79. {  
  80. }  
  81.   
  82. void HelloWorld::onEnter()  
  83. {  
  84.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);  
  85.     CCLayer::onEnter();  
  86. }  
  87. void HelloWorld::onExit()  
  88. {  
  89.     CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  
  90.     CCLayer::onExit();  
  91. }  

转载http://blog.csdn.net/song_hui_xiang/article/details/22809647


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值