您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x设置屏幕可触摸

2013-10-03 20:39 295 查看
首先 我们在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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: