There is nothing to do now. Just write it. Today, I want to share with you how to use a virtual key to walk a role in a map. This is very important in the game. Although a little cumbersome, it is not difficult to achieve. We don't have much to worry about. We started to leave.
How can this problem be solved? First, we add the virtual key in the control direction to create a class named direlayer layer;
In its header file, define the following:
#pragma once#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\cclayer.h"#include "cocos2d.h"#include "DirectionSprite.h"class DirectionLayer :public cocos2d::CCLayer{public:DirectionLayer(void);~DirectionLayer(void);CREATE_FUNC(DirectionLayer);virtual bool init();DirectionSprite* _directionSprite;cocos2d::CCSpriteFrameCache* _directionCache;cocos2d::CCSpriteBatchNode* _directionBatchNode;void initDirectionSprite();};In its. cpp file:
#include "DirectionLayer.h"USING_NS_CC;DirectionLayer::DirectionLayer(void){this->_directionSprite=NULL;this->_directionCache=CCSpriteFrameCache::sharedSpriteFrameCache();this->_directionCache->addSpriteFramesWithFile("dhkey/direction.plist");this->_directionCache->retain();this->_directionBatchNode =CCSpriteBatchNode::create("dhkey/direction.pvr.ccz");this->_directionBatchNode->retain();this->addChild(this->_directionBatchNode);}DirectionLayer::~DirectionLayer(void){this->_directionBatchNode->release();this->_directionCache->release();}bool DirectionLayer::init(){if(!CCLayer::init()){return false;}this->initDirectionSprite();return true;}void DirectionLayer::initDirectionSprite(){this->_directionSprite =DirectionSprite::create();this->_directionBatchNode->addChild(this->_directionSprite);this->_directionSprite->setOpacity(180);this->_directionSprite->setScale(0.30);this->_directionSprite->setPosition(ccp(80,40));}
Then we will create its subnode DirectionSprite class:
In its header file:
#pragma once#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\sprite_nodes\ccsprite.h"#include "cocos2d.h"class DirectionSprite :public cocos2d::CCSprite{public:DirectionSprite(void);~DirectionSprite(void);CREATE_FUNC(DirectionSprite);virtual bool init();};
Its. cpp file:
#include "DirectionSprite.h"USING_NS_CC;DirectionSprite::DirectionSprite(void){}DirectionSprite::~DirectionSprite(void){}bool DirectionSprite::init(){if(!CCSprite::initWithSpriteFrameName("direction.png")){return false;}return true;}Then add it to the DirectionLayer, which has been written above.
Then we add the DirectionLayer layer to the DhInAllLayer layer, which has been written in Chapter 1;
Add a piece of code to DhInAllLayer. h:
#include "DirectionLayer.h"
void directionLayerCreate();DirectionLayer* _directionLayer;
In. cpp:
In the constructor:
DhInAllLayer::DhInAllLayer(void){this->_directionLayer=NULL;}
void DhInAllLayer::directionLayerCreate(){this->_directionLayer=DirectionLayer::create();this->addChild(this->_directionLayer);}
Finally, call the init method:
this->directionLayerCreate();
In this way, we create a Static Virtual key: first look at the effect:
The following is what we want to talk about today: how can we press the virtual key, right-click and right-click to go to the left?
This is a little tedious. Let's proceed step by step.
First, enable touch so that the virtual key can be touched accordingly: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + examples/qsb0tKXD/examples + sLro7o8L3A + outputs = "brush: java;"> void upload (void); void ccTouchesBegan (cocos2d: CCSet * pTouches, cocos2d :: CCEvent * pEvent); void ccTouchesMoved (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent); void Merge (cocos2d: CCSet * pTouches, cocos2d :: CCEvent * pEvent); void update (float dt); // listens to all activities
Add the following code to. cpp and I will directly paste it here, which is tedious:
void RoseLdsMainLayer::registerWithTouchDispatcher(void) { CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,-1);} void RoseLdsMainLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { CCTouch* pTouch = (CCTouch*)pTouches->anyObject();if(this->_dhInAllLayer->_directionLayer->_directionSprite->boundingBox().containsPoint(pTouch->getLocation())){ this->_pmdInAllLayer->_jlmLayer->_jlmSprite->zouLuAnimation();CCPoint DirectionPoint = this->_dhInAllLayer->_directionLayer->_directionSprite->getPosition();CCPoint TouchPoint =pTouch->getLocation();int x = TouchPoint.x-DirectionPoint.x ;if(x>=0){this->_pmdInAllLayer->_jlmLayer->_jlmSprite->_dirSV=ccp(1,0);this->_pmdInAllLayer->_jlmLayer->getJlmSpeedV(this->_pmdInAllLayer->_jlmLayer->_jlmSprite->_dirSV);}else{this->_pmdInAllLayer->_jlmLayer->_jlmSprite->_dirSV=ccp(-1,0);this->_pmdInAllLayer->_jlmLayer->getJlmSpeedV(this->_pmdInAllLayer->_jlmLayer->_jlmSprite->_dirSV);}} } void RoseLdsMainLayer::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) { } void RoseLdsMainLayer::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) { this->_pmdInAllLayer->_jlmLayer->_jlmSprite->_spedV=CCPointZero;this->_pmdInAllLayer->_jlmLayer->_jlmSprite->_jlmState=kYuanDiState;this->_pmdInAllLayer->_jlmLayer->_jlmSprite->yuanDiAnimation();} void RoseLdsMainLayer::update(float dt){this->_pmdInAllLayer->_jlmLayer->jlmRun(dt);}Then we open the JlmSprite. h class in the previous chapter and add the following content to it:
If you have any questions about the role-related content and status, contact me in the group: I directly posted:
Add the following to JlmSprite. h:
In addition to the class, see an enumeration: indicates the current status of the elf Cat:
typedef enum{kYuanDiState,kZouLuState,kTiaoYueState}kJlmState;
In the class:
Void zouLuAnimation (); // inner animation int _ speed; // cocos2d: CCPoint _ difulfill; // cocos2d :: CCPoint _ spedV; // cocos2d: CCPoint _ dirMV; // kJlmState _ jlmState;
In the. cpp constructor:
this->_dirMV=CCPointZero;this->_spedV=CCPointZero;this->_speed=60;this->_jlmState=kYuanDiState
In zoulu Animation:
Void JlmSprite: zouLuAnimation () {this-> _ jlmState = kZouLuState; if (this-> _ jlmState = kZouLuState) {// In-situ animation of the sprite; this-> stopAllActions (); CCArray * jlmArray = CCArray: create (); jlmArray-> retain (); int I; for (I = 1; I <26; I ++) {CCSpriteFrame * jlmFrames = CCSpriteFrameCache: equals ()-> spriteFrameByName (CCString: createWithFormat ("Export dzoulu.png", I)-> getCString ()); jlmArray-> addObject (jlmFrames);} CCAnimation * jlmAnination = CCAnimation: Unknown (jlmArray, 0.02); this-> runAction (CCRepeatForever: create (CCAnimate :: create (jlmAnination )));}}
Next we will add something in its parent class, which is the JlmLayer class:
In. h, add the following content:
Void getJlmSpeedV (cocos2d: CCPoint point); // get the speed void jlmRun (float dt) of the elf cat; // listen to the walking destination of the elf cat
In. cpp:
Void JlmLayer: getJlmSpeedV (cocos2d: CCPoint point) {if (this-> _ isJlmCreate) {this-> _ jlmSprite-> _ spedV = ccp (this-> _ jlmSprite-> _ speed * point. x, this-> _ jlmSprite-> _ speed * point. y); // determine whether to change the role orientation if (this-> _ jlmSprite-> _ spedV. x> 0) {this-> _ jlmSprite-> setScaleX (0.42);} else {this-> _ jlmSprite-> setScaleX (-0.42); }}} void JlmLayer :: jlmRun (float dt) {if (this-> _ isJlmCreate) {this-> _ jlmSprite-> _ dirMV = ccpAdd (this-> _ jlmSprite-> getPosition (), ccpMult (this-> _ jlmSprite-> _ spedV, dt); this-> _ jlmSprite-> setPosition (this-> _ jlmSprite-> _ dirMV );}}
In this way, we can achieve the desired walking through the Virtual key:
First look at the effect:
This is the exchange group of cocos2d-x 367086379, welcome to join the discussion, learning and progress together. Now, let's get here today! 7086379