//HelloWorldScene.h фрагмент класса HelloWorld
virtual void onTouchEnded(cocos2d::Touch*, cocos2d::Event*);
virtual void onTouchMoved(cocos2d::Touch*, cocos2d::Event*);
//HelloWorldScene.cpp
void HelloWorld::Init()
{
//...
touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
}
void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
cocos2d::log("touch ended");
}
void HelloWorld::onTouchMoved(Touch* touches, Event* event)
{
cocos2d::log("touch moved");
}
class Line
{
public:
std::vector<cocos2d::CCPoint> curve;
};
class HelloWorld
{
//.....
private:
cocos2d::DrawNode *_drawNode;
std::vector<Line> m_lines;
Line * newLine;
};
bool HelloWorld::Init()
{
//...
_drawNode = DrawNode::create();
this->addChild(_drawNode);
_drawNode->clear();
}
bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
CCPoint touchLocation = touch->getLocationInView();
touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
touchLocation = convertToNodeSpace(touchLocation);
createLine(touchLocation);
return true;
}
void HelloWorld::onTouchMoved(Touch* touch, Event* event)
{
CCPoint touchLocation = touch->getLocationInView();
touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
touchLocation = convertToNodeSpace(touchLocation);
addPoint(touchLocation);
HelloWorld::drawing();
}
void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
deleteLine();
}
void HelloWorld::createLine(cocos2d::CCPoint tloc)
{
newLine = new Line;
(newLine->curve).push_back(tloc);
}
void HelloWorld::deleteLine()
{
delete newLine;
newLine = NULL;
}
void HelloWorld::addPoint(cocos2d::CCPoint tloc)
{
(newLine->curve).push_back(tloc);
}
void HelloWorld::drawing()
{
int m = (newLine->curve).size();
for(int i=1; i < m; i++)
{
_drawNode->drawLine(newLine->curve[i-1], newLine->curve[i], Color4F::BLUE);
}
}