cocos2d-x 2.0实现绘图功能实例-《你画你猜》--沈大海cocos2d-x教程14

cocos2d-x 2.0实现绘图功能实例-《你画你猜》--沈大海cocos2d-x教程14_第1张图片

#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__

#include "cocos2d.h"
using namespace cocos2d;
class GameScene :
 public cocos2d::CCLayerColor
{
public:
 GameScene(void);
 ~GameScene(void);
 cocos2d::CCArray * m_allPoint;    //保存所有触摸过的点
public:
 virtual bool init(); 
    virtual void onEnter();                      

    virtual void draw(); //绘图

   /////图层事件处理
    virtual bool  ccTouchBegan(CCTouch  *pTouches, CCEvent *pEvent);
    virtual void ccTouchMoved(CCTouch  *pTouches, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch  *pTouches, CCEvent *pEvent);
    virtual void registerWithTouchDispatcher(void);
  /////获取包含本层的窗口 

  static cocos2d::CCScene* scene();
    void menuCloseCallback(CCObject* pSender);//菜单回掉
    CREATE_FUNC(GameScene);
};
#endif

 

#include "GameScene.h"
#include "HelloWorldScene.h"
using namespace cocos2d;
GameScene::GameScene(void)
{}

GameScene::~GameScene(void)
{}
bool GameScene::init(){
 CCLayerColor::init();
 this->m_allPoint=CCArray::create();
 m_allPoint->retain();

////////////返回游戏主界面
 cocos2d::CCLabelTTF * lable=::CCLabelTTF::create("back","Arial",24);
 CCMenuItemLabel * pmenu=CCMenuItemLabel::create(lable,this,menu_selector(GameScene::menuCloseCallback));
 pmenu->setPosition(100,50);
 CCMenu * menu=CCMenu::create(pmenu,NULL);
 this->addChild(menu,1);


 this->setTouchEnabled(true);//启动事件处理
 return true;
}


void GameScene::onEnter(){//该方法暂时没有使用
 CCLayerColor::onEnter();
}                       //开始绘制node
void GameScene::draw(){
 CCLayerColor::draw();
 ::ccDrawColor4B(255,0,0,255);//设定颜色为红色
 ::glLineWidth(5);                         //线条宽度5像素
 if (m_allPoint!=NULL){
  int count=this->m_allPoint->count();

  for(int i=0;i<count;i++)
  { CCPoint * p=(CCPoint*)(this->m_allPoint->objectAtIndex(i));
  if(i==0)
   ccDrawPoint(ccp(p->x,p->y));
  else
  {CCPoint * pstart=(CCPoint*)(this->m_allPoint->objectAtIndex(i-1));
  ::ccDrawLine(ccp(p->x,p->y),ccp(pstart->x,pstart->y ));

  }
  }
 }
}
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
CCScene*  GameScene::scene(){
 CCScene * scene=CCScene::create();
 GameScene * gs=GameScene::create();
 scene->addChild(gs,2);
 return scene;
}
// a selector callback
void GameScene::menuCloseCallback(CCObject* pSender){
 CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());
}

 

//记录按下点
bool  GameScene::ccTouchBegan(CCTouch  *pTouche, CCEvent *pEvent){
 this->m_allPoint->removeAllObjects();
 // CCLayer::ccTouchBegan(pTouche,pEvent);
 CCPoint *p=new CCPoint();
 p->x=pTouche->getLocation( ).x ;
 p->y=pTouche->getLocation().y;

 this->m_allPoint->addObject(p);
 p->release();
 //CCLog("ccTouchesBegan%f,%f",p->x,p->y);
 return true; //这里返回true才能处理移动

}

//移动的时候也要记录
void GameScene::ccTouchMoved(CCTouch  *pTouche, CCEvent *pEvent){
 //CCLog("ccTouchesMoved%f,%f",pTouche->getLocationInView().x,pTouche->getLocationInView().y); //该方法得到的是针对屏幕的坐标左上角0,0

 // pTouche->getLocation()得到的是opengl的坐标
 CCPoint *p=new CCPoint();
 p->x=pTouche->getLocation( ).x ;
 p->y=pTouche->getLocation().y;

 this->m_allPoint->addObject(p);
 p->release();
}
void GameScene::ccTouchEnded(CCTouch  *pTouche, CCEvent *pEvent){
}
void GameScene::registerWithTouchDispatcher()
{  
 CCDirector* pDirector = CCDirector::sharedDirector();
 pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

你可能感兴趣的:(cocos2d-x 2.0实现绘图功能实例-《你画你猜》--沈大海cocos2d-x教程14)