单点触控事件、

1.实现单点触控事件

bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);


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)
{


}


然后在飞机的.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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值