基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动

本来以为引擎会自带这个摇杆类,后来查了一下,-x里面没有,自己写了一个来试试,基本可以使用,感觉还可以,拿出来和大家分享一下,可能还有很多不足之处,请大家指出
【基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动】头文件:

#ifndef _CONTROLLER_H_#define _CONTROLLER_H_#include "cocos2d.h"USING_NS_CC;class Controller : public Layer{public:bool init();CREATE_FUNC(Controller);static Scene* createScene();bool onTouchBegan(Touch *touch, Event *event);void onTouchMoved(Touch *touch, Event *event);void onTouchEnded(Touch *touch, Event *event);private:Point m_centerPoint;Point m_currentPoint;Point m_PlanePoint;float m_radius;Sprite* m_Sprite;Sprite* m_bgSprite;Sprite* m_plane;Size m_size;void update(float dt);EventListenerTouchOneByOne* m_listener;};#endif

源文件:

基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动

文章插图
#include "Controller.h"#include "VisibleRect.h"Scene* Controller::createScene(){auto scene = Scene::create();auto layer = Controller::create();scene->addChild(layer);return scene;}bool Controller::init(){m_radius = 60.0;m_size = Director::getInstance()->getWinSize();m_PlanePoint = Vec2(m_size.width / 2, m_size.height / 2);auto bg = Sprite::create("image/background.png");bg->setPosition(m_size.width / 2, m_size.height / 2);this->addChild(bg);m_bgSprite = Sprite::create("yellow_round.png");m_bgSprite->setPosition(VisibleRect::leftBottom().x + m_bgSprite->getContentSize().width / 2, VisibleRect::bottom().y + m_bgSprite->getContentSize().height / 2);this->addChild(m_bgSprite,10);auto size = m_bgSprite->getContentSize();m_centerPoint = Vec2(size.width / 2, size.height / 2);m_currentPoint = m_centerPoint;m_Sprite = Sprite::create("CloseSelected.png");m_Sprite->setPosition(m_centerPoint);m_Sprite->setScale(1.5);this->addChild(m_Sprite,10);m_plane = Sprite::create("hero2.png");m_plane->setPosition(m_PlanePoint);this->addChild(m_plane,10);this->scheduleUpdate();m_listener = EventListenerTouchOneByOne::create();m_listener->onTouchBegan = CC_CALLBACK_2(Controller::onTouchBegan, this);m_listener->onTouchMoved = CC_CALLBACK_2(Controller::onTouchMoved, this);m_listener->onTouchEnded = CC_CALLBACK_2(Controller::onTouchEnded, this);_eventDispatcher->addEventListenerWithSceneGraphPriority(m_listener, this);return true;}void Controller::update(float dt){m_Sprite->setPosition(m_currentPoint);//右if (m_currentPoint.x - m_centerPoint.x > 0&&m_plane->getPosition().x+m_plane->getContentSize().width/2setPosition(m_plane->getPosition().x + 3,m_plane->getPosition().y);m_plane->setFlipX(false);}//左if (m_currentPoint.x - m_centerPoint.x < 0 && m_plane->getPosition().x - m_plane->getContentSize().width / 2>0){m_plane->setPosition(m_plane->getPosition().x - 3, m_plane->getPosition().y);m_plane->setFlipX(true);}//上if (m_currentPoint.y - m_centerPoint.y > 0 && m_plane->getPosition().y + m_plane->getContentSize().height / 2setPosition(m_plane->getPosition().x, m_plane->getPosition().y+3);}//下if (m_currentPoint.y - m_centerPoint.y < 0 && m_plane->getPosition().y - m_plane->getContentSize().height / 2>0){m_plane->setPosition(m_plane->getPosition().x, m_plane->getPosition().y - 3);}}bool Controller::onTouchBegan(Touch *touch, Event *event){auto point = touch->getLocationInView();point = Director::getInstance()->convertToGL(point);if (ccpDistance(point, m_centerPoint) > m_radius){return false;}else{m_currentPoint = point; }return true;}void Controller::onTouchMoved(Touch *touch, Event *event){auto point = touch->getLocationInView();point = Director::getInstance()->convertToGL(point);if (ccpDistance(point, m_centerPoint) > m_radius){m_currentPoint = ccpAdd(m_centerPoint, ccpMult(ccpNormalize(ccpSub(point, m_centerPoint)), m_radius));}else{m_currentPoint = point;}}void Controller::onTouchEnded(Touch *touch, Event *event){m_currentPoint = m_centerPoint;}
基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动

文章插图
好,demo本身实现比较简单,只是单纯满足摇杆功能实现,以及简单控制角色移动 。效果还不是很好,会加以改进