cocos2d-x设置屏幕可触摸

来源:互联网 发布:linux部署php网站 编辑:程序博客网 时间:2024/04/28 19:20

首先 我们在fishscene.pp中声明四个触摸事件的函数

 //触屏开始事件  
    virtual void ccTouchesBegan (CCSet *pTouches, CCEvent *pEvent);  
    //触屏移动事件  
    virtual void ccTouchesMoved (CCSet *pTouches, CCEvent *pEvent);  
    //触屏结束事件  
    virtual void ccTouchesEnded (CCSet *pTouches, CCEvent *pEvent);  
    //触屏取消事件  
    virtual void ccTouchesCancelled (CCSet *pTouches, CCEvent *pEvent);

然后定义这四个函数

void fish::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)  
{  
  //我们用迭代器获得多点触摸的第一个触点  
    CCSetIterator it=pTouches->begin();    
    CCTouch* touch=(CCTouch*)(* it);    
  
    //得到第一个触点的位置  
    //(2.0之后不再需要对取得的位置进行坐标系的转换,即从屏幕坐标系转换GL坐标系)  
CCPoint location=touch->locationInView(0);//getLocation();  
  
    //创建一个精灵,并将其显示在触摸点的位置  
CCSprite* sprite=CCSprite::spriteWithFile("fish7.png");  
    //sprite->setPosition(ccp(location.x,location.y)); 
sprite->setPosition(ccp(0,100));
    this->addChild(sprite,1); 
}  
void fish::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)  
{  
  
}  
void fish::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)  
{  
    //我们用迭代器获得多点触摸的第一个触点  
    CCSetIterator it=pTouches->begin();    
    CCTouch* touch=(CCTouch*)(* it);    
  
    //得到第一个触点的位置  
    //(2.0之后不再需要对取得的位置进行坐标系的转换,即从屏幕坐标系转换GL坐标系)  
CCPoint location=touch->locationInView(0);//getLocation();  
  
    //创建一个精灵,并将其显示在触摸点的位置  
CCSprite* sprite=CCSprite::spriteWithFile("fish7.png");  
    //sprite->setPosition(ccp(location.x,location.y)); 
sprite->setPosition(ccp(90,100));
    this->addChild(sprite,1); 
}  
void fish::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)  
{  
  

然后再在init中设置屏幕可触摸

//设置屏幕可以触摸  
this->CCLayer::setIsTouchEnabled(true);


原创粉丝点击