当前位置:首页 > 开发教程 > 手机开发 >

区分上下左右手势cocos2dx

时间:2014-04-30 09:08 来源: 作者: 收藏

void GameLayer::ccTouchEnded(CCTouch *touch,CCEvent *event){ CCLog(end %f,%f,touchStartP.x,touchStartP.y); if (openTouch) { CCPoint touchEndP=touch-getLocation();

 

直接上方法:我是根据触摸开始点和结束点做的方向判断 ,有更好方法的小伙伴请留言给我 ,多谢

 

 

void GameLayer::ccTouchEnded(CCTouch *touch,CCEvent *event){
   
    if (openTouch) {
        CCPoint touchEndP=touch->getLocation();
        
        double d=Distance(touchStartP,touchEndP);
        CCLOG("两点的距离 :%f",d );
        //如果触摸距离超过20判断手势方向
        if (d>20) {
            if (touchStartP.x-touchEndP.x<0&&touchStartP.x-touchEndP.x<touchStartP.y-touchEndP.y&&touchEndP.x-touchStartP.x>touchStartP.y-touchEndP.y) {
                CCLOG("右");
                
                touchRight();
                
            }
            if (touchStartP.x-touchEndP.x>0&&touchStartP.x-touchEndP.x>touchStartP.y-touchEndP.y&&touchEndP.x-touchStartP.x<touchStartP.y-touchEndP.y) {
                CCLOG("左");
                
                touchLeft();
                
            }
            
            if (touchStartP.y-touchEndP.y<0&&touchStartP.y-touchEndP.y<touchStartP.x-touchEndP.x&&touchEndP.y-touchStartP.y>touchStartP.x-touchEndP.x) {
                CCLOG("上");
                
                touchUp();
                
            }
            if (touchStartP.y-touchEndP.y>0&&touchStartP.y-touchEndP.y>touchStartP.x-touchEndP.x&&touchEndP.y-touchStartP.y<touchStartP.x-touchEndP.x) {
                
                touchDown();
                
            }
            
        }
    }
   
    
}

 

 

/************************
 计算两点间距离
 ************************/
double GameLayer::Distance(CCPoint pt1,CCPoint pt2)
{
    double d;
    d=sqrt((pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y));
    return d;
}

已在游戏中使用!

更多

手机开发阅读排行

最新文章