[cpp] view plain copy bool HelloWorld::init() { if (!Layer::init()) { return false ; } SizevisibleSize=Director::getInstance()-getVisibleSize(); autosprite=Sprite::create( "HelloWorld.png" ); sprite-setPosition(Vec2(visibleSize.width/2,visi
- bool HelloWorld::init()
- {
- if ( !Layer::init() )
- {
- return false;
- }
- Size visibleSize = Director::getInstance()->getVisibleSize();
- auto sprite = Sprite::create("HelloWorld.png");
- sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
- this->addChild(sprite);
- dotNode = DrawNode::create();
- dotNode->retain();
- dotNode->drawDot(Point(0, 0), 4.0f, Color4F(255, 0, 0, 255));
- auto pBg = Sprite::create("bg.png"); //这个作为当“油漆层”
- pBg->setAnchorPoint(Point(0.5, 0.5));
- pBg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
- pRender = RenderTexture::create(visibleSize.width, visibleSize.height);
- pRender->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
- this->addChild(pRender); //渲染纹理层需加入该父节点层
- pRender->begin();
- pBg->visit();
- pRender->end();
- auto listener = EventListenerTouchOneByOne::create();
- listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
- listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
- listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
- return true;
- }
- void HelloWorld::onTouchMoved(Touch *touch, Event *unused_event)
- {
- auto touchPoint = touch->getLocation();
- dotNode->setPosition(touchPoint.x, touchPoint.y);
- //设置混合模式
- BlendFunc blendFunc = { GL_ONE, GL_ZERO };
- dotNode->setBlendFunc(blendFunc);
- //将橡皮擦的像素渲染到画布上,与原来的像素进行混合
- pRender->begin();
- dotNode->visit();
- pRender->end();
- }
效果图: