菜鸟生涯的记忆(3)
cocos2d_x 实现的制作画板
.h
class Drawingboard:public Layer
{
public:
static Scene* createScene1();
void men(Ref * pSender);
bool init();
CREATE_FUNC(Many1);
void changcolor1(Ref*pSender);
void changcolor2(Ref*pSender);
void changcolor3(Ref*pSender);
void changcolor4(Ref*pSender);
virtual bool Began(Touch *Touch, Event *Event) ;
virtual void Moved(Touch *Touch, Event *Event) ;
virtual void Ended(Touch *Touch, Event *Event);
virtual void Cancelled(Touch *Touch, Event *Event);
void onEnter1();
void onCealer1(Ref*pSender);
private:
DrawNode* node;
Color4F color;
};
.ccp 的实现;
ode = DrawNode::create();
this->addChild(node);
auto color1 = MenuItemFont::create("红色");
color1->setColor(Color3B(255,0,0));
color1->setPosition(40,20);
menu->addChild(color1);
color1->setCallback(CC_CALLBACK_1(Drawingboard::changcolor1, this));
auto color2 = MenuItemFont::create("黑色");
color2->setColor(Color3B(0,0,1));
color2->setPosition(40,70);
menu->addChild(color2);
color2->setCallback(CC_CALLBACK_1(Drawingboard::changcolor3, this));
auto color3 = MenuItemFont::create("蓝色");
color3->setColor(Color3B(135,206,235));
color3->setPosition(40,130);
menu->addChild(color3);
color3->setCallback(CC_CALLBACK_1(Drawingboard::changcolor2, this));
auto color4 = MenuItemFont::create("军绿");
color4->setColor(Color3B(0,250,154));
color4->setPosition(40,190);
menu->addChild(color4);
color4->setCallback(CC_CALLBACK_1(Drawingboard::changcolor4, this));
onEnter1();
return true;
}
bool Drawingboard:: Began(Touch *Touch, Event *Event) {
auto s = Touch->getLocation();
node->drawPoint(Vec2(s.x,s.y), 10, color);
return true;
log("123");
}
void Drawingboard:: Moved(Touch *Touch, Event *Event) {
auto s = Touch->getPreviousLocation();
auto y = Touch->getLocation();
node->drawLine(s, y, color);//Color4F(0,1,0,1)
log("ppp");
;}
void Drawingboard:: Ended(Touch *Touch, Event *Event){
log("dddd");}
void Drawingboard:: Cancelled(Touch *Touch, Event *Event){
;
}
void Drawingboard::onEnter1(){
this->_touchEnabled = true;
auto Disrter = Director::getInstance()->getEventDispatcher();
auto tocher = EventListenerTouchOneByOne::create();
tocher->setSwallowTouches(true);//吞噬触摸
tocher->onTouchBegan = CC_CALLBACK_2(Drawingboard::Began, this);
tocher->onTouchMoved = CC_CALLBACK_2(Drawingboard::Moved, this);
tocher->onTouchEnded = CC_CALLBACK_2(Drawingboard::Ended, this);
tocher->onTouchCancelled = CC_CALLBACK_2(Drawingboard::Cancelled, this);
Disrter->addEventListenerWithSceneGraphPriority(tocher, this);
}
void Drawingboard::onCealer1(Ref*pSender)
{
node->clear();
}
void Drawingboard:: changcolor1(Ref*pSender)
{
// 红
color = Color4F(1,0,0,1);
}
void Drawingboard:: changcolor2(Ref*pSender)
{
// 蓝色
color = Color4F(0,0,1,1) ;
}
void Drawingboard:: changcolor3(Ref*pSender)
{
// 黑色
color = Color4F(0.1,0.2,0.235,2);
}
void Drawingboard:: changcolor4(Ref*pSender)
{
// 军绿
color = Color4F(0.2,0.5,0,1) ;
}
代码需精简