Trouble using RenderTexture and Sprite to draw on screen

Hi. I’m making an application for android phones, and I am trying to allow the user to draw in a box for part of it. I found some code online that supposedly does what I need it to do, but I am having trouble editing and implementing it to work properly. My code compiles, but the sprites are not actually drawn on the screen. Here is the code:


In the constructor:

target = RenderTexture::create(visibleSize.width, visibleSize.height, CCTexture2DPixelFormat::RGBA8888);
target->retain();
target->setPosition(visibleSize.width / 2, visibleSize.height / 2);

this->addChild(target);

brush = Sprite::create("Brush Sprite.png");
brush->retain();

In the Header:

void onTouchesMoved(cocos2d::Set* touches, cocos2d::Event* event);

cocos2d::RenderTexture *target;
cocos2d::Sprite *brush;

Implementation of onTouchesMoved:

void TenthScene::onTouchesMoved(Set* touches, Event* event)
{
Touch *touch = (Touch *)touches->anyObject();
Point start = touch->getLocationInView();
start = Director::sharedDirector()->convertToGL(start);
Point end = touch->getPreviousLocationInView();
end = Director::sharedDirector()->convertToGL(end);

target->begin();

float distance = ccpDistance(start, end);

for (int i = 0; i < distance; i++)
{
    float difx = end.x - start.x;
    float dify = end.y - start.y;
    float delta = (float)i / distance;
    brush->setPosition(start.x + (difx * delta), start.y + (dify * delta));
    brush->setZOrder(50);
    CCLog("Drawing");

    brush->visit();
}

target->end();

}


CCLog(“Drawing”) is not being called, so something must be preventing onTouchesMoved from being called. Help me please, if you can :frowning:. I suck.

I use this ccTouchMoved:

in .h:

virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);

in .cpp:

 void HUD_Signature::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
    {
        CCPoint start = pTouch->getLocationInView();
        start = CCDirector::sharedDirector()->convertToGL(start);
        CCPoint end = pTouch->getPreviousLocationInView();
        end = CCDirector::sharedDirector()->convertToGL(end);
        target->begin();
    
        float distance = ccpDistance(start, end);
    
        for (int i = 0; i < distance; i++)
        {
            float difx = end.x - start.x;
            float dify = end.y - start.y;
            float delta = (float)i / distance;
            brush->setPosition(
                ccp(start.x + (difx * delta), start.y + (dify * delta)));
    
            brush->visit();
        }
    
        target->end();
    }

Moreover, I would suggest you to use a brush as a line dot and not a sprite:

brush = CCDrawNode::create();
    brush->drawDot(
        CCPointZero,
        1.0f,
        ccc4FFromccc4B(Font_Color_Black));

Edit: I see that I have to also implement onTouchBegan and onTouchEnded functions. I will do this now.

I am now using your code, but changed ccTouchMoved to onTouchMoved, because it was producing an error for me. Unfortunately, it still seems as though the function is not being called at all (I am checking using CCLog().) Do I need to set up a touch listener? I was assuming that “onTouchMoved” and similar functions included listeners.