加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:55:05 所属栏目:百科 来源:网络整理
导读:代码如下: #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 obj

代码如下:

#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();  
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读