cocos2dx 3.x 区域画图

时间:2025-04-08 13:34:31

.h文件

    bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
    void onTouchMoved(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);

    void SeveImage(cocos2d::Ref *pSender);
    void ClearImage(cocos2d::Ref *pSender);

    cocos2d::RenderTexture *m_pTarget;
    std::vector<cocos2d::Sprite *> m_pBrushs;
    cocos2d::Rect m_rRect;

.cpp文件

   Size size = Director::getInstance()->getWinSize();
    Vec2 origin = Vec2(size.width * .5f, size.height * .5f);
    //创建画布
    m_pTarget = RenderTexture::create(size.width, size.height , Texture2D::PixelFormat::RGBA8888);
    m_pTarget->setPosition(Vec2(size.width * .5f, size.height * .5f));
    addChild(m_pTarget, -);

    //创建触摸
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
    listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
    listener->setSwallowTouches(true);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    //创建保存 清除按钮
    MenuItemFont::setFontSize();//字体大小..整个 MenuItemFont 类 修改
    MenuItemFont *seve = MenuItemFont::create("Seve Image  ", CC_CALLBACK_1(HelloWorld::SeveImage, this));
    MenuItemFont *clear = MenuItemFont::create("Clear", CC_CALLBACK_1(HelloWorld::ClearImage, this));
    Menu *menu = Menu::create( seve, clear, NULL );
    menu->alignItemsVertically();//垂直对齐
    menu->setPosition(Vec2(size.width - , size.height  - ));
    addChild(menu);

    //创建区域.只能在区域里画图
    m_rRect = Rect(origin.x - size.width * .25f, origin.y - size.height * .25f, size.width * .5f, size.height * .5f);
bool HelloWorld::onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){
    Vec2 location = pTouch->getLocation();

    //当点中区域时才可以传递到moved
    if (m_rRect.containsPoint(location))
        return true;

    return true;
}
void HelloWorld::onTouchMoved(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){

    Vec2 begin = pTouch->getLocation();
    Vec2 end = pTouch->getPreviousLocation();

    //移动出了区域就return
    if (!m_rRect.containsPoint(begin) || !m_rRect.containsPoint(end))
        return;

    //开始画图  std::vector<cocos2d::Sprite *> m_pTarget
    m_pTarget->begin();//开始

    float distance = begin.getDistance(end);//间距
    )
    {
        int d = (int)distance;
        m_pBrushs.clear();

        ; i < d; i++)
        {
            Sprite *spr = Sprite::create("fire.png");
            spr->setColor(Color3B::RED);
            spr->setOpacity();
            m_pBrushs.push_back(spr);

            float difx = end.x - begin.x;
            float dify = end.y - begin.y;
            float delta = (float)i / distance;
            m_pBrushs[i]->setPosition(Vec2(begin.x + (difx * delta), begin.y + (dify * delta)));
            m_pBrushs[i]->setRotation(rand() % );
             / 50.0f) + 0.25f;
            m_pBrushs[i]->setScale(r);
            m_pBrushs[i]->setColor(Color3B(rand() %  + , , ));
            m_pBrushs[i]->visit();
        }
    }
    //结束画图
    m_pTarget->end();
}
void HelloWorld::SeveImage(cocos2d::Ref *pSender){
    log("Seve Image");
    //保存成功会有弹窗..但不知道保存到那个位置了...
    if(m_pTarget->saveToFile("123.png", kCCImageFormatPNG)){

        MessageBox("Print Screen OK!", "Hit");
    }
}
void HelloWorld::ClearImage(cocos2d::Ref *pSender){
    log("Clear");
    //清空画笔
    m_pTarget->clear(, , , );
}