几种抽奖方式之轮盘抽奖
2015-06-30 14:20
295 查看
最近做项目使用到了抽奖功能,于是把抽奖模块独立出来了,以便重复利用,资源来自https://www.geek-share.com/detail/2601100880.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++; } }
相关文章推荐
- 几种抽奖方式之轮盘抽奖
- Activity切换动画无效(android:windowIsTranslucent)影响(android:windowAnimationStyle)
- 编程技巧20法 (上)
- 软件启动报错,该内存不能为read
- mybatis 动态SQL与模糊查询
- eclipse failed to create the java virtual machine 问题
- Laravel5.1学习笔记8 Blade模板
- WCF事务应用[转]
- org.tinygroup.vfs-虚拟文件系统
- 移动5年 Android生态系统的演进
- WPF的ProgressBar进度条
- 异或运算与常用方法示例
- Swift学习笔记之基础篇(一)
- easyui datagrid 编辑器添加事件
- 自然排名,更倾向于哪个要素?
- sudo 命令执行的很慢,很久才出现密码输入
- java Volatile关键字解析
- hdu 1059 Dividing 多重背包
- org.tinygroup.templateengine-模板引擎
- 2015 年五大移动端设计趋势