MainJs.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. var WxCommon = require("../extra/WxCommon")
  2. //sdk接入
  3. var Sdk = require('../Sdk/sdk')
  4. var sdk = new Sdk('吃鸡大兵')
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. sceneManager: {
  9. default: null,
  10. type: cc.Node
  11. },
  12. bgNode: {
  13. default: null,
  14. type: cc.Node
  15. },
  16. bg1Node: {
  17. default: null,
  18. type: cc.Node
  19. },
  20. ShareStrategyNode: {//游戏攻略图片节点
  21. default: null,
  22. type: cc.Node
  23. },
  24. planeMoveNode: {
  25. default: null,
  26. type: cc.Node
  27. },
  28. bgMusic: { //背景音乐
  29. default: null,
  30. url: cc.AudioClip
  31. },
  32. score: { //玩家分数
  33. default: null,
  34. type: cc.Label
  35. },
  36. SignAtlas:{
  37. default:null,
  38. type:cc.SpriteAtlas,
  39. },
  40. bgspeed:5,
  41. feiendtex: null,
  42. otherGamesIndex : 0,
  43. timerid : null,
  44. },
  45. // use this for initialization
  46. onLoad: function () {
  47. UserInfo.getCurWeekScoreValue();
  48. this.WxLogin();
  49. this.initShare();
  50. this.listenGame();
  51. var date = new Date();
  52. var y = date.getFullYear();
  53. var m = date.getMonth() + 1;
  54. var w = date.getDate();
  55. var currentTime = y + "-" + m +"-" + w;
  56. var timeStr = UserInfo.getString("curTime");
  57. if ((timeStr == null) || currentTime != timeStr)
  58. {
  59. UserInfo.setString("curTime",currentTime);
  60. UserInfo.setString("coinCount", 5);
  61. }
  62. if (UserInfo.platform == 1) {
  63. wx.setPreferredFramesPerSecond(60);
  64. }
  65. // this.ShareStrategyNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  66. this.onPlaneMoveing();
  67. //SDK接入
  68. sdk.showAdIcon(100,500,this.node);
  69. var launchOptions = wx.getLaunchOptionsSync();
  70. console.log(launchOptions.scene);
  71. sdk.initSdk(launchOptions.scene);
  72. },
  73. start() {
  74. this.feiendtex = new cc.Texture2D();
  75. if (cc.vv == undefined) {
  76. cc.vv = {};
  77. }
  78. // cc.view.setDesignResolutionSize(750, 1334, cc.ResolutionPolicy.SHOW_ALL);
  79. },
  80. WxLogin() {
  81. if (UserInfo.platform != 1) {
  82. return;
  83. }
  84. wx.login({
  85. success: function (res) {
  86. if (res.code) {
  87. var js_code = res.code;
  88. } else {
  89. console.log('登录失败!' + res.errMsg)
  90. }
  91. }
  92. });
  93. },
  94. initShare(data) {
  95. let datavalue = UserInfo.getString('allscore');
  96. let key = UserInfo.getCurWeekScoreValue();
  97. if (UserInfo.IsEmpty(datavalue)) {
  98. UserInfo.setString('allscore', key);
  99. return;
  100. }
  101. let datalist = datavalue.split('*');
  102. let removeIndex = -1;
  103. for (let index = 0; index < datalist.length; ++index) {
  104. if (key == datalist[index]) {
  105. // UserInfo.removeString(datalist[index]);
  106. removeIndex = index;
  107. }
  108. }
  109. if (removeIndex != -1) {
  110. datalist.splice(removeIndex, 1);
  111. }
  112. WxCommon.RemoveUserCloudStorage(datalist, this, this.RemoveScoreKey);
  113. // this.RemoveScoreKey(datalist, true);
  114. },
  115. listenGame() {
  116. if (UserInfo.platform == 1) {
  117. // var date = UserInfo.GetNowFormatDate();
  118. WxCommon.SetEnableDebug(false);
  119. WxCommon.OnShow(null, this, null);
  120. WxCommon.ShowShareMenu();
  121. WxCommon.OnShareAppMessage(null, this, this.shareCallback);
  122. WxCommon.GetSystemInfo(null, this, null);
  123. }
  124. },
  125. onEnable: function () {
  126. this.score.string = "" + UserInfo.getGold();
  127. //this.loadPrisonSP();
  128. this.bgMusicID = cc.audioEngine.playMusic(this.bgMusic, true, 1);
  129. },
  130. onDisable: function () {
  131. cc.audioEngine.uncache(this.bgMusic);
  132. },
  133. onDestroy: function () {
  134. },
  135. onGameStart: function () {
  136. this.sceneManager.getComponent('SceneManager').changeCanvasByName('Game');
  137. // var sdk = new Sdk("元气骑士");
  138. // var canvas = wx.createCanvas();
  139. // sdk.openQrcode();
  140. // console.log("元气骑士");
  141. //
  142. // var data = wx.getLaunchOptionsSync();
  143. // sdk.initSdk(data.scene);
  144. // sdk.showAdIcon(23,4,canvas);
  145. },
  146. onPlaneMoveing: function()
  147. {
  148. var planeNode = this.planeMoveNode.getChildByName("plane");
  149. planeNode.runAction(cc.repeatForever(cc.moveTo(4,cc.p(0,1600))));
  150. },
  151. onGoShop: function () {
  152. this.sceneManager.getComponent('SceneManager').changeCanvasByName('Shop');
  153. },
  154. onShowStrategy: function () {
  155. this.ShareStrategyNode.active = true;
  156. this.createTimer();
  157. },
  158. // updateOtherGames: function(self) {
  159. // self.otherGamesIndex = (self.otherGamesIndex + 1 + WxCommon.otherGameCount) % WxCommon.otherGameCount;
  160. // self.createTimer();
  161. // },
  162. createTimer: function() {
  163. var self = this;
  164. var updateOtherGames = function() {
  165. self.otherGamesIndex = (self.otherGamesIndex + 1 + WxCommon.otherGameCount) % WxCommon.otherGameCount;
  166. self.ShareStrategyNode.getChildByName("othergames").getComponent(cc.Sprite).spriteFrame = self.SignAtlas.getSpriteFrame('games' + self.otherGamesIndex);
  167. self.createTimer();
  168. };
  169. if (this.ShareStrategyNode.active == true) {
  170. this.timerid = setTimeout(updateOtherGames,2000);
  171. }
  172. },
  173. killTimer: function() {
  174. if (this.timerid != null) {
  175. clearTimeout(this.timerid);
  176. this.timerid = null;
  177. }
  178. },
  179. onTouchEnd: function (event) {
  180. this.hideNode();
  181. },
  182. hideNode: function () {
  183. if (this.ShareStrategyNode.active) {
  184. this.killTimer();
  185. this.ShareStrategyNode.active = false;
  186. }
  187. },
  188. SaveImg: function () {
  189. if (UserInfo.platform == 1) {
  190. var path = UserInfo.ewmurl;
  191. WxCommon.SaveImageToPhotosAlbum(path, this, this.hideNode);
  192. }
  193. },
  194. bgMove:function(speed){
  195. var bgPositionY = this.bgNode.getPosition().y;
  196. var bg1PositionY = this.bg1Node.getPosition().y;
  197. this.bgNode.setPosition(cc.v2(0,bgPositionY - speed));
  198. this.bg1Node.setPosition(cc.v2(0,bg1PositionY - speed));
  199. },
  200. //检查是否要重置位置
  201. checkBgReset:function(){
  202. var bgYMax = this.bgNode.getContentSize().height;
  203. if(this.bgNode.getPosition().y <= -bgYMax)
  204. {
  205. this.bgNode.setPosition(cc.v2(0,bgYMax-7)); //会有缝隙 所以设置成小一点 有拼接
  206. }
  207. if(this.bg1Node.getPosition().y <= -bgYMax)
  208. {
  209. this.bg1Node.setPosition(cc.v2(0,bgYMax-7));
  210. }
  211. },
  212. onButtonRank: function (event, optData) {
  213. if (UserInfo.platform != 1 || !cc.vv.isRank) {
  214. return;
  215. }
  216. wx.postMessage({
  217. method: 'showFriendRank',
  218. data: optData
  219. });
  220. cc.find("Canvas/PHB").getChildByName("BtnPHBClose").active = (optData == "true");
  221. cc.find("Canvas/PHB").getChildByName("bg").active = (optData == "true");
  222. if ("true" == optData) {
  223. this.showRank();
  224. }
  225. if ("false" == optData) {
  226. wx.postMessage({
  227. method: 'hideRank',
  228. data: optData
  229. });
  230. cc.find("Canvas/PHB").active = false;
  231. }
  232. },
  233. showRank() {
  234. if (UserInfo.platform == 1 && cc.vv.isRank) {
  235. let openDataContext = wx.getOpenDataContext();
  236. let sharedCanvas = openDataContext.canvas;
  237. this.feiendtex.initWithElement(sharedCanvas);
  238. this.feiendtex.handleLoadedTexture();
  239. cc.find("Canvas/PHB").getChildByName("feiendrank").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.feiendtex);
  240. //cc.find("Canvas/PHB").getChildByName("feiendrank").setContentSize(720,1280);
  241. var scale = Math.min(750 / this.feiendtex.width, 1334 / this.feiendtex.height);
  242. cc.find("Canvas/PHB").getChildByName("feiendrank").setContentSize(this.feiendtex.width * scale, this.feiendtex.height * scale);
  243. cc.find("Canvas/PHB").active = true;
  244. }
  245. },
  246. // called every frame
  247. update: function (dt) {
  248. if (cc.find("Canvas/PHB").active == true) {
  249. this.showRank();
  250. }
  251. this.bgMove(this.bgspeed);
  252. this.checkBgReset();
  253. var planeNode = this.planeMoveNode.getChildByName("plane");
  254. if(planeNode.getPosition().y==1600)
  255. {
  256. planeNode.setPosition(cc.v2(0,-1600));
  257. }
  258. },
  259. // loadPlaneSP: function () {
  260. // //人物根据玩家记录等级动态加载
  261. // let prisonSprite = this.plane.getComponent(cc.Sprite);
  262. // cc.loader.loadRes('texture/Game/Airplane' + UserInfo.getCurrentPrison(), cc.SpriteFrame, function (err, spriteframe) {
  263. // planeSprite.spriteFrame = spriteframe;
  264. // });
  265. // },
  266. onCloseStrategy: function() {
  267. this.hideNode();
  268. },
  269. onShowOtherGame: function() {
  270. if (UserInfo.platform == 1) {
  271. var path = WxCommon.otherGameMap.get(this.otherGamesIndex);
  272. WxCommon.PreviewImage(path, this, this.hideNode);
  273. }
  274. },
  275. });