日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

發布時間:2025/3/20 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文: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,實現動畫效果

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 原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者博客鏈接,謝謝!

總結

以上是生活随笔為你收集整理的Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。