代码:https://gitee.com/wolfdong7/cocos_lucky_table/tree/master

···
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Label)
result:cc.Label = null;
@property(cc.Node)
running_item:cc.Node = null;
@property(cc.Node)
item_root:cc.Node = null;
start_index = 0; //开始的位置
end_index = 0; //结束的位置
is_running = false; //是否在运行
road_set = null; //路劲的设置
next_step = null;
walk_time = 0;
passed_time = 0;
max_v = 3000; //最快速度
min_v = 120; //最慢速度
v = 0; //当前速度
a_v = 0; //加速度
s_v = 0; //减速度
lucky_result = ["¥5元", "Iphone 8", "¥2元", "¥100元话费", "房卡x10", "¥3元", "Iphone 8",
"房卡x5", "¥100元", "¥10元话费", "¥2元", "¥50元话费"];
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
this.is_running = false;
var pos = this.item_root.children[0].getPosition();
this.running_item.setPosition(pos);
}
btn_run(){
console.log('btn run');
if (this.is_running) {
return;
}
// 生成我们的结果: --> 发到我们的服务器上的 不做抽奖的服务器 随机生成一个结果;
var ret = Math.random() * 12; // [0, 1)
ret = Math.floor(ret);
this.result.string = "剧透:"+this.lucky_result[ret];
this.show_result_anim(ret);
}
show_result_anim(ret){
//每次转动至少3圈
var round = 3;
this.road_set = []; // ---> 最后 停下来了;
for(var i = 0; i < round; i ++) {
for(var j = this.start_index; j < this.item_root.childrenCount; j ++) {
this.road_set.push(this.item_root.children[j].getPosition());
}
for(var j = 0; j < this.start_index; j ++) {
this.road_set.push(this.item_root.children[j].getPosition());
}
}
this.end_index = ret;
//判断结束的位置和起始的位置,好规划好路线
if (this.end_index < this.start_index)
{
for(var j = this.start_index; j < this.item_root.childrenCount; j ++) {
this.road_set.push(this.item_root.children[j].getPosition());
}
for(var j = 0; j <= this.end_index; j ++) {
this.road_set.push(this.item_root.children[j].getPosition());
}
}
else
{
for(var j = this.start_index; j <= this.end_index; j ++) {
this.road_set.push(this.item_root.children[j].getPosition());
}
}
//设置好其实位置
this.running_item.setPosition(this.road_set[0]);
this.next_step = 1;
this.v = this.min_v;
this.a_v = (this.max_v - this.min_v) / this.road_set.length * 3; //每次速度增加(减少)这么多,先加速,后减速
this.s_v = (this.max_v - this.min_v) / this.road_set.length * 3/2; //每次速度增加(减少)这么多,先加速,后减速
console.log("加速度:",this.a_v);
console.log("总共路程:",this.road_set.length);
this.walk_to_next();
}
walk_to_next() {
if (this.next_step >= this.road_set.length) {
this.start_index = this.end_index;
this.is_running = false;
this.v = this.min_v;
return;
}
this.is_running = true;
var src = this.running_item.getPosition();
var dst = this.road_set[this.next_step];
var dir = dst.sub(src);//两个向量相减
var len = dir.mag();//计算其长度
this.walk_time = len / this.v;
this.passed_time = 0;
}
update (dt) {
if (this.is_running === false) {
return;
}
this.passed_time += dt;
if (this.passed_time >= this.walk_time) {
this.running_item.setPosition(this.road_set[this.next_step]);
if(this.next_step <= (this.road_set.length/3))
{
this.v += this.a_v;
}
else if(this.next_step > (this.road_set.length/3))
{
if(this.next_step == (this.road_set.length - 3))
{
this.v = this.min_v + this.s_v*2;
}
else
{
this.v -= this.s_v;
}
if(this.v < this.min_v)
{
this.v = this.min_v;
}
}
this.next_step ++;
console.log(this.v);
this.walk_to_next();
}
}
}
···
网友评论