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);
//触屏开始事件
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);
相关文章推荐
- 【iOS-Cocos2d游戏开发之三】CCScene切换的所有特效(27种)以及设置屏幕横竖屏!
- Cocos2d-x游戏开发之设置精灵的触摸范围
- cocos2d 设置屏幕默认方向
- Cocos2d-x 3.0 屏幕触摸及消息分发机制
- cocos2d-x自适应屏幕 和 设置当前层显示区域
- Cocos2D-x游戏开发之九:屏幕触摸一:基本屏幕操作响应
- 【iOS-Cocos2d游戏开发之三】CCScene切换的所有特效(28种)以及设置屏幕横竖屏!
- Cocos2d-x屏幕触摸事件
- 在cocos2d-x jsb/html5中设置触摸代理的方法
- Cocos2D-iphone 开发之 设置屏幕方向(Interface Orientation)
- 【iOS-Cocos2d游戏开发之三】CCScene切换的所有特效(28种)以及设置屏幕横竖屏!
- 【iOS-cocos2d-X 游戏开发之十六】Cocos2dx编译后的Android自动使用(-hd)高清图&设置自适应屏幕
- Cocos2d设置精灵触摸范围
- 【iOS-Cocos2d游戏开发之三】CCScene切换的所有特效(28种)以及设置屏幕横竖屏!
- 【Cocos2d游戏开发之三】CCScene切换的所有特效(27种)以及设置屏幕横竖屏!
- cocos2d怎么设置屏幕朝向?横屏 or 竖屏设置
- 【Cocos2d游戏开发之三】CCScene切换的所有特效(27种)以及设置屏幕横竖屏!
- cocos2d-x 设置屏幕方向 横屏 || 竖屏
- 【Cocos2d游戏开发之三】CCScene切换的所有特效(27种)以及设置屏幕横竖屏!
- Cocos2d-x 3.0 屏幕触摸及消息分发机制