zoukankan      html  css  js  c++  java
  • 几种抽奖方式之轮盘抽奖

    最近做项目使用到了抽奖功能,于是把抽奖模块独立出来了,以便重复利用,资源来自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

  • 相关阅读:
    写代码实现两个 goroutine,其中一个产生随机数并写入到 go channel 中,另外一 个从 channel 中读取数字并打印到标准输出。最终输出五个随机数。
    05| RWMutex:读写锁的实现原理及避坑指南
    go 面试题
    go 局部变量在哪
    12 _ atomic:要保证原子操作,一定要使用这几种方法
    11 _ Context:信息穿透上下文
    什么是线程
    go面试题
    redis连接池 go
    docker 指定版本rpm包安装
  • 原文地址:https://www.cnblogs.com/skyxu123/p/9543813.html
Copyright © 2011-2022 走看看