cocos creator 的大风车

cc.Class({

    extends: cc.Component,

 

    properties: {

        startBtn: {

            default: null,      // The default value will be used only when the component attachin                    // to a node for the first time

            type: cc.Button,     // optional, default is typeof default

            visible: true,      // optional, default is true

            displayName: 'startBtn', // optional

        },

        wheelSp: {

            default: null,

            type: cc.Sprite

        },

        maxSpeed: {

            default: 5,

            type: cc.Float,

            max: 15,

            min: 2,

        },

        duration: {

            default: 3,

            type: cc.Float,

            max: 5,

            min: 1,

            tooltip: "减速前旋转时间"

        },

        acc: {

            default: 0.1,

            type: cc.Float,

            max: 0.2,

            min: 0.01,

            tooltip: "加速度"

        },

        targetID: {

            default: 0,

            type: cc.Integer,

            tooltip: "指定结束时的齿轮"

        },

        springback: {

            default: false,

            tooltip: "旋转结束是否回弹"

        },

        effectAudio: {

            default: null,

            url: cc.AudioClip

        }

    },

 

    // use this for initialization

    onLoad: function () {

        cc.log("....onload");

        this.wheelState = 0;

        this.curSpeed = 0;

        this.spinTime = 0;                   //减速前旋转时间

        this.gearNum = 6;

        this.defaultAngle = 360 / 150 / 2;        //修正默认角度

        this.gearAngle = 360 / this.gearNum;   //每个齿轮的角度

        this.wheelSp.node.rotation = this.defaultAngle;

        this.finalAngle = 0;                 //最终结果指定的角度

        this.effectFlag = 0;                 //用于音效播放

        // if (!cc.sys.isBrowser) {

        //     cc.loader.loadRes('Sound/game_turntable', function (err, res) { if (err) { cc.log('...err:' + err); } });

        // }

        this.startBtn.node.on(cc.Node.EventType.TOUCH_END, function (event) {

            // this.targetID = Math.floor(Math.random() * 6 + 1);

            this.targetID = this.myRandom();

            cc.log("begin spin");

            if (this.wheelState !== 0) {

                return;

            }

            this.decAngle = 2 * 360;  // 减速旋转两圈

            this.wheelState = 1;

            this.curSpeed = 0;

            this.spinTime = 0;

            // var act = cc.rotateTo(10, 360*10);

            // this.wheelSp.node.runAction(act.easing(cc.easeSineInOut()));

        }.bind(this));

    },

 

    start: function () {

        // cc.log('....start');

    },

    getRandom: function (start, end) {

        var length = end - start;

        var num = parseInt(Math.random() * (length) + start);

        return num;

    },

    /**

     * 谢谢参与25%,1张30%,2张30%,3张4%,5张4%  10张4%

    */

    myRandom: function () {

        var rand = Math.random();

        if (rand < 0.3) return 2;//1张30%,2张30%

        if (rand < 0.6) return 3;//1张30%,2张30%

        if (rand < 0.88) return 1;//谢谢参与28%

        if (rand < 0.92) return 4;//谢谢参与4%

        if (rand < 0.96) return 5;//谢谢参与4%

        return 6;//谢谢参与4%

    },

    caculateFinalAngle: function (targetID) {

        this.finalAngle = 360 - this.targetID * this.gearAngle + this.defaultAngle;

        if (this.springback) {

            this.finalAngle += this.gearAngle;

        }

    },

    // called every frame, uncomment this function to activate update callback

    update: function (dt) {

        if (this.wheelState === 0) {

            return;

        }

        // 播放音效有可能卡

        this.effectFlag += this.curSpeed;

        if (!cc.sys.isBrowser && this.effectFlag >= this.gearAngle) {

            // if (this.audioID) {

            //     cc.audioEngine.pauseEffect(this.audioID);

            // }

            // // this.audioID = cc.audioEngine.playEffect(this.effectAudio,false);

            // this.audioID = cc.audioEngine.playEffect(cc.url.raw('resources/sound/game_XXX.mp3'));

            this.effectFlag = 0;

        }

 

        if (this.wheelState == 1) {

            // cc.log('....加速,speed:' + this.curSpeed);

            this.spinTime += dt;

            this.wheelSp.node.rotation = this.wheelSp.node.rotation + this.curSpeed;

            if (this.curSpeed <= this.maxSpeed) {

                this.curSpeed += this.acc;

            }

            else {

                if (this.spinTime < this.duration) {

                    return;

                }

                // cc.log('....开始减速');

                //设置目标角度

                this.finalAngle = 360 - this.targetID * this.gearAngle + this.defaultAngle;

                this.maxSpeed = this.curSpeed;

                if (this.springback) {

                    this.finalAngle += this.gearAngle;

                }

                this.wheelSp.node.rotation = this.finalAngle;

                this.wheelState = 2;

            }

        }

        else if (this.wheelState == 2) {

            // cc.log('......减速');

            var curRo = this.wheelSp.node.rotation; //应该等于finalAngle

            var hadRo = curRo - this.finalAngle;

            this.curSpeed = this.maxSpeed * ((this.decAngle - hadRo) / this.decAngle) + 0.2;

            this.wheelSp.node.rotation = curRo + this.curSpeed;

 

            if ((this.decAngle - hadRo) <= 0) {

                // cc.log('....停止');

                this.wheelState = 0;

                this.wheelSp.node.rotation = this.finalAngle;

                if (this.springback) {

                    //倒转一个齿轮

                    // var act = new cc.rotateBy(0.6, -this.gearAngle);

                    var act = cc.rotateBy(0.6, -this.gearAngle);

                    var seq = cc.sequence(cc.delayTime(0.2), act, cc.callFunc(this.showRes, this));

                    this.wheelSp.node.runAction(seq);

                }

                else {

                    this.showRes();

                }

            }

        }

    },

    showRes: function () {

        // var Config = require("Config");

        // if (cc.sys.isBrowser) {

        //     alert(data, 'You have got ' + this.targetID);

        // }

        //向后台发数据,刷新页面才能重新生成随机数,所以要把按钮禁用

        var data = 0;

        if (this.targetID == 1) {

            var data = 0;

        } else if (this.targetID == 2) {

            var data = 1;

        } else if (this.targetID == 3) {

            var data = 2;

        } else if (this.targetID == 4) {

            var data = 3;

        } else if (this.targetID == 5) {

            var data = 5;

        } else if (this.targetID == 6) {

            var data = 10;

        }

        this.sendWheelData(data);

    },

    sendWheelData: function (data) {

        var onEnter = function (ret) {

            if (ret.errcode !== 0) {

                cc.vv.wc.hide();

                var msg = "获得奖励失败!";

                if (ret.errcode == 1) {

                    msg = "获得奖励失败,小主可以联系群主需求帮助喔!";

                }

                else if (ret.errcode == -1) {

                    msg = "不好意思小主的房卡不足以扣除抽奖所需!";

                }

                cc.vv.shownotice.show(msg);

            }

            else {

                if (data.gems == 0) {

                    var msg = "谢谢参与,小主需要继续加油!";

                    cc.vv.shownotice.show(msg);

                } else {

                    var msg = "恭喜小主获得" + data.gems + "房卡奖励,继续加油!";

                    cc.vv.shownotice.show(msg);

                }

            }

        };

 

        var data = {

            userid: cc.vv.userMgr.userId,

            gems: data,

        };

        cc.vv.http.sendRequest("/send_wheel_data", data, onEnter);

    }

 

});

 

### 回答1: Cocos Creator是一种强大的游戏开发引擎,可以用它来制作各种各样的游戏。合成大西瓜是近期非常受欢迎的一款小游戏,让我们来看一下如何使用Cocos Creator来制作这个游戏。 首先,创建一个新的游戏项目,在项目中添加一个场景。我们需要在场景中创建一个背景,并设置适当的大小和位置。 接下来,我们需要添加合成大西瓜游戏的各个元素。我们可以使用Cocos Creator提供的2D节点系统来实现这一点。添加一个节点作为刀子,并设置初始位置和大小。然后,我们可以添加一些西瓜和其他水果的节点,设置它们的位置和大小,并在刀子碰到时进行相应的反应。 为了实现切水果的效果,我们可以使用Cocos Creator的物理引擎和碰撞检测功能。将刀子和水果节点都设置为碰撞体,并设置适当的形状和大小。当刀子和水果节点发生碰撞时,可以通过编写代码来分离碰撞的节点,增加得分,播放音效等。 游戏正常运行后,我们可以添加一些特效和动画来增加游戏的乐趣和视觉效果。例如,可以在切割时添加粒子效果,或者在西瓜分离时添加爆炸动画。 最后,我们可以添加一些游戏逻辑和界面元素。例如,可以添加计分板来跟踪玩家的得分,还可以添加倒计时的元素来限制游戏的时间。 通过使用Cocos Creator,我们可以轻松地制作合成大西瓜这样的小游戏。只需简单的设置节点、物理引擎和碰撞检测,再添加一些特效和动画,最后完成游戏逻辑和界面设计。希望这些信息对您有所帮助! ### 回答2: Cocos Creator是一款专业的游戏开发引擎,可以帮助开发者快速创建各类游戏。合成大西瓜是一款益智休闲游戏,玩家需要通过不断合成西瓜来获得更大的西瓜。在Cocos Creator中实现合成大西瓜游戏可以分为以下几个步骤: 1. 创建游戏场景:使用Cocos Creator的界面编辑器可以创建游戏主场景,设计好游戏背景、按钮和各种元素。 2. 添加游戏元素:在游戏场景中添加西瓜元素,可以使用Cocos Creator提供的精灵组件将西瓜图片加载到游戏中。可以使用代码控制西瓜的位置、大小和交互行为。 3. 实现合成逻辑:在游戏中,玩家可以通过点击相同大小的西瓜进行合成,合成后的西瓜会变得更大。可以使用Cocos Creator的碰撞检测功能来判断玩家是否点击到了相同大小的西瓜,并执行合成操作。 4. 设计游戏规则:设定游戏的规则和目标,例如规定玩家需要在规定时间内合成尽可能大的西瓜,或者设定一定数量的西瓜后游戏结束等。 5. 添加声音和动画效果:使用Cocos Creator的动画编辑器可以给游戏中的元素添加动画效果,增加游戏的趣味性。同时,可以使用Cocos Creator的音频引擎来播放背景音乐和音效,提升游戏的音效效果。 通过以上步骤,我们可以在Cocos Creator中实现一个简单的合成大西瓜游戏。当然,具体的实现细节还需要根据项目需求和个人创意来决定。Cocos Creator以其强大的功能和易用性,为开发者提供了一个快速实现游戏创意的平台。 ### 回答3: Cocos Creator 是一款开发工具,它能让开发者更加轻松地创建游戏和应用程序。而“合成大西瓜”则是一款非常流行的休闲游戏。 “合成大西瓜”游戏的玩法非常简单,玩家需要通过不断点击屏幕,让小西瓜合成更大的西瓜。初始时,玩家只有一个小西瓜,通过不断合成,可以让西瓜逐渐变大。合成后的西瓜可以卖掉,用来获得更多金币,进一步扩展农田,购买更多的小西瓜,从而加快合成的速度。 在 Cocos Creator 中开发“合成大西瓜”这样的游戏非常方便。Cocos Creator 提供了强大的编辑器和工具,可以帮助开发者快速创建游戏场景、设计游戏元素、编写游戏逻辑等。开发者可以使用 Cocos Creator 内置的脚本语言 TypeScript 或 JavaScript 来编写游戏逻辑,并且可以在实时预览中即时查看游戏效果。 对于“合成大西瓜”这个游戏来说,开发者可以借助 Cocos Creator 的节点系统来管理游戏中的所有元素,通过点击事件来触发合成逻辑。同时,Cocos Creator 还提供了丰富的动画系统,可以帮助开发者实现流畅的合成过程和其他动态效果。 总的来说,Cocos Creator 是一个非常适合开发“合成大西瓜”这类休闲游戏的工具。它简化了游戏开发的流程,提供了丰富的功能和易用的界面,让开发者能够更加专注于游戏的创意和设计,从而创造出更好玩、更好看的游戏作品。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值