1.实现单点触控事件
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
void registerWithTouchDispatcher(); //注册单点触控事件
void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
void registerWithTouchDispatcher(); //注册单点触控事件
在.cpp中进行实现
bool GameLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
return true;
}
void GameLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
if(this->planeLayer->isAlive){ //如果飞机还活着的话
CCPoint beginPoint=pTouch->getLocationInView();
beginPoint=CCDirector::sharedDirector()->convertToGL(beginPoint); //转换为opgl下的坐标
CCRect planeRect=this->planeLayer->getChildByTag(AIRPLANE)->boundingBox(); //获得飞机的碰撞范围
planeRect.origin.x-=15;
planeRect.origin.y-=15;
planeRect.size.width+=30;
planeRect.size.height+=30;
//判断一下触摸点是否在飞机的碰撞范围内
if(planeRect.containsPoint(this->getParent()->convertTouchToNodeSpace(pTouch))) {
CCPoint endPoint=pTouch->getPreviousLocationInView();
endPoint=CCDirector::sharedDirector()->convertToGL(endPoint);
//算一下差
CCPoint offSet=ccpSub(beginPoint,endPoint);
//算出飞机真正需要移动的距离
CCPoint toPoint=ccpAdd(this->planeLayer->getChildByTag(AIRPLANE)->getPosition(),offSet);
//真正移动
this->planeLayer->MoveTo(toPoint);
}
}
}
void GameLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
}
{
return true;
}
void GameLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
if(this->planeLayer->isAlive){ //如果飞机还活着的话
CCPoint beginPoint=pTouch->getLocationInView();
beginPoint=CCDirector::sharedDirector()->convertToGL(beginPoint); //转换为opgl下的坐标
CCRect planeRect=this->planeLayer->getChildByTag(AIRPLANE)->boundingBox(); //获得飞机的碰撞范围
planeRect.origin.x-=15;
planeRect.origin.y-=15;
planeRect.size.width+=30;
planeRect.size.height+=30;
//判断一下触摸点是否在飞机的碰撞范围内
if(planeRect.containsPoint(this->getParent()->convertTouchToNodeSpace(pTouch))) {
CCPoint endPoint=pTouch->getPreviousLocationInView();
endPoint=CCDirector::sharedDirector()->convertToGL(endPoint);
//算一下差
CCPoint offSet=ccpSub(beginPoint,endPoint);
//算出飞机真正需要移动的距离
CCPoint toPoint=ccpAdd(this->planeLayer->getChildByTag(AIRPLANE)->getPosition(),offSet);
//真正移动
this->planeLayer->MoveTo(toPoint);
}
}
}
void GameLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
}
然后在飞机的.cpp中加入MoveTo(CCPoint location){
方法
void PlaneLayer::MoveTo(CCPoint location){
//���б߽��ж�
if(isAlive && !CCDirector::sharedDirector()->isPaused())//�Ѿ���NoTouchLayer��
{
CCPoint actualPoint;
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
CCSize planeSize=this->getChildByTag(AIRPLANE)->getContentSize();
if (location.x<planeSize.width/2)
{
location.x=planeSize.width/2;
}
if (location.x>winSize.width-planeSize.width/2)
{
location.x=winSize.width-planeSize.width/2;
}
if (location.y<planeSize.height/2)
{
location.y=planeSize.width/2+10;
}
if (location.y>winSize.height-planeSize.height/2)
{
location.y=winSize.height-planeSize.height/2;
}
this->getChildByTag(AIRPLANE)->setPosition(location);
}
}
//���б߽��ж�
if(isAlive && !CCDirector::sharedDirector()->isPaused())//�Ѿ���NoTouchLayer��
{
CCPoint actualPoint;
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
CCSize planeSize=this->getChildByTag(AIRPLANE)->getContentSize();
if (location.x<planeSize.width/2)
{
location.x=planeSize.width/2;
}
if (location.x>winSize.width-planeSize.width/2)
{
location.x=winSize.width-planeSize.width/2;
}
if (location.y<planeSize.height/2)
{
location.y=planeSize.width/2+10;
}
if (location.y>winSize.height-planeSize.height/2)
{
location.y=winSize.height-planeSize.height/2;
}
this->getChildByTag(AIRPLANE)->setPosition(location);
}
}