123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- let httpUtils = require("HttpUtils");
- cc.Class({
- extends: cc.Component,
- properties: {
- //定义绳子的节点
- rope_node:{
- default:null,
- type:cc.Node
- },
- //获取牛的节点
- cattle_ins:{
- default: null,
- type: cc.Node
- },
- rope_imgs:{
- default:[],
- type:cc.SpriteFrame
- },
- //定义预制体
- cattle_prefab:{
- default:null,
- type:cc.Prefab
- },
- //设置倒计时属性
- time:0,
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad () {
- //定义是否捕捉成功
- this.success = false;
- //初始化分数
- this.scoreNum = 0;
- },
- start () {
- //获取组件
- let countDownLabel = cc.find("Canvas/bg_sprite/count_down").getComponent(cc.Label);
- //初始化时间
- //let time = 60;
- //活动赋值
- countDownLabel.string = this.time + "s";
- //设置倒计时,第一个参数是函数,第二个参数表示间隔时间,单位:秒
- this.schedule(function (){
- this.time--;
- //给倒计时节点赋值
- countDownLabel.string = this.time + "s"
- //判断时间是否为0
- if(this.time == 0){
- // cc.log("游戏结束!");
- //获取弹窗节点
- let resultNode = cc.find("Canvas/result");
- //获取title和content节点
- let titleNode = resultNode.getChildByName("title");
- let contentNode = resultNode.getChildByName("content");
- //展示分数
- titleNode.getComponent(cc.Label).string = "最终得分: " + this.scoreNum;
- //判断成就
- //获取组件
- let contentLabel = contentNode.getComponent(cc.Label)
- //根据分数判断成就
- switch (true) {
- case this.scoreNum <= 3:
- contentLabel.string = "套牛青铜";
- break;
- case this.scoreNum <= 6:
- contentLabel.string = "套牛高手";
- break;
- case this.scoreNum > 6:
- contentLabel.string = "套牛王者";
- break;
- }
- //显示弹窗
- resultNode.active = true;
- //获取当前得分
- // let score = this.scoreNum;
- // wx.login({
- // success(res) {
- // if (res.code) {
- // // 发起网络请求给游戏后台
- // //改造前
- // // wx.request({
- // // url:"http://localhost:8080/updateScore",
- // // method: "POST",
- // // header:{
- // // 'content-type':'application/x-www-form-urlencoded'
- // // },
- // // data:{
- // // code:res.code,
- // // score:score
- // // }
- // // })
- //
- // //改造后
- // httpUtils.request({
- // url:"http://localhost:8080/updateScore",
- // method: "POST",
- // data:{
- // code:res.code,
- // score:score
- // }
- // });
- // } else {
- // console.log("保存分数失败"+ res.errMsg);
- // }
- // }
- // });
- //游戏暂停
- cc.director.pause();
- }
- },1);
- //获取微信的SystemInfo信息(包括一些硬件信息,比如手机屏幕的宽和高)
- // let sysInfo = wx.getSystemInfoSync();
- // //获取微信界面的大小(通常是手机屏幕的宽和高)
- // let screenWidth = sysInfo.screenWidth;
- // let screenHeight = sysInfo.screenHeight;
- //创建一个用户授权按钮
- // const wxLoginBtn = wx.createUserInfoButton({
- // type:"text",
- // text:"", //按钮上要显示的文字
- // //按钮样式
- // style:{
- // left: 0,
- // top: 0,
- // width: screenWidth,
- // height: screenHeight,
- // lineHeight: 40,
- // backgroundColor: '#00000000', //前面6个代码演示,后面两个0表示全透明
- // color: '#ffffff',
- // // textAlign: 'center',
- // // fontSize: 16,
- // // borderRadius: 4
- // }
- // });
- // let self = this;
- //监听
- // wxLoginBtn.onTap((res) => {
- // console.log(res.userInfo);
- // //拿到微信用户信息
- // let userInfo = res.userInfo;
- // self.wxLogin(userInfo);
- // //销毁按钮
- // wxLoginBtn.destroy();
- // });
- //已经授权过,获取用户信息
- // wx.getUserInfo({
- // success(res) {
- // // 此处可以获取到用户信息
- // let userInfo = res.userInfo;
- // console.log(res.userInfo);
- // self.wxLogin(userInfo);
- // },
- // fail(err) {
- // console.log("接口调用失败!");
- // }
- // })
- //微信广告位
- // let bannerAd = wx.createBannerAd({
- // adUnitId:"adunit-7d733ad66a23b87b",
- // style:{
- // left:27.5, //可以通过计算获取,比如iPhone6宽度是375:(375-320)/2
- // top:80, //一般在80~100,过低会遮挡顶部显示的头像等
- // width:320 //广告位的宽度最低不能低于320
- // }
- // });
- // //监听是否拉取广告位成功
- // bannerAd.onError(err => {
- // console.log(err);
- // });
- // //显示广告位
- // bannerAd.show();
- },
- //定义获取用户信息的方法
- // wxLogin(userInfo){
- //
- // let icon = cc.find("Canvas/bg_sprite/icon").getComponent(cc.Sprite);
- // //加载远程网络图片
- // cc.loader.load({url:userInfo.avatarUrl,type:"png"},function (err,texture){
- // icon.spriteFrame = new cc.SpriteFrame(texture);
- // });
- //
- // wx.login({
- // success(res) {
- // if (res.code){
- // //发起网络请求给游戏后台
- // //改造前
- // // wx.request({
- // // url:"http://localhost:8080/login",
- // // method:"POST",
- // // //在微信中,如果用POST请求,默认是用body携带参数,在后端接收参数
- // // // 需要用Requestbody,如果不想在后端添加Requestbody,就需要在前端
- // // // 添加header,指定content-type
- // // header:{
- // // 'content-type':'application/x-www-form-urlencoded'
- // // },
- // // data:{
- // // code:res.code,
- // // nickName:userInfo.nickName,
- // // avatarUrl:userInfo.avatarUrl
- // // }
- // // })
- //
- // //改造后
- // httpUtils.request({
- // url:"http://localhost:8080/login",
- // method:"POST",
- // data:{
- // code:res.code,
- // nickName:userInfo.nickName,
- // avatarUrl:userInfo.avatarUrl
- // },
- // success(msg){
- // console.log(msg);
- // }
- // })
- //
- // }else {
- // console.log("登录失败!" + res.errMsg)
- // }
- // }
- // })
- // },
- // update (dt) {},
- //捕获按钮点击事件
- clickCapture (event,customEventData){
- //显示绳子
- this.rope_node.active = true;
- //设置绳子在当前父节点的顺序
- this.rope_node.setSiblingIndex(100);
- //设置绳子起始位置
- this.rope_node.y = -480;
- //向上移动
- let up = cc.moveTo(0.4,this.rope_node.x,68);
- //this.rope_node.runAction(up);
- //判定结果
- let result = cc.callFunc(function (){
- //获取当前牛儿的x点
- let currentX = this.cattle_ins.x;
- if(currentX > -50 && currentX < 50){
- cc.log("捕捉成功!");
- //获取背景
- let bgNode = this.node.getChildByName("bg_sprite");
- //移除被捕获的牛
- bgNode.removeChild(this.cattle_ins)
- //更换绳子
- //获取绳子的类型,其中getComponent的参数cattle是脚本cattle的名称
- //绳子类型就是牛的类型+1
- let ropeType = this.cattle_ins.getComponent("cattle").randomType + 1;
- //更换绳子的Sprite Frame(也就是通过切换Sprite Frame来实现)
- this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[ropeType];
- //生成新的牛节点
- this.cattle_ins = cc.instantiate(this.cattle_prefab);
- //设置y轴
- this.cattle_ins.y = 0;
- //将新的牛节点添加到背景下
- bgNode.addChild(this.cattle_ins);
- //捕捉成功后,将this.uccess置为true
- this.success = true;
- //捕捉成功后,分数+1
- this.scoreNum++;
- }else{
- cc.log("捕捉失败!");
- }
- },this);
- //往回拉
- let down = cc.moveTo(0.4,this.rope_node.x,-600);
- let finish = cc.callFunc(function (){
- //重置绳子
- this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[0];
- //判断是否捕捉成功
- if (this.success == true){
- //获取score的Label节点
- let scoreLabel = cc.find("Canvas/bg_sprite/score").getComponent(cc.Label);
- scoreLabel.string = "Score: " + this.scoreNum;
- //完成分数显示后,将this.success重新置为false
- this.success = false;
- }
- },this)
- //定义一个序列动画
- let sequence = cc.sequence(up,result,down,finish);
- this.rope_node.runAction(sequence);
- },
- //关闭按钮,继续游戏
- closeBtn(){
- cc.log("继续游戏");
- //恢复游戏
- cc.director.resume();
- //重新加载场景
- cc.director.loadScene("game");
- },
- //微信分享
- shareBtn(){
- wx.shareAppMessage({
- title:"大家都来玩套牛小游戏",
- imageUrl:"http://img.zhubohome.com.cn/game_share.png",//必须是远程地址,不能是本地图片
- success(res){
- console.log(res);
- },
- fail(err){
- console.log(err);
- }
- })
- }
- });
|