cocos2dx 2.x 动态纹理创建CCRenderTexture

bool RenderTest::init()
{
    if(!CCLayer::init())
    {
        return false;
    }
    this->setTouchEnabled(true);
    size=CCDirector::sharedDirector()->getWinSize();
    CCSprite* background=CCSprite::create("background.jpg");
    background->setPosition(ccp(size.width*0.5,size.height*0.5));
    this->addChild(background);
    
    CCRenderTexture* renderTexture=CCRenderTexture::create(200, 300);//指定画布的大小
    renderTexture->beginWithClear(0, 23, 5, 200);//调用begin()开始在画布上绘制纹理
    renderTexture->end();//渲染完成
    
    dynamicTexture=CCSprite::createWithTexture(renderTexture->getSprite()->getTexture());//使用动态纹理创建精灵
    dynamicTexture->setPosition(ccp(size.width*0.5, size.height*0.5));
    this->addChild(dynamicTexture);
        
    return true;
}


void RenderTest::registerWithTouchDispatcher()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
}


bool RenderTest::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    CCPoint currentPoint=convertTouchToNodeSpace(pTouch);
    if(dynamicTexture->boundingBox().containsPoint(currentPoint))
    {
        srandom(time(NULL));
        int r=random()%255;
        int g=random()%255;
        int b=random()%255;
        dynamicTexture->setColor(ccc3(r, g, b));
    }
    
    return true;
}


void RenderTest::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
    CCPoint currentPoint=convertTouchToNodeSpace(pTouch);
    dynamicTexture->setPosition(currentPoint);
}


void RenderTest::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
    
}

你可能感兴趣的:(cocos2d-x)