几种抽奖方式之轮盘抽奖
来源:互联网 发布:软件系统结构图阶段 编辑:程序博客网 时间:2024/04/28 11:25
最近做项目使用到了抽奖功能,于是把抽奖模块独立出来了,以便重复利用,资源来自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
0 0
- 几种抽奖方式之轮盘抽奖
- 轮盘抽奖
- Android之实现“抽奖大轮盘”
- js轮盘抽奖
- IOS 抽奖轮盘
- 轮盘抽奖代码
- 轮盘抽奖代码
- Cocos Creator轮盘抽奖
- Android抽奖轮盘
- 课堂答问轮盘抽奖系统
- 几种抽奖概率算法
- HTML5 canvas例子-抽奖轮盘-时钟
- 玩转js--炫酷抽奖轮盘
- python模拟轮盘抽奖的游戏
- css制作抽奖轮盘效果/扇形菜单
- 抽奖
- 抽奖
- 抽奖
- java Volatile关键字解析
- 自然排名,更倾向于哪个要素?
- easyui datagrid 编辑器添加事件
- Activity切换动画无效(android:windowIsTranslucent)影响(android:windowAnimationStyle)
- sudo 命令执行的很慢,很久才出现密码输入
- 几种抽奖方式之轮盘抽奖
- eclipse failed to create the java virtual machine 问题
- sqlcipher 命令行给db外部加密的实现
- 编程技巧20法 (上)
- HLSL Semantics (HLSL语义)
- jQuery(基本用法)
- Eclipse中<uses-permission>和<permission>和<uses-feature>区别
- 编程技巧20法 (下)
- 代码签名探析