91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[cocos2d-x]圖層的旋轉縮放效果

發布時間:2020-07-24 18:54:10 來源:網絡 閱讀:266 作者:蓬萊仙羽 欄目:游戲開發

要實現一個兩個圖層疊加在一起,然后點擊其中的一個圖層,實現另外一個圖層的旋轉縮放的效果。

預期效果:

1.實現兩個layer添加在一個場景中。

2.實現點擊一個場景能實現另一個場景的旋轉縮放的功能。

3.實現layer中精靈的隨機生成和自由走動。

4.實現場景中具有觸摸事件的精靈。

效果圖:

[cocos2d-x]圖層的旋轉縮放效果[cocos2d-x]圖層的旋轉縮放效果

[cocos2d-x]圖層的旋轉縮放效果

實現步驟:

1.首先分析一下這個效果是由兩個圖層組成的,先來實現一下上面的一個黃×××層

GameLayer.h:

#ifndef _______GameLayer__ #define _______GameLayer__  #include <iostream> #include "cocos2d.h" using namespace cocos2d; class GameLayer:public CCLayer { public:     CCPoint gameLayerPosition;     CCPoint lastTouchLocation;     bool init();          CREATE_FUNC(GameLayer);     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);     virtual void registerWithTouchDispatcher(void);          void addRandomThings();     void runRandomMoveSequence(CCNode* node); }; #endif /* defined(_______GameLayer__) */



GameLayer.cpp:

#include "GameLayer.h" #include "HelloWorldScene.h" #include "Spider.h" bool GameLayer::init() {     if (!CCLayer::init()) {         return false;     }               CCSize size = CCDirector::sharedDirector()->getWinSize();     CCSprite * background = CCSprite::create("grass.png");     background->setPosition(CCPointMake(size.width/2, size.height/2));     this->addChild(background);     CCLabelTTF *label = CCLabelTTF::create("GameLayer", "Marker Felt", 44);     label->setColor(ccBLACK);     label->setPosition(CCPointMake(size.width/2, size.height/2));     label->setAnchorPoint(CCPointMake(0.5f, 1));     this->addChild(label);     this->GameLayer::addRandomThings();     this->setTouchEnabled(true);     return true; }  void GameLayer::registerWithTouchDispatcher() {     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); }  bool GameLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {     //記錄點擊下的坐標點     lastTouchLocation = HelloWorld::locationFromTouch(pTouch);          return true; }  void GameLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {     CCPoint currentTouchLocation = HelloWorld::locationFromTouch(pTouch);     //獲得兩點之間的差值     CCPoint moveTo = ccpSub(lastTouchLocation, currentTouchLocation);     moveTo = ccpMult(moveTo, -1);          lastTouchLocation = currentTouchLocation;     //將當前圖層移動到鼠標移動的地方     this->setPosition(ccpAdd(this->getPosition(), moveTo));     CCLog("gameLayertouchMoved"); }  void GameLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {     //恢復一開始的position(gameLayerPosition(0,0))     CCMoveTo* move = CCMoveTo::create(1, gameLayerPosition);     CCEaseIn* ease = CCEaseIn::create(move, 0.5f);     ease->setTag(103);     this->runAction(ease);     CCLog("gameLayertouchEnded"); }  void GameLayer::addRandomThings() {     CCSize screenSize = CCDirector::sharedDirector()->getWinSize();     //向圖層加太陽     for (int i=0; i<4; i++) {         CCSprite* firething = CCSprite::create("firething.png");         firething->setPosition(CCPointMake(CCRANDOM_0_1() * screenSize.width, CCRANDOM_0_1() * screenSize.height));         this->addChild(firething);         this->runRandomMoveSequence(firething);     }     //向圖層加蜘蛛     for (int j=0; j<10; j++) {         //添加用CCNode封裝了的Spider精靈,Spider具有觸摸事件         Spider::spiderWithParentNode(this);     } }  void GameLayer::runRandomMoveSequence(CCNode* node) {     float duration = CCRANDOM_0_1() * 5 + 1;     CCMoveBy* move1 = CCMoveBy::create(duration, CCPointMake(-180, 0));     CCMoveBy* move2 = CCMoveBy::create(duration, CCPointMake(0, -180));     CCMoveBy* move3 = CCMoveBy::create(duration, CCPointMake(180, 0));     CCMoveBy* move4 = CCMoveBy::create(duration, CCPointMake(0, 180));     CCSequence* sequence = (CCSequence*)CCSequence::create(move1,move2,move3,move4,NULL);     CCRepeatForever* repeat = CCRepeatForever::create(sequence);     node->runAction(repeat); }





2.其次實現下面的那個綠色草坪的圖層

UserInterfaceLayer.h:


#ifndef _______UserInterfaceLayer__ #define _______UserInterfaceLayer__  #include <iostream> #include "cocos2d.h" using namespace cocos2d; class UserInterfaceLayer:public CCLayer { public:     bool init();     CREATE_FUNC(UserInterfaceLayer);     bool isTouchForMe(CCPoint touchLocation);     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);     virtual void registerWithTouchDispatcher(void); }; #endif /* defined(_______UserInterfaceLayer__) */

UserInterfaceLayer.cpp:

#include "UserInterfaceLayer.h" #include "HelloWorldScene.h" #include "GameLayer.h" bool UserInterfaceLayer::init() {     if (!CCLayer::init()) {         return false;     }               CCSize size = CCDirector::sharedDirector()->getWinSize();     CCSprite * background = CCSprite::create("ui-frame.png");     background->setPosition(CCPointMake(size.width/2, size.height));     background->setAnchorPoint(CCPointMake(0.5, 1));     this->addChild(background,0,101);          CCLabelTTF* label = CCLabelTTF::create("Here be your Game Scores etc", "Courier", 22);     label->setColor(ccBLACK);     label->setPosition(CCPointMake(size.width/2, size.height));     label->setAnchorPoint(CCPointMake(0.5f, 1));     this->addChild(label);          this->setTouchEnabled(true);     return true; }  void UserInterfaceLayer::registerWithTouchDispatcher() {     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false); }  //判斷是否點擊了我所在的范圍 bool UserInterfaceLayer::isTouchForMe(CCPoint touchLocation) {     CCNode * node = this->getChildByTag(101);     //boundBox方法是返回當前node所占的rect     return node->boundingBox().containsPoint(touchLocation); }  bool UserInterfaceLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {     CCPoint location = pTouch->getLocation();          bool isTouchHandled = this->isTouchForMe(location);     if (isTouchHandled) {         CCNode * node = this->getChildByTag(101);                  ((CCSprite*)node)->setColor(ccRED);     }               GameLayer *gameLayer = HelloWorld::sharedhelloworld()->gameLayer();     CCRotateBy * rotate = CCRotateBy::create(16, 360);     //gameLayer->runAction(rotate);               CCScaleTo * scale1 = CCScaleTo::create(8, 0);     CCScaleTo *scale2 = CCScaleTo::create(8, 1);     CCSequence* sequence = CCSequence::create(scale1,scale2,NULL);     sequence->setTag(111);     gameLayer->stopActionByTag(111);     gameLayer->setRotation(0);     gameLayer->setScale(1);     gameLayer->runAction(rotate);     gameLayer->runAction(sequence);     return true; }  void UserInterfaceLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {     CCNode* node = this->getChildByTag(101);     ((CCSprite*)node)->setColor(ccGREEN);     CCLog("uiLayertouchMoved"); }  void UserInterfaceLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {     CCNode * node = this->getChildByTag(101);     ((CCSprite*)node)->setColor(ccWHITE);     CCLog("uiLayertouchEnded"); }

3.接著是一個帶有觸摸功能的Spider類

Spider.h:

#ifndef _______Spider__ #define _______Spider__  #include <iostream> #include "cocos2d.h" using namespace cocos2d; //CCLayer是默認繼承了觸摸協議的,這里蜘蛛類只要是繼承CCNode類然后繼承CCTargetedTouchDelegate class Spider:public CCNode,public CCTargetedTouchDelegate { public:     CCSprite* spiderSprite;     int numUpdates;     static Spider* spiderWithParentNode(CCNode* parentNode);     bool initWithParentNode(CCNode* parentNode);               //spider觸摸事件     void update(float delta);     void moveAway(float duration,CCPoint moveTo);     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); }; #endif /* defined(_______Spider__) */

Spider.cpp:

#include "Spider.h" #include "HelloWorldScene.h" Spider* Spider::spiderWithParentNode(CCNode* parentNode) {     //類似宏定義CREAT_FUNC創建對象     Spider* pRet = new Spider();     if (pRet&&pRet->initWithParentNode(parentNode)) {         pRet->autorelease();         return pRet;     }     else     {         delete pRet;         pRet = NULL;         return pRet;     } }  bool Spider::initWithParentNode(CCNode* parentNode) //參數指GameLayer層 {     //注意:要執行某個節點的scheduleUpdate方法必須要把它添加到層上去     parentNode->addChild(this);//this指當前具有觸摸事件的node節點     CCSize screenSize = CCDirector::sharedDirector()->getWinSize();     spiderSprite = CCSprite::create("spider.png");     spiderSprite->setPosition(CCPointMake(CCRANDOM_0_1()*screenSize.width, CCRANDOM_0_1()*screenSize.height));     this->addChild(spiderSprite);          //添加觸摸事件     CCDirector* pDirector = CCDirector::sharedDirector();     pDirector->getTouchDispatcher()->addTargetedDelegate(this, -1, true);          this->scheduleUpdate(); }  //點擊蜘蛛隨機移動 void Spider::moveAway(float duration, cocos2d::CCPoint moveTo) {     spiderSprite->stopAllActions();     CCMoveBy* move = CCMoveBy::create(duration, moveTo);     spiderSprite->runAction(move); }  void Spider::update(float delta) {     numUpdates++;     if (numUpdates > 50) {         numUpdates = 0;         CCPoint moveTo = CCPointMake(CCRANDOM_0_1()*200 - 100, CCRANDOM_0_1()*100 - 50);         this->moveAway(2, moveTo);     } }  bool Spider::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {     CCPoint touchLocation = HelloWorld::locationFromTouch(pTouch);          bool isTouchHandled = spiderSprite->boundingBox().containsPoint(touchLocation);     if (isTouchHandled) {         numUpdates = 0;         CCPoint moveTo;         float moveDistance = 60;         //float rand = CCRANDOM_0_1();         if (CCRANDOM_0_1() < 0.25f)         {             moveTo = CCPointMake(moveDistance, moveDistance);         }         else if (CCRANDOM_0_1() <0.5f)         {             moveTo = CCPointMake(-moveDistance, moveDistance);         }         else if(CCRANDOM_0_1() <0.75)         {             moveTo = CCPointMake(moveDistance, -moveDistance);         }         else         {             moveTo = CCPointMake(-moveDistance, -moveDistance);         }         this->moveAway(0.1f, moveTo);     }     return isTouchHandled;     return true; }


4.然后是合成界面,也就是主場景界面

HelloworldScene.h:

#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__  #include "cocos2d.h" #include "GameLayer.h"   using namespace cocos2d; class HelloWorld : public cocos2d::CCLayer { public:     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)     virtual bool init();      // there's no 'id' in cpp, so we recommend to return the class instance pointer     static cocos2d::CCScene* scene();          static CCPoint locationFromTouch(CCTouch* touch);     // preprocessor macro for "static create()" constructor ( node() deprecated )     CREATE_FUNC(HelloWorld);          static HelloWorld* sharedhelloworld();          GameLayer* gameLayer(); };  #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp:

#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include "GameLayer.h" #include "UserInterfaceLayer.h" using namespace cocos2d; using namespace CocosDenshion;  static HelloWorld * helloWorld = NULL;  CCScene* HelloWorld::scene() {     // 'scene' is an autorelease object     CCScene *scene = CCScene::create();      //    // 'layer' is an autorelease object     HelloWorld *layer = HelloWorld::create();      // add layer as a child to scene     scene->addChild(layer);                // return the scene     return scene; }  // on "init" you need to initialize your instance bool HelloWorld::init() {     //////////////////////////////     // 1. super init first     if ( !CCLayer::init() )     {         return false;     }     helloWorld = this;     GameLayer* gameLayer = GameLayer::create();     this->addChild(gameLayer,1,201);          UserInterfaceLayer* userInterfaceLayer = UserInterfaceLayer::create();     this->addChild(userInterfaceLayer,2,202);          return true; }  HelloWorld*HelloWorld::sharedhelloworld() {     return helloWorld; }  GameLayer*HelloWorld::gameLayer() {     CCNode * node = this->getChildByTag(201);     return (GameLayer *)node; }  CCPoint HelloWorld::locationFromTouch(CCTouch* touch) {     return touch->getLocation(); }

×××:http://download.csdn.net/detail/s10141303/6247313
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黎城县| 永和县| 余庆县| 平乡县| 吉水县| 朝阳市| 开阳县| 政和县| 响水县| 宽甸| 繁昌县| 永登县| 宁南县| 卢湾区| 达孜县| 昆山市| 中山市| 淳化县| 基隆市| 平湖市| 汝南县| 焦作市| 酒泉市| 南岸区| 隆回县| 秭归县| 山东省| 巩义市| 罗城| 石河子市| 珠海市| 福鼎市| 民乐县| 商城县| 南皮县| 桂平市| 绥中县| 东兴市| 邮箱| 广东省| 黄陵县|