cocos2d-x2.2 遮罩手电筒效果

代码如下:

#include "HelloWorldScene.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! cclayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

        // 1. Add a menu item with "X" image,which is clicked to quit the program.

        // Create a "close" menu item with close icon,it's an auto release object.
        CcmenuItemImage *pCloseItem = CcmenuItemImage::create(
            "Closenormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20,20));

        // Create a menu with the "close" menu item,it's an auto release object.
        Ccmenu* pMenu = Ccmenu::create(pCloseItem,NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu,1);

        // 2. Add a label shows "Hello World".

        // Create a label and initialize with string "Hello World".
        cclabelTTF* pLabel = cclabelTTF::create("Hello World","Arial",24);
        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 - 50));

        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel,1);

        // 3. Add add a splash screen,show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2,size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite,0);

		//create render target  
		CCRenderTexture* pRenderTexture = CCRenderTexture::create( 480,320 );  

		//render the target to black  
		pRenderTexture->begin();  
		gldisable( GL_BLEND );  
		//ccDrawSolidRect( ccp( 0,0 ),ccp( 480,320 ),ccc4f( 0,255,50 ) );  
		ccDrawSolidRect( ccp( 0,1 ) );  
		glEnable( GL_BLEND );  
		pRenderTexture->end();  

		//set the render target over the scene  
		pRenderTexture->setPosition( ccp( 240,160 ) );  
		addChild(pRenderTexture,2,1000 );  

		//render target is the sprite's texture.  
		//set the sprite render parms.  
		//ccBlendFunc bf = { GL_SRC_ALPHA,GL_SRC_ALPHA };  
		ccBlendFunc bf = { GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA  };  
		pRenderTexture->getSprite()->setBlendFunc( bf );  

		setTouchEnabled(true);

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}
void HelloWorld::cctouchesBegan(CCSet *ptouches,CCEvent *pEvent)
{
	if(ptouches->count() == 1)
	{
		CCTouch *pTouch = (CCTouch *) ptouches->anyObject(); 
		CCPoint location = pTouch->getLocation();

		//get transparent area  
		CCPoint ptBottomLeftCorner = ccpsub( pTouch->getLocation(),ccp( 20,20 ) );  
		CCPoint ptTopRightCorner = ccpAdd( pTouch->getLocation(),20 ) );  

		//set the area transparent  
		CCRenderTexture* rt = (CCRenderTexture*)getChildByTag(1000);
		rt->begin();  
		gldisable( GL_BLEND );  
		//ccDrawSolidRect( ptBottomLeftCorner,ptTopRightCorner,50 ) );
		ccDrawSolidRect( ptBottomLeftCorner,0 ) );  
		glEnable( GL_BLEND );  
		rt->end();  
	}
}
void HelloWorld::cctouchesMoved(CCSet *ptouches,CCEvent *pEvent)
{
	if(ptouches->count() == 1)
	{
		CCTouch *pTouch = (CCTouch *) ptouches->anyObject(); 
		//get transparent area  
		CCPoint ptBottomLeftCorner = ccpsub( pTouch->getLocation(),20 ) );  

		CCRenderTexture* rt = (CCRenderTexture*)getChildByTag(1000);
		rt->begin();  
		gldisable( GL_BLEND );  

		//set the render target black  
		//ccDrawSolidRect( CCPointZero,50 ) );  
		ccDrawSolidRect( CCPointZero,1 ) );  
		//set the new area transparent  
		//ccDrawSolidRect( ptBottomLeftCorner,50 ) );  
		ccDrawSolidRect( ptBottomLeftCorner,0 ) );  

		glEnable( GL_BLEND );  
		rt->end();  
	}
}
void HelloWorld::cctouchesEnded(CCSet *ptouches,CCEvent *pEvent)
{
	CCRenderTexture* rt = (CCRenderTexture*)getChildByTag(1000);
	rt->begin();  
	gldisable( GL_BLEND );  
	//ccDrawSolidRect( CCPointZero,50 ) ); 
	ccDrawSolidRect( CCPointZero,1 ) ); 
	glEnable( GL_BLEND );  
	rt->end();  
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...