几种抽奖方式之轮盘抽奖

最近做项目使用到了抽奖功能,于是把抽奖模块独立出来了,以便重复利用,资源来自http://www.cnblogs.com/zisou/p/cocos2d-xZhuanpan.html。下面直接上代码:

#ifndef __Wheel__CWheelLayer__
#define __Wheel__CWheelLayer__

#include <stdio.h>
#include "cocos2d.h"
#include "../cocos2d/cocos/editor-support/cocostudio/CocoStudio.h"
#include "../cocos2d/cocos/ui/CocosGUI.h"
using namespace cocostudio;
using namespace ui;
USING_NS_CC;

class CWheelLayer:public Layer
{
public:
    CWheelLayer();
    ~CWheelLayer();
    bool init();
    CREATE_FUNC(CWheelLayer);
    static Scene* createScene();
    void update(float dt);
private:
    Sprite *_wheel;
    //转盘状态0静止,1加速,2减速
    int _state;
    float _speed;
    float _speedAcc;
    //目标 设置index即为结果
    int _index;
    float _targetRo;
    //控制音效
    int _soundCount1;
    int _soundCount2;
};

#endif /* defined(__Wheel__CWheelLayer__) */

//
//  CWheelLayer.cpp
//  Wheel
//
//  Created by xujw on 15/6/29.
//
//

#include "CWheelLayer.h"
#include "CSoundTools.h"

CWheelLayer::CWheelLayer():_wheel(nullptr)
                            ,_state(0)
                            ,_speed(0)
                            ,_speedAcc(0.05)
                            ,_targetRo(0)
                            ,_index(0)
                            ,_soundCount1(0)
                            ,_soundCount2(0)
{
}
CWheelLayer::~CWheelLayer()
{
}

bool CWheelLayer::init()
{
    if (!Layer::init())
    {
        return false;
    }

    auto node = CSLoader::createNode("wheel.csb");
    this->addChild(node);

    auto close = dynamic_cast<Button*>(node->getChildByName("btn_close"));
    close->addTouchEventListener([this,close](Ref* tar,Widget::TouchEventType type)
                                 {
                                     if (type == Widget::TouchEventType::ENDED)
                                     {
                                         CCLOG("close...");
                                         Director::getInstance()->popScene();
                                     }
                                 });

    _wheel = dynamic_cast<Sprite*>(node->getChildByName("sprite_wheel"));
    _wheel->setRotation(12);

    auto spin = dynamic_cast<Button*>(node->getChildByName("btn_spin"));
    spin->addTouchEventListener([this](Ref* tar,Widget::TouchEventType type)
                                 {
                                     if (type == Widget::TouchEventType::ENDED)
                                     {
                                         if (_state==0)
                                         {
                                             CCLOG("spin...");
                                             _state = 1;
                                             _index = arc4random()%18;
                                             _targetRo = _index*20 + 12 + 360*2;
                                             _speed = 0;
                                             _soundCount1 = 0;
                                             _soundCount2 = 0;
                                         }
                                     }
                                 });

    scheduleUpdate();

    return true;
}

Scene* CWheelLayer::createScene()
{
    auto s = Scene::create();
    s->addChild(CWheelLayer::create());
    return s;
}

void CWheelLayer::update(float dt)
{
    if (_state == 1)
    {
        auto ro = _wheel->getRotation();
        ro += _speed;

        if (ro>=360*3)
        {
            ro = 0;
            _state = 2;
            _soundCount1 = 0;
        }

        _wheel -> setRotation(ro);
        if (_speed<=8)
        {
            _speed += _speedAcc;
        }

        if ((int)ro/18 > _soundCount1)
        {
            if (_soundCount2>5)
            {
                _soundCount2 = 0;
                _soundCount1 = (int)ro/18;
                CSoundTools::playEffect(TURN_TABLE);
            }
        }
        _soundCount2++;
    }
    else if (_state == 2)
    {
        auto ro = _wheel->getRotation();
        auto spp = (_targetRo - ro)*0.02;
        auto finRo = _targetRo - ro;
        if (spp>=_speed)
        {
            spp = _speed;
        }
        ro += spp;
        _wheel->setRotation(ro);
        if (spp<=0.02)
        {
            spp = finRo;
            _wheel->setRotation(_targetRo-360*2);
            _state = 0;
            CSoundTools::playEffect(TURN_TABLE);
            MessageBox(StringUtils::format("Now index:%d",_index).c_str(), "Congratulation");
        }

        if ((int)ro/18 > _soundCount1)
        {
            if (_soundCount2>5)
            {
                _soundCount2 = 0;
                _soundCount1 = (int)ro/18;
                CSoundTools::playEffect(TURN_TABLE);
            }
        }
        _soundCount2++;
    }
}

下载链接:https://github.com/sky068/WheelAndCoster

原文地址:https://www.cnblogs.com/skyxu123/p/9543813.html