game.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. let httpUtils = require("HttpUtils");
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. //定义绳子的节点
  6. rope_node:{
  7. default:null,
  8. type:cc.Node
  9. },
  10. //获取牛的节点
  11. cattle_ins:{
  12. default: null,
  13. type: cc.Node
  14. },
  15. rope_imgs:{
  16. default:[],
  17. type:cc.SpriteFrame
  18. },
  19. //定义预制体
  20. cattle_prefab:{
  21. default:null,
  22. type:cc.Prefab
  23. },
  24. //设置倒计时属性
  25. time:0,
  26. },
  27. // LIFE-CYCLE CALLBACKS:
  28. onLoad () {
  29. //定义是否捕捉成功
  30. this.success = false;
  31. //初始化分数
  32. this.scoreNum = 0;
  33. },
  34. start () {
  35. //获取组件
  36. let countDownLabel = cc.find("Canvas/bg_sprite/count_down").getComponent(cc.Label);
  37. //初始化时间
  38. //let time = 60;
  39. //活动赋值
  40. countDownLabel.string = this.time + "s";
  41. //设置倒计时,第一个参数是函数,第二个参数表示间隔时间,单位:秒
  42. this.schedule(function (){
  43. this.time--;
  44. //给倒计时节点赋值
  45. countDownLabel.string = this.time + "s"
  46. //判断时间是否为0
  47. if(this.time == 0){
  48. // cc.log("游戏结束!");
  49. //获取弹窗节点
  50. let resultNode = cc.find("Canvas/result");
  51. //获取title和content节点
  52. let titleNode = resultNode.getChildByName("title");
  53. let contentNode = resultNode.getChildByName("content");
  54. //展示分数
  55. titleNode.getComponent(cc.Label).string = "最终得分: " + this.scoreNum;
  56. //判断成就
  57. //获取组件
  58. let contentLabel = contentNode.getComponent(cc.Label)
  59. //根据分数判断成就
  60. switch (true) {
  61. case this.scoreNum <= 3:
  62. contentLabel.string = "套牛青铜";
  63. break;
  64. case this.scoreNum <= 6:
  65. contentLabel.string = "套牛高手";
  66. break;
  67. case this.scoreNum > 6:
  68. contentLabel.string = "套牛王者";
  69. break;
  70. }
  71. //显示弹窗
  72. resultNode.active = true;
  73. //获取当前得分
  74. // let score = this.scoreNum;
  75. // wx.login({
  76. // success(res) {
  77. // if (res.code) {
  78. // // 发起网络请求给游戏后台
  79. // //改造前
  80. // // wx.request({
  81. // // url:"http://localhost:8080/updateScore",
  82. // // method: "POST",
  83. // // header:{
  84. // // 'content-type':'application/x-www-form-urlencoded'
  85. // // },
  86. // // data:{
  87. // // code:res.code,
  88. // // score:score
  89. // // }
  90. // // })
  91. //
  92. // //改造后
  93. // httpUtils.request({
  94. // url:"http://localhost:8080/updateScore",
  95. // method: "POST",
  96. // data:{
  97. // code:res.code,
  98. // score:score
  99. // }
  100. // });
  101. // } else {
  102. // console.log("保存分数失败"+ res.errMsg);
  103. // }
  104. // }
  105. // });
  106. //游戏暂停
  107. cc.director.pause();
  108. }
  109. },1);
  110. //获取微信的SystemInfo信息(包括一些硬件信息,比如手机屏幕的宽和高)
  111. // let sysInfo = wx.getSystemInfoSync();
  112. // //获取微信界面的大小(通常是手机屏幕的宽和高)
  113. // let screenWidth = sysInfo.screenWidth;
  114. // let screenHeight = sysInfo.screenHeight;
  115. //创建一个用户授权按钮
  116. // const wxLoginBtn = wx.createUserInfoButton({
  117. // type:"text",
  118. // text:"", //按钮上要显示的文字
  119. // //按钮样式
  120. // style:{
  121. // left: 0,
  122. // top: 0,
  123. // width: screenWidth,
  124. // height: screenHeight,
  125. // lineHeight: 40,
  126. // backgroundColor: '#00000000', //前面6个代码演示,后面两个0表示全透明
  127. // color: '#ffffff',
  128. // // textAlign: 'center',
  129. // // fontSize: 16,
  130. // // borderRadius: 4
  131. // }
  132. // });
  133. // let self = this;
  134. //监听
  135. // wxLoginBtn.onTap((res) => {
  136. // console.log(res.userInfo);
  137. // //拿到微信用户信息
  138. // let userInfo = res.userInfo;
  139. // self.wxLogin(userInfo);
  140. // //销毁按钮
  141. // wxLoginBtn.destroy();
  142. // });
  143. //已经授权过,获取用户信息
  144. // wx.getUserInfo({
  145. // success(res) {
  146. // // 此处可以获取到用户信息
  147. // let userInfo = res.userInfo;
  148. // console.log(res.userInfo);
  149. // self.wxLogin(userInfo);
  150. // },
  151. // fail(err) {
  152. // console.log("接口调用失败!");
  153. // }
  154. // })
  155. //微信广告位
  156. // let bannerAd = wx.createBannerAd({
  157. // adUnitId:"adunit-7d733ad66a23b87b",
  158. // style:{
  159. // left:27.5, //可以通过计算获取,比如iPhone6宽度是375:(375-320)/2
  160. // top:80, //一般在80~100,过低会遮挡顶部显示的头像等
  161. // width:320 //广告位的宽度最低不能低于320
  162. // }
  163. // });
  164. // //监听是否拉取广告位成功
  165. // bannerAd.onError(err => {
  166. // console.log(err);
  167. // });
  168. // //显示广告位
  169. // bannerAd.show();
  170. },
  171. //定义获取用户信息的方法
  172. // wxLogin(userInfo){
  173. //
  174. // let icon = cc.find("Canvas/bg_sprite/icon").getComponent(cc.Sprite);
  175. // //加载远程网络图片
  176. // cc.loader.load({url:userInfo.avatarUrl,type:"png"},function (err,texture){
  177. // icon.spriteFrame = new cc.SpriteFrame(texture);
  178. // });
  179. //
  180. // wx.login({
  181. // success(res) {
  182. // if (res.code){
  183. // //发起网络请求给游戏后台
  184. // //改造前
  185. // // wx.request({
  186. // // url:"http://localhost:8080/login",
  187. // // method:"POST",
  188. // // //在微信中,如果用POST请求,默认是用body携带参数,在后端接收参数
  189. // // // 需要用Requestbody,如果不想在后端添加Requestbody,就需要在前端
  190. // // // 添加header,指定content-type
  191. // // header:{
  192. // // 'content-type':'application/x-www-form-urlencoded'
  193. // // },
  194. // // data:{
  195. // // code:res.code,
  196. // // nickName:userInfo.nickName,
  197. // // avatarUrl:userInfo.avatarUrl
  198. // // }
  199. // // })
  200. //
  201. // //改造后
  202. // httpUtils.request({
  203. // url:"http://localhost:8080/login",
  204. // method:"POST",
  205. // data:{
  206. // code:res.code,
  207. // nickName:userInfo.nickName,
  208. // avatarUrl:userInfo.avatarUrl
  209. // },
  210. // success(msg){
  211. // console.log(msg);
  212. // }
  213. // })
  214. //
  215. // }else {
  216. // console.log("登录失败!" + res.errMsg)
  217. // }
  218. // }
  219. // })
  220. // },
  221. // update (dt) {},
  222. //捕获按钮点击事件
  223. clickCapture (event,customEventData){
  224. //显示绳子
  225. this.rope_node.active = true;
  226. //设置绳子在当前父节点的顺序
  227. this.rope_node.setSiblingIndex(100);
  228. //设置绳子起始位置
  229. this.rope_node.y = -480;
  230. //向上移动
  231. let up = cc.moveTo(0.4,this.rope_node.x,68);
  232. //this.rope_node.runAction(up);
  233. //判定结果
  234. let result = cc.callFunc(function (){
  235. //获取当前牛儿的x点
  236. let currentX = this.cattle_ins.x;
  237. if(currentX > -50 && currentX < 50){
  238. cc.log("捕捉成功!");
  239. //获取背景
  240. let bgNode = this.node.getChildByName("bg_sprite");
  241. //移除被捕获的牛
  242. bgNode.removeChild(this.cattle_ins)
  243. //更换绳子
  244. //获取绳子的类型,其中getComponent的参数cattle是脚本cattle的名称
  245. //绳子类型就是牛的类型+1
  246. let ropeType = this.cattle_ins.getComponent("cattle").randomType + 1;
  247. //更换绳子的Sprite Frame(也就是通过切换Sprite Frame来实现)
  248. this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[ropeType];
  249. //生成新的牛节点
  250. this.cattle_ins = cc.instantiate(this.cattle_prefab);
  251. //设置y轴
  252. this.cattle_ins.y = 0;
  253. //将新的牛节点添加到背景下
  254. bgNode.addChild(this.cattle_ins);
  255. //捕捉成功后,将this.uccess置为true
  256. this.success = true;
  257. //捕捉成功后,分数+1
  258. this.scoreNum++;
  259. }else{
  260. cc.log("捕捉失败!");
  261. }
  262. },this);
  263. //往回拉
  264. let down = cc.moveTo(0.4,this.rope_node.x,-600);
  265. let finish = cc.callFunc(function (){
  266. //重置绳子
  267. this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[0];
  268. //判断是否捕捉成功
  269. if (this.success == true){
  270. //获取score的Label节点
  271. let scoreLabel = cc.find("Canvas/bg_sprite/score").getComponent(cc.Label);
  272. scoreLabel.string = "Score: " + this.scoreNum;
  273. //完成分数显示后,将this.success重新置为false
  274. this.success = false;
  275. }
  276. },this)
  277. //定义一个序列动画
  278. let sequence = cc.sequence(up,result,down,finish);
  279. this.rope_node.runAction(sequence);
  280. },
  281. //关闭按钮,继续游戏
  282. closeBtn(){
  283. cc.log("继续游戏");
  284. //恢复游戏
  285. cc.director.resume();
  286. //重新加载场景
  287. cc.director.loadScene("game");
  288. },
  289. //微信分享
  290. shareBtn(){
  291. wx.shareAppMessage({
  292. title:"大家都来玩套牛小游戏",
  293. imageUrl:"http://img.zhubohome.com.cn/game_share.png",//必须是远程地址,不能是本地图片
  294. success(res){
  295. console.log(res);
  296. },
  297. fail(err){
  298. console.log(err);
  299. }
  300. })
  301. }
  302. });