Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主
工程文件TouchesTest.h和TouchesTest.cpp
相关素材文件
事件驱动同样适用于cocos2d-x引擎,cocos2d-x的触屏事件可分为单点和多点触屏。
一般用到情况有:
Layer统一接受触屏消息,然后由程序根据需要分发给不同位置的sprite;
自定义可接收触屏事件的sprite。
Layer层实现触屏事件
1.开启触屏事件
在Layer层初始化的时候设置
setIsTouchEnabled( true );
2.重写(覆盖)父类CCLayer的方法
以下为CCLayer类的CCCtouch相关虚方法
// default implements are used to call script callback if existvirtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);// default implements are used to call script callback if existvirtual 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);
Begin:触屏事件开始
Ended:触屏事件结束
Moved:触屏拖动
根据具体情况,改写自己需要的触屏事件方法。
选择其中的ccTouchesEnded方法,目的实现Sprite射击开火。
1 void TouchesLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 2 { 3 CCSetIterator it; 4 CCTouch* touch; 5 for( it = pTouches->begin(); it != pTouches->end(); it++) 6 { 7 touch = (CCTouch*)(*it); 8 if(!touch) 9 break; 10 CCPoint location = touch->locationInView(); 11 location = CCDirector::sharedDirector()->convertToGL(location); 12 13 //SGQSpriteBatchRepeatAnimation(role2,"Role2Run%d.png",10,0.2f,1); 14 //CCMoveTo* role2Move=CCMoveTo::actionWithDuration(2,location); 15 //role2->runAction(role2Move); 16 17 CCSprite* Role2FireArrow=CCSprite::spriteWithSpriteFrameName("FireArrow0.png"); 18 Role2FireArrow->setPosition(role2->getPosition()); 19 Role2FireArrowBatch= CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2FireArrow.png");//与CCSpriteFrameCache同一纹理 20 Role2FireArrowBatch->addChild(Role2FireArrow); 21 this->addChild(Role2FireArrowBatch,1); 22 23 Role2Fire(Role2FireArrow,location); 24 } 25 }
其中包含方法Role2Fire,实现动画效果
1 void TouchesLayer::Role2Fire(CCSprite* pSprite,CCPoint touchLocation) 2 { 3 //创建逐帧数组 4 CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(8); 5 char str1[100]={0}; 6 for(int i=0;i<8;i++) 7 { 8 sprintf(str1,"FireArrow%d.png",i); 9 CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 ); 10 animFrames1->addObject(pFrame); 11 } 12 CCMutableArray<CCSpriteFrame*>* animFrames2=new CCMutableArray<CCSpriteFrame*>(9); 13 for(int i=0;i<9;i++) 14 { 15 sprintf(str1,"FireArrowExplode%d.png",i); 16 CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 ); 17 animFrames2->addObject(pFrame); 18 } 19 CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.25f); 20 CCAnimation* animation2=CCAnimation::animationWithFrames(animFrames2,0.1f); 21 CCMoveTo* Role2FireArrowMoveTo=CCMoveTo::actionWithDuration(2,touchLocation); 22 CCCallFuncN* role3FunN=CCCallFuncN::actionWithTarget(this,callfuncN_selector(TouchesLayer::removeSprite)); 23 24 //float offY=touchLocation.y-this->getPosition().y; 25 //float offX=touchLocation.x-this->getPosition().x; 26 //float angleRadians=atan2f(offY,offX); 27 //float angleDegrees=CC_RADIANS_TO_DEGREES(angleRadians); 28 //float cocosAngle=-1*angleDegrees; 29 //pSprite->setRotation(cocosAngle); 30 31 pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false)); 32 pSprite->runAction(CCSequence::actions(Role2FireArrowMoveTo,CCAnimate::actionWithAnimation(animation2,false),role3FunN,NULL)); 33 animFrames1->release(); 34 }
注意
坐标系转换,设备中是以左上角为坐标系原点,cococs2d-x以左下角为坐标系原点,所以,在获取坐标点后,需要转换坐标系。
CCPoint location = touch->locationInView();location = CCDirector::sharedDirector()->convertToGL(location);
运行后,触摸屏幕后,Sprite会向着该触点发射开火。
Sprite自定义触屏事件
同样,在CCLayer上实现Touch的效果,使用Sprite自定义触屏事件也可。
1. 创建一个类继承CCSprite和Touch相关接口
要使sprite实现自定义touch必须继承相关的touch接口。
CCTouchDelegate下包含CCTargetedTouchDelegate和CCStandardTouchDelegate委托
2.CCTouch方法中处理事件函数
1 void TouchesSprite::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 2 { 3 CCSetIterator it; 4 CCTouch* touch; 5 for( it = pTouches->begin(); it != pTouches->end(); it++) 6 { 7 touch = (CCTouch*)(*it); 8 if(!touch) 9 break; 10 CCPoint location = touch->locationInView(); 11 location = CCDirector::sharedDirector()->convertToGL(location); 12 13 TouchesRole2Running(this); 14 CCMoveTo* moveTo=CCMoveTo::actionWithDuration(2,location); 15 runAction(moveTo); 16 } 17 }
其中,TouchesRole2Running实现动画,触摸屏幕后,Sprite会跑动到触点位置。
1 void TouchesSprite::TouchesRole2Running(CCSprite* pSprite) 2 { 3 //创建逐帧数组 4 CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(10); 5 char str1[100]={0}; 6 for(int i=0;i<10;i++) 7 { 8 sprintf(str1,"Role2Run%d.png",i); 9 CCSpriteFrame* pFrame=TouchesRole2Cache->spriteFrameByName( str1 ); 10 animFrames1->addObject(pFrame); 11 } 12 CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f); 13 pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false)); 14 animFrames1->release(); 15 }
运行后,触摸屏幕后,Sprite会跑动到触点位置。
Lyaer层和Sprite层实现触摸屏事件,各有各的优点,都可实现触摸效果。
完整代码
1 #ifndef _TOUCHES_TEST_ 2 #define _TOUCHES_TEST_ 3 4 #include "cocos2d.h" 5 6 using namespace cocos2d; 7 8 //------------------------------- 9 // 10 //TouchesScene 11 // 12 //------------------------------- 13 class TouchesScene:public CCScene 14 { 15 public: 16 TouchesScene(); 17 ~TouchesScene(); 18 19 virtual void onEnter(); 20 }; 21 22 //---------------------------- 23 // 24 //TouchesSprite 25 // 26 //---------------------------- 27 class TouchesSprite:public CCSprite,public CCStandardTouchDelegate 28 { 29 public: 30 TouchesSprite(); 31 ~TouchesSprite(); 32 33 static TouchesSprite* SGQSpriteWithSpriteFrameName(const char* spriteFrameName); 34 35 public: 36 virtual void onEnter(); 37 //virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);//ccTouchEnded无效,触摸无反应 38 virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); 39 40 CCSpriteFrameCache* TouchesRole2Cache; 41 void TouchesRole2Running(CCSprite* pSprite); 42 }; 43 44 //------------------------------ 45 // 46 //TouchesLayer 47 // 48 //------------------------------ 49 class TouchesLayer:public CCLayer 50 { 51 public: 52 TouchesLayer(); 53 ~TouchesLayer(); 54 55 //ccTouchEnded出现BUG,触摸无反应 56 //virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); 57 58 virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); 59 60 void Role2Standing(CCSprite* pSprite); 61 void Role2Fire(CCSprite* pSprite,CCPoint touchLocation); 62 void TouchesRole2Standing(CCSprite* pSprite); 63 64 void removeSprite(CCNode* pSender); 65 private: 66 CCSprite* role2; 67 TouchesSprite* touchesRole2; 68 CCSpriteFrameCache* cache; 69 CCSpriteBatchNode* Role2FireArrowBatch; 70 }; 71 72 #endif
1 #include "pch.h" 2 #include "Classes\TouchesTest.h" 3 4 //------------------------------------ 5 // 6 //TouchesLayer 7 // 8 //------------------------------------ 9 TouchesLayer::TouchesLayer() 10 { 11 setIsTouchEnabled( true );//开启触屏事件 12 CCSize s=CCDirector::sharedDirector()->getWinSize(); 13 //Layer层Touches 14 cache=CCSpriteFrameCache::sharedSpriteFrameCache(); 15 cache->addSpriteFramesWithFile("Sprite/Role2/Role2.plist","Sprite/Role2/Role2.png"); 16 cache->addSpriteFramesWithFile("Sprite/Role2/Role2FireArrow.plist","Sprite/Role2/Role2FireArrow.png"); 17 18 role2=CCSprite::spriteWithSpriteFrameName("Role2Stand0.png"); 19 role2->setPosition(CCPointMake(s.width/2,s.height/2)); 20 21 CCSpriteBatchNode* spritebatch1 = CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2.png");//与CCSpriteFrameCache同一纹理 22 spritebatch1->addChild(role2); 23 this->addChild(spritebatch1,1); 24 25 Role2Standing(role2); 26 27 //Sprite层Touches 28 touchesRole2=TouchesSprite::SGQSpriteWithSpriteFrameName("Role2Stand0.png"); 29 touchesRole2->setPosition(CCPointMake(100,s.height-200)); 30 CCSpriteBatchNode* spritebatch2 = CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2.png");//与CCSpriteFrameCache同一纹理 31 spritebatch2->addChild(touchesRole2); 32 this->addChild(spritebatch2,1); 33 34 TouchesRole2Standing(touchesRole2); 35 } 36 37 TouchesLayer::~TouchesLayer() 38 {} 39 40 void TouchesLayer::Role2Standing(CCSprite* pSprite) 41 { 42 //创建逐帧数组 43 CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(8); 44 char str1[100]={0}; 45 for(int i=0;i<8;i++) 46 { 47 sprintf(str1,"Role2Stand%d.png",i); 48 CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 ); 49 animFrames1->addObject(pFrame); 50 } 51 CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f); 52 pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false))); 53 animFrames1->release(); 54 } 55 56 void TouchesLayer::TouchesRole2Standing(CCSprite* pSprite) 57 { 58 //创建逐帧数组 59 CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(8); 60 char str1[100]={0}; 61 for(int i=0;i<8;i++) 62 { 63 sprintf(str1,"Role2Stand%d.png",i); 64 CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 ); 65 animFrames1->addObject(pFrame); 66 } 67 CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f); 68 pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation1,false))); 69 animFrames1->release(); 70 } 71 72 /* 73 ccTouchEnded出现BUG,触摸无反应 74 */ 75 //void TouchesLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) 76 //{ 77 // CCPoint location=pTouch->locationInView(); 78 // location=CCDirector::sharedDirector()->convertToGL(location); 79 // 80 // CCMoveTo* moveTo=CCMoveTo::actionWithDuration(3,location); 81 // role->runAction(moveTo); 82 //} 83 84 void TouchesLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 85 { 86 CCSetIterator it; 87 CCTouch* touch; 88 for( it = pTouches->begin(); it != pTouches->end(); it++) 89 { 90 touch = (CCTouch*)(*it); 91 if(!touch) 92 break; 93 CCPoint location = touch->locationInView(); 94 location = CCDirector::sharedDirector()->convertToGL(location); 95 96 //SGQSpriteBatchRepeatAnimation(role2,"Role2Run%d.png",10,0.2f,1); 97 //CCMoveTo* role2Move=CCMoveTo::actionWithDuration(2,location); 98 //role2->runAction(role2Move); 99 100 CCSprite* Role2FireArrow=CCSprite::spriteWithSpriteFrameName("FireArrow0.png"); 101 Role2FireArrow->setPosition(role2->getPosition()); 102 Role2FireArrowBatch= CCSpriteBatchNode::batchNodeWithFile("Sprite/Role2/Role2FireArrow.png");//与CCSpriteFrameCache同一纹理 103 Role2FireArrowBatch->addChild(Role2FireArrow); 104 this->addChild(Role2FireArrowBatch,1); 105 106 Role2Fire(Role2FireArrow,location); 107 } 108 } 109 110 void TouchesLayer::Role2Fire(CCSprite* pSprite,CCPoint touchLocation) 111 { 112 //创建逐帧数组 113 CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(8); 114 char str1[100]={0}; 115 for(int i=0;i<8;i++) 116 { 117 sprintf(str1,"FireArrow%d.png",i); 118 CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 ); 119 animFrames1->addObject(pFrame); 120 } 121 CCMutableArray<CCSpriteFrame*>* animFrames2=new CCMutableArray<CCSpriteFrame*>(9); 122 for(int i=0;i<9;i++) 123 { 124 sprintf(str1,"FireArrowExplode%d.png",i); 125 CCSpriteFrame* pFrame=cache->spriteFrameByName( str1 ); 126 animFrames2->addObject(pFrame); 127 } 128 CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.25f); 129 CCAnimation* animation2=CCAnimation::animationWithFrames(animFrames2,0.1f); 130 CCMoveTo* Role2FireArrowMoveTo=CCMoveTo::actionWithDuration(2,touchLocation); 131 CCCallFuncN* role3FunN=CCCallFuncN::actionWithTarget(this,callfuncN_selector(TouchesLayer::removeSprite)); 132 133 //float offY=touchLocation.y-this->getPosition().y; 134 //float offX=touchLocation.x-this->getPosition().x; 135 //float angleRadians=atan2f(offY,offX); 136 //float angleDegrees=CC_RADIANS_TO_DEGREES(angleRadians); 137 //float cocosAngle=-1*angleDegrees; 138 //pSprite->setRotation(cocosAngle); 139 140 pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false)); 141 pSprite->runAction(CCSequence::actions(Role2FireArrowMoveTo,CCAnimate::actionWithAnimation(animation2,false),role3FunN,NULL)); 142 animFrames1->release(); 143 } 144 145 void TouchesLayer::removeSprite(CCNode* pSender) 146 { 147 this->removeChild(Role2FireArrowBatch,true); 148 } 149 150 //------------------------------------ 151 // 152 //TouchesSprite 153 // 154 //------------------------------------ 155 156 TouchesSprite::TouchesSprite() 157 { 158 TouchesRole2Cache=CCSpriteFrameCache::sharedSpriteFrameCache(); 159 TouchesRole2Cache->addSpriteFramesWithFile("Sprite/Role2/Role2.plist","Sprite/Role2/Role2.png"); 160 } 161 162 TouchesSprite::~TouchesSprite() 163 {} 164 165 TouchesSprite* TouchesSprite::SGQSpriteWithSpriteFrameName(const char* spriteFrameName) 166 { 167 TouchesSprite* pSprite=new TouchesSprite(); 168 pSprite->initWithSpriteFrameName(spriteFrameName); 169 return pSprite; 170 } 171 172 void TouchesSprite::onEnter() 173 { 174 //CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);//ccTouchEnded无效 175 CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); 176 CCSprite::onEnter(); 177 } 178 179 //ccTouchEnded无效 180 //void TouchesSprite::ccTouchEnded(CCTouch* touch, CCEvent* event) 181 //{ 182 // CCPoint location=touch->locationInView(); 183 // location=CCDirector::sharedDirector()->convertToGL(location); 184 // 185 // CCMoveTo* moveTo=CCMoveTo::actionWithDuration(3,location); 186 // runAction(moveTo); 187 //} 188 189 void TouchesSprite::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 190 { 191 CCSetIterator it; 192 CCTouch* touch; 193 for( it = pTouches->begin(); it != pTouches->end(); it++) 194 { 195 touch = (CCTouch*)(*it); 196 if(!touch) 197 break; 198 CCPoint location = touch->locationInView(); 199 location = CCDirector::sharedDirector()->convertToGL(location); 200 201 TouchesRole2Running(this); 202 CCMoveTo* moveTo=CCMoveTo::actionWithDuration(2,location); 203 runAction(moveTo); 204 } 205 } 206 207 void TouchesSprite::TouchesRole2Running(CCSprite* pSprite) 208 { 209 //创建逐帧数组 210 CCMutableArray<CCSpriteFrame*>* animFrames1=new CCMutableArray<CCSpriteFrame*>(10); 211 char str1[100]={0}; 212 for(int i=0;i<10;i++) 213 { 214 sprintf(str1,"Role2Run%d.png",i); 215 CCSpriteFrame* pFrame=TouchesRole2Cache->spriteFrameByName( str1 ); 216 animFrames1->addObject(pFrame); 217 } 218 CCAnimation* animation1=CCAnimation::animationWithFrames(animFrames1,0.2f); 219 pSprite->runAction(CCAnimate::actionWithAnimation(animation1,false)); 220 animFrames1->release(); 221 } 222 223 //------------------------------------ 224 // 225 //TouchesScene 226 // 227 //------------------------------------ 228 TouchesScene::TouchesScene() 229 {} 230 231 TouchesScene::~TouchesScene() 232 {} 233 234 void TouchesScene::onEnter() 235 { 236 CCScene::onEnter(); 237 CCLayer* touchesLayer=new TouchesLayer(); 238 addChild(touchesLayer); 239 touchesLayer->release(); 240 }
著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主相关推荐
- Cocos2d 3.0继承自Sprite的类在addChild后出现故障
当继承自Sprite的类被addChild到其它的Node里后出现例如以下图问题,说明没有调用父类Sprite::init()的方法.由于父类Sprite里的_textureAtlas须要初始化为nu ...
- 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比(一家之言)
初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比 发布于:2013-07-18 11:00阅读数:1984 ...
- 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比
前言 iOS7 beta发布后,大部分开发者和用户的注意力都集中在了iOS 7的全新UI交互设计界面上.一直负责硬件工业设计的Jony Ive首次全面负责苹果的软件和硬件设计工作,自然要把他自己的设计 ...
- 译文1 手把手教你用cocos2d开发iphone游戏
手把手教你用cocos2d开发iphone游戏-译文1 (2011-07-07 16:37:00) Learning Cocos2d – A Hands On Guide to Building iO ...
- 手把手教你用cocos2d开发iphone游戏-译文1
Learning Cocos2d – A Hands On Guide to Building iOS Gaming 说明:本书的英文版美国当地时间7月8日出版,译文基于该书的Rough Cut版本( ...
- [转]25个增强iOS应用程序性能的提示和技巧
在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的,如果你的程序运行迟钝或缓慢,会招致用户的差评.然而由于iOS设备的局限性,有时候要想获得良好的性能,是很困难的.在开发过程 ...
- 非常优秀的iphone学习文章总结!
This site contains a ton of fun tutorials – so many that they were becoming hard to find! So I put t ...
- 25个增强iOS应用程序性能的提示和技巧 — 中级篇
本文由破船译自:raywenderlich 转载请注明出处:BeyondVincent的博客 _____________ 在开发iOS应用程序时.让程序具有良好的性能是非常关键的.这也是用户所期望的. ...
- 2d游戏地图制作html5,如何通过 Cocos2d-html5 使用砖块地图编辑器
简介 砖块地图编辑器 (Tiled Map Editor) 是一个伟大的工具,用于创建游戏级别和砖块地图. 砖块应用程序在 Qt 应用程序框架的帮助下被写入 c + +,它是免费的. 它允许开发人员创 ...
- ios应用相关的性能优化及参考文档
ios app programming guide: https://developer.apple.com/library/ios/documentation/iphone/conceptual/i ...
最新文章
- 从零开始学_JavaScript_系列(24)——查看对象属性,合并数组
- 【SmartJob】配置说明
- cf不能全屏win7的解决方法_win7电脑输入法不能打字的解决方法
- 没有足够多的数据怎么办?计算机视觉数据增强方法总结
- Java微信公众平台开发(二)--微信服务器post消息体的接收
- 汇编语言第二章知识梳理及思考
- PAT A 1118. Birds in Forest (25)【并查集】
- php基础 简书,PHP入门基础
- 谷歌为什登不上去github_8个月,从中年Web前端到亚马逊百万年薪软件工程师:GitHub最励志计算机自学教程!...
- X86平台下基于grub2+busybo+linux-2.6.36制作linux系统
- 软件项目管理的重点知识
- RC电路 CR电路 理解
- 蜕变测试(metamorphic testing)经典论文阅读 Compiler Validation via Equivalence Modulo Inputs
- 命运更喜欢将丰硕约果实馈赠给那些含着泪微笑的灵魂
- 分享9个超好用的免费工具/软件/网站(一定有你要的)
- 07-PDI(Kettle)源码编译8.2.0.0.R版本
- 【id:179】【20分】C. DS二叉树--赫夫曼树的构建与编码(不含代码框架)
- 这样美化PPT图表,真的好看到爆~
- python 线程退出方法
- ZStack——存储模型:主存储和备份存储