标签:style blog http color io os ar 2014 art
游戏不同于影音,强交互性是其一大特色,在游戏中主要体现为接受用户的输入并响应。智能手机触摸是其重要的输入方式。
在cocos2d-x中,触摸分为单点触摸和多点触摸。
单点触摸:主要继承CCTargetedTouchDelegate 实现。
多点触摸:主要继承CCStandardTouchDelegate实现。
MyScene.h
1 #ifndef MyScene_H_H 2 #define MyScene_H_H 3 4 #include "cocos2d.h" 5 using namespace cocos2d; 6 7 class MyScene : public CCLayer 8 { 9 public: 10 static CCScene* createScene(); 11 virtual bool init(); 12 CREATE_FUNC( MyScene ); 13 14 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); 15 virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); 16 virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); 17 virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); 18 19 virtual void registerWithTouchDispatcher(); 20 21 22 private: 23 }; 24 25 #endif
MyScene.cpp
1 #include "MyScene.h" 2 3 CCScene* MyScene::createScene() 4 { 5 CCScene *scene = CCScene::create(); 6 MyScene *layer = MyScene::create(); 7 scene->addChild(layer); 8 return scene; 9 }; 10 11 12 bool MyScene::init() 13 { 14 if( !CCLayer::init() ){ 15 return false; 16 } 17 18 CCSize size = CCDirector::sharedDirector()->getWinSize(); 19 CCSprite *sprite = CCSprite::create("pal4.png"); 20 sprite->setAnchorPoint( ccp(0.5, 0.5) ); 21 //sprite->setPosition( ccp(size.width/2, size.height/2) ); 22 sprite->setPosition( ccp(size.width/2, size.height/2) ); 23 sprite->setScale(0.5f); 24 sprite->setTag(2345); 25 addChild(sprite); 26 27 setTouchEnabled(true); 28 29 return true; 30 } 31 32 bool MyScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) 33 { 34 CCPoint point = pTouch->getLocationInView(); 35 point = CCDirector::sharedDirector()->convertToGL(point); 36 CCNode *node = getChildByTag(2345); 37 float x = node->getPositionX(); 38 float y = node->getPositionY(); 39 float width = node->getContentSize().width*node->getScale(); 40 float height = node->getContentSize().height*node->getScale(); 41 //CCRect rect = CCRectMake(node->getPositionX()-node->getContentSize().width/2, node->getPositionY()-node->getContentSize().height/2, 42 // node->getContentSize().width, node->getContentSize().height); 43 CCRect rect = CCRectMake(x-width/2, y-height/2, width, height); 44 45 //CCLog("Touch start! %.1f,%.1f", point.x, point.y); 46 //CCMessageBox("start","info"); 47 //return true; 48 49 if( rect.containsPoint(point) ){ 50 CCLog("Touch start! %.1f,%.1f", point.x, point.y); 51 return true; 52 } 53 54 return false; 55 } 56 57 void MyScene::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) 58 { 59 CCPoint point = pTouch->getLocation(); 60 CCLog("Touch moved! %.1f,%.1f", point.x, point.y); 61 } 62 63 void MyScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) 64 { 65 CCPoint point = pTouch->getLocation(); 66 CCLog("Touch ended! %.1f, %.1f", point.x, point.y); 67 } 68 69 void MyScene::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) 70 { 71 CCPoint point = pTouch->getLocation(); 72 CCLog("Touch canceled! %.1f, %.1f", point.x, point.y); 73 } 74 75 void MyScene::registerWithTouchDispatcher() 76 { 77 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 78 }
运行结果:
在TouchStart中,判断按下的点是否在图片显示区域内。如果在,则打出log,并返回true,然后CCTouchMove、CCTouchEnded等事件才会被响应;否则,不会响应。
Touches标准型。多点触摸时,会将所有的触摸点放进一个CCSet中。
1 void MyScene::registerWithTouchDispatcher() 2 { 3 //CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 4 CCLayer::registerWithTouchDispatcher(); 5 } 6 7 8 void MyScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) 9 { 10 if( pTouches->count() == 1 ){ 11 CCTouch *touch = (CCTouch*)(*( pTouches->begin() )); 12 13 CCPoint point = touch->getLocationInView(); 14 point = CCDirector::sharedDirector()->convertToGL(point); 15 CCNode *node = getChildByTag(2345); 16 float x = node->getPositionX(); 17 float y = node->getPositionY(); 18 float width = node->getContentSize().width*node->getScale(); 19 float height = node->getContentSize().height*node->getScale(); 20 CCRect rect = CCRectMake(x-width/2, y-height/2, width, height); 21 22 //CCLog("Touches start! %.1f,%.1f", point.x, point.y); 23 24 if( rect.containsPoint(point) ){ 25 CCLog("Touches start! %.1f,%.1f", point.x, point.y); 26 } 27 } 28 29 //return false; 30 } 31 32 void MyScene::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) 33 { 34 if( pTouches->count() == 1 ){ 35 CCTouch *touch = (CCTouch*)(*( pTouches->begin() )); 36 CCPoint point = touch->getLocationInView(); 37 CCLog("Touches moved! %.1f,%.1f", point.x, point.y); 38 } 39 } 40 41 void MyScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 42 { 43 if( pTouches->count() == 1 ){ 44 CCTouch *touch = (CCTouch*)(*( pTouches->begin() )); 45 CCPoint point = touch->getLocationInView(); 46 CCLog("Touches ended! %.1f,%.1f", point.x, point.y); 47 } 48 } 49 50 void MyScene::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) 51 { 52 if( pTouches->count() == 1 ){ 53 CCTouch *touch = (CCTouch*)(*( pTouches->begin() )); 54 CCPoint point = touch->getLocationInView(); 55 CCLog("Touches canceled! %.1f,%.1f", point.x, point.y); 56 } 57 }
和标准CCTouch基本相同,只是在注册时,调用一次CCLayer的触摸注册函数即可。
运行结果:
标签:style blog http color io os ar 2014 art
原文地址:http://www.cnblogs.com/MiniHouse/p/3971566.html