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
. I suck.