cocos2dx 3.x虚拟摇杆及其应用

1、虚拟摇杆类(OperateLayer.h)

#include "cocos2d.h"  
using namespace cocos2d;

class OperateLayer : public cocos2d::Layer
{
public:
    OperateLayer();
    ~OperateLayer();
    virtual bool init();
    CREATE_FUNC(OperateLayer);
    void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
    void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
    void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
    Point publicDirection;
    float publicDistance;
private:
    void showJoystick(cocos2d::Point pos);
    void hideJoystick();
    void updateJoystick(cocos2d::Point direction, float distance);
    cocos2d::Sprite *m_pJoystick;
    cocos2d::Sprite *m_pJoystickBg;
};

2、OperateLayer.cpp

#include "OperateLayer.h"  
using namespace cocos2d;
using namespace std;

OperateLayer::OperateLayer() :
m_pJoystick(NULL),
m_pJoystickBg(NULL)
{
}
OperateLayer::~OperateLayer()
{
}
bool OperateLayer::init()
{
    bool ret = false;
    do {        
        CC_BREAK_IF(!Layer::init());
        m_pJoystick = Sprite::create("joystick.png");
        m_pJoystickBg = Sprite::create("joystick_bg.png");
        this->addChild(m_pJoystick);
        this->addChild(m_pJoystickBg);
        auto listener = EventListenerTouchAllAtOnce::create();
        listener->onTouchesBegan = CC_CALLBACK_2(OperateLayer::onTouchesBegan, this);
        listener->onTouchesMoved = CC_CALLBACK_2(OperateLayer::onTouchesMoved, this);
        listener->onTouchesEnded = CC_CALLBACK_2(OperateLayer::onTouchesEnded, this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
        ret = true;
    } while (false);
    return ret;
}
void OperateLayer::showJoystick(Point pos)
{
    m_pJoystick->setPosition(pos);
    m_pJoystickBg->setPosition(pos);
    m_pJoystick->setVisible(true);
    m_pJoystickBg->setVisible(true);
}
void OperateLayer::hideJoystick()
{
    m_pJoystick->setPosition(m_pJoystickBg->getPosition());
    m_pJoystick->setVisible(false);
    m_pJoystickBg->setVisible(false);
}
void OperateLayer::updateJoystick(Point direction, float distance)
{
    Point start = m_pJoystickBg->getPosition();
    if (distance < 78)
    {
        m_pJoystick->setPosition(start + (direction * distance));
        //小范围(移动慢速)
        publicDirection = direction;
        publicDistance = distance;
    }
    else if (distance > 100) {
        m_pJoystick->setPosition(start + (direction * 100));
        //大范围(移动快速)
        publicDirection = direction;
        publicDistance = 100;
    }
    else {
        m_pJoystick->setPosition(start + (direction * 78));
        //中范围(移动中速)
        publicDirection = direction;
        publicDistance = 78;
    }
}
void OperateLayer::onTouchesBegan(const vector<Touch*>& touches, Event *unused_event)
{

    Size winSize = Director::getInstance()->getWinSize();
    vector<Touch*>::const_iterator touchIter = touches.begin();
    while (touchIter != touches.end())
    {
        Touch *pTouch = (Touch*)(*touchIter);
        Point p = pTouch->getLocation();
        if (p.x <= winSize.width / 2)
        {
            //this->showJoystick(p);
        }
        else {

        }
        ++touchIter;
    }
}
void OperateLayer::onTouchesMoved(const vector<Touch*>& touches, Event *unused_event)
{
    Size winSize = Director::getInstance()->getWinSize();
    std::vector<Touch*>::const_iterator touchIter = touches.begin();
    Touch *pTouch = (Touch*)(*touchIter);
    Point start = pTouch->getStartLocation();
    if (start.x > winSize.width / 2)
    {
        return;
    }
    Point dest = pTouch->getLocation();
    float distance = start.getDistance(dest);
    Point direction = (dest - start).getNormalized();
    this->updateJoystick(direction, distance);
}
void OperateLayer::onTouchesEnded(const vector<Touch*>& touches, Event *unused_event)
{
    this->showJoystick(m_pJoystickBg->getPosition());
    publicDistance = 0;
}

3、引用

//精灵初始化
sprite1 = Sprite::create("whiteSprite.png");
sprite1->setPosition(s_centre);
addChild(sprite1);
//虚拟摇杆初始化
operateLayer = OperateLayer::create();
operateLayer->setPosition(Vec2(200, visibleSize.height / 2+100));
addChild(operateLayer);
//启动计时器更新精灵的位置
this->scheduleUpdate();

void HelloWorld::update(float dt){
    //publicDistance除以值可使让移动按比例变慢
    sprite1->setPosition(sprite1->getPosition() + (operateLayer->publicDirection*(operateLayer->publicDistance/10)));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值