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 exist
    virtual 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 exist
    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);

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,实现动画效果

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层实现触摸屏事件,各有各的优点,都可实现触摸效果。

完整代码

TouchesTest.h
 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
TouchesTest.cpp
  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 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

posted @ 2013-04-24 17:25  Ghost Soar  阅读(1663)  评论(4编辑  收藏  举报