cocos2d-x 触屏画线

cocos2d-x版本:2.1.3

原理:当出现屏幕触摸事件时,记录触摸点的坐标,然后通过重载draw()方法,这个方法每帧会画一次屏幕,我们在该方法中将记录的点画出来。

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

#include "Box2D/Box2D.h"

#include "SimpleAudioEngine.h"
USING_NS_CC;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();  
    static cocos2d::CCScene* scene();
    void menuCloseCallback(CCObject* pSender);
    CREATE_FUNC(HelloWorld);
public :
    CCPointArray*pArray;
    virtual void draw();
    virtual bool ccTouchBegan(CCTouch*touch,CCEvent*event);//触摸开始
    virtual void ccTouchMoved(CCTouch*touch,CCEvent*event);//触摸移动
    virtual void ccTouchEnded(CCTouch*touch,CCEvent*event);//触摸结束
    virtual void registerWithTouchDispatcher(void);//注册单点触摸
};

#endif  // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        scene = CCScene::create();
        CC_BREAK_IF(! scene);
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);
        scene->addChild(layer);
    } while (0);
    return scene;
}
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);
        this->addChild(pMenu, 1);
        pArray=CCPointArray::create(20);
        pArray->retain();//增加引用计数,不然会被自动释放
        this->setTouchEnabled(true);
        bRet = true;
    } while (0);

    return bRet;
}
void HelloWorld::draw(){
    //画直线
    glLineWidth(5);
    ccDrawColor4B(0,255,255,255);
    //是不是最后一个点
    bool isFirstPoint=false;
    if(pArray!=NULL){
        int count=pArray->count();
        for(int i=0;i<count;i++){
            //取出一个坐标
            CCPoint point=pArray->getControlPointAtIndex(i);
            //判断是不是松手时的坐标
            if(point.equals(ccp(-1,-1))){
                isFirstPoint=true;
            }else{
                //如果是第一个坐标点或松手时的坐标点画点,否则画直线
                if(i==0||isFirstPoint){
                    ccDrawPoint(ccp(point.x,point.y));
                    isFirstPoint=false;
                }else{
                    CCPoint ps=pArray->getControlPointAtIndex(i-1);
                    ccDrawLine(ccp(point.x,point.y),ccp(ps.x,ps.y )); 
                }
            }
        }
    }

};
//触摸开始
bool HelloWorld::ccTouchBegan(CCTouch*touch,CCEvent*event){
    CCPoint point=touch->getLocationInView();
    point=CCDirector::sharedDirector()->convertToGL(point);
    pArray->addControlPoint(point);
    return true;
};
//触摸移动
void HelloWorld::ccTouchMoved(CCTouch*touch,CCEvent*event){
    CCPoint point =touch->getLocationInView();
    point=CCDirector::sharedDirector()->convertToGL(point);
    pArray->addControlPoint(point);
};
//触摸结束
void HelloWorld::ccTouchEnded(CCTouch*touch,CCEvent*event){
    pArray->addControlPoint(ccp(-1,-1));
};
//注册单点触摸
void HelloWorld::registerWithTouchDispatcher(void){
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
};
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    //pArray->removeAllObjects(); 
}



最后想清除所有的点,实现清空画板的效果,但是没完成。

原文地址:https://www.cnblogs.com/android-qian/p/3384216.html