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

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值