本来以为引擎会自带这个摇杆类,后来查了一下,Cocos2d-x里面没有,自己写了一个来试试,基本可以使用,感觉还可以,拿出来和大家分享一下,可能还有很多不足之处,请大家指出
头文件:
<pre name="code" >
#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
源文件:
#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/2<m_size.wIDth) { m_plane->setposition(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 / 2<m_size.height) { m_plane->setposition(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,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;}
好,demo本身实现比较简单,只是单纯满足摇杆功能实现,以及简单控制角色移动。效果还不是很好,会加以改进
总结以上是内存溢出为你收集整理的基于Cocos2d-x3.2的虚拟摇杆实现及 *** 控角色移动全部内容,希望文章能够帮你解决基于Cocos2d-x3.2的虚拟摇杆实现及 *** 控角色移动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)