Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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();  
}

大佬总结

以上是大佬教程为你收集整理的cocos2d-x2.2 遮罩手电筒效果全部内容,希望文章能够帮你解决cocos2d-x2.2 遮罩手电筒效果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。