Game.ts 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. class Game extends egret.DisplayObjectContainer {
  2. public static Instance: Game = new Game();
  3. public levelConfig:any;
  4. public ballConfig:any;
  5. public balls_pool: Array<Ball>;
  6. public ballContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();
  7. public mapConfig:any;
  8. public mapPosition:any;
  9. public startIndex:number = 0;
  10. public startjian:boolean = false;
  11. //当前移动的第一个球的下标
  12. public index:number = 0;
  13. public canPlay:boolean = false;
  14. public playNumEnterFrame:number = 0;
  15. public levelIndex:number = 0;
  16. public targetX:number = 0;
  17. public targetY:number = 0;
  18. public isAttack:boolean = false;
  19. //是否通关
  20. public pass:boolean = false;
  21. public passFrame:number = 0;
  22. public passIndex:number = 1;
  23. //玩家操作的球
  24. public playerBall:Ball;
  25. public fireBalls:Ball[] = new Array();
  26. public nextPlayerBall:Ball;
  27. public cosValue:number;
  28. public sinValue:number;
  29. public boomMC:egret.MovieClip;
  30. //游戏是否结束
  31. public end:boolean = false;
  32. public timeIndex:number = 1;
  33. //每执行一次定时器球的移动速度
  34. public speed:number = 25;
  35. //回退速度
  36. public backSpeed:number = 25;
  37. //前进定时器
  38. public timer: egret.Timer = new egret.Timer(20);
  39. //插入动画定时器
  40. public insertTimer: egret.Timer = new egret.Timer(20);
  41. //插入动画的移动速度
  42. public insertSpeed:number = 10;
  43. public insertstart:number;
  44. public insertend:number;
  45. public insertCheckIndex:number;
  46. public insertNum:number = 0;
  47. //是否添加插入定时器监听
  48. private insertTimerInit:boolean = false;
  49. //回退定时器
  50. private backTimer: egret.Timer = new egret.Timer(20);
  51. //是否添加定时器监听事件
  52. private backTimerInit:boolean = false;
  53. //是否初始化
  54. public inited:boolean = false;
  55. public backIndex:number;
  56. public backstartIndex:number;
  57. public delNum:number = 0;
  58. public numji:number = -1;
  59. public canFire:boolean;
  60. //准线小点的个数
  61. public dotLength:number = 20;
  62. //是否有准线
  63. public haveDot:boolean = false;
  64. //玩家球的炮台
  65. public ballServe:egret.Bitmap;
  66. public gameBg:egret.Bitmap;
  67. public mapBg:egret.Bitmap;
  68. public lianjiEffect:egret.BitmapText;
  69. public dengji:egret.BitmapText;
  70. //道具
  71. public back:egret.Bitmap;
  72. public boom:egret.Bitmap;
  73. public propbg:egret.Bitmap;
  74. //结束警告
  75. public mapend:egret.Bitmap;
  76. public blast:egret.Bitmap;
  77. //头部背景
  78. public gamehead:egret.Bitmap;
  79. //经验条
  80. public experbg:egret.Bitmap;
  81. public exper:egret.Bitmap;
  82. //分数背景
  83. public fraction:egret.Bitmap;
  84. public score: egret.TextField;
  85. //等级
  86. public grade: egret.TextField;
  87. public gradeScore: egret.TextField;
  88. private power: egret.TextField;
  89. private percent: egret.TextField;
  90. private touchArea: egret.Bitmap;
  91. public dead: boolean = false;
  92. public dotContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();
  93. public throughContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();
  94. public rewardContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();
  95. public shakeTween: egret.Tween;
  96. //游戏定义
  97. public leftBullet = 0;
  98. private fireing: boolean = false;
  99. private constructor() {
  100. super();
  101. this.once(egret.Event.ADDED_TO_STAGE, (event: egret.Event) => {
  102. this.levelConfig = RES.getRes("level_json");
  103. this.ballConfig = RES.getRes("ball_json");
  104. this.balls_pool = new Array<Ball>();
  105. this.throughContainer.width = this.stage.width;
  106. this.throughContainer.height = this.stage.height;
  107. this.throughContainer.x = 0;
  108. this.throughContainer.y = 0;
  109. this.gamehead = new egret.Bitmap();
  110. this.gamehead.texture = RES.getRes("gamehead_jpg");
  111. this.gamehead.x = 0;
  112. this.gamehead.y = 0;
  113. this.gamehead.width = this.stage.width;
  114. this.gamehead.height = 177;
  115. this.addChild(this.gamehead);
  116. this.experbg = new egret.Bitmap();
  117. this.experbg.texture = RES.getRes("experiencebg_png");
  118. this.experbg.x = 0;
  119. this.experbg.y = 162;
  120. this.experbg.width = this.stage.width;
  121. this.experbg.height = 12;
  122. this.addChild(this.experbg);
  123. this.exper = new egret.Bitmap();
  124. this.exper.texture = RES.getRes("experience_png");
  125. this.exper.x = 0;
  126. this.exper.y = 162;
  127. this.exper.width = this.stage.width;
  128. this.exper.height = 12;
  129. this.exper.mask = new egret.Rectangle(0,0,0,0);
  130. this.addChild(this.exper);
  131. let course = new egret.DisplayObjectContainer();
  132. this.addChild(course);
  133. this.ballContainer.width = 600;
  134. this.ballContainer.height = 900;
  135. this.ballContainer.anchorOffsetX = 300;
  136. //this.ballContainer.anchorOffsetY = 450;
  137. this.ballContainer.x = this.stage.width / 2;
  138. this.ballContainer.y = 177;
  139. this.ballContainer.mask = new egret.Rectangle(0,0,600,900);
  140. this.dotContainer.width = 600;
  141. this.dotContainer.height = 900;
  142. this.dotContainer.anchorOffsetX = 300;
  143. //this.dotContainer.anchorOffsetY = 450;
  144. this.dotContainer.x = this.stage.width / 2;
  145. this.dotContainer.y = 177;
  146. this.addChild(this.ballContainer);
  147. this.addChild(this.dotContainer);
  148. let kongmingJson = RES.getRes("boom_json");
  149. let kongmingImg = RES.getRes("boom_png");
  150. let mcFactory = new egret.MovieClipDataFactory(kongmingJson,kongmingImg);
  151. this.boomMC = new egret.MovieClip(mcFactory.generateMovieClipData());
  152. this.boomMC.anchorOffsetX = this.boomMC.width /2 + 20;
  153. this.boomMC.anchorOffsetY = this.boomMC.height / 2 + 20;
  154. this.boomMC.visible = false;
  155. this.propbg = new egret.Bitmap();
  156. this.propbg.texture = RES.getRes("propbg_png");
  157. this.propbg.x = this.stage.width / 2 - 225;
  158. this.propbg.y = this.stage.height - 89;
  159. this.propbg.width = 450;
  160. this.propbg.height = 89;
  161. this.addChild(this.propbg);
  162. this.touchArea = new egret.Bitmap();
  163. this.touchArea.touchEnabled = true;
  164. this.touchArea.width = 600;
  165. this.touchArea.height = 900;
  166. this.touchArea.anchorOffsetX = 0;
  167. this.touchArea.anchorOffsetY = 0;
  168. this.touchArea.x = 0;
  169. this.touchArea.y = 0;
  170. this.ballContainer.addChild(this.touchArea);
  171. let offsetY = 0
  172. if (window.innerWidth == 375 && window.innerHeight == 812) {
  173. offsetY = 40;
  174. }
  175. this.fraction = new egret.Bitmap();
  176. this.fraction.texture = RES.getRes("fraction_png");
  177. this.fraction.x = this.stage.width / 2 - this.fraction.width / 2;
  178. this.fraction.y = 0;
  179. this.addChild(this.fraction);
  180. this.mapend = new egret.Bitmap();
  181. this.mapend.texture = RES.getRes("mapend_png");
  182. this.mapend.anchorOffsetX = this.mapend.width / 2;
  183. this.mapend.anchorOffsetY = this.mapend.height / 2;
  184. this.mapend.x = 300;
  185. this.mapend.y = 450;
  186. this.mapend.visible = false;
  187. this.blast = new egret.Bitmap();
  188. this.blast.texture = RES.getRes("blast_png");
  189. this.blast.anchorOffsetX = this.blast.width / 2;
  190. this.blast.anchorOffsetY = this.blast.height / 2;
  191. this.blast.x = 250;
  192. this.blast.y = 450;
  193. this.blast.visible = false;
  194. this.score = new egret.TextField();
  195. //this.score.font = RES.getRes('wwt_score_fnt');
  196. this.score.text = '0';
  197. this.score.textColor = 0x422904;
  198. this.score.size = 48;
  199. this.score.bold = true;
  200. this.score.anchorOffsetY = this.score.height / 2;
  201. //console.log(this.fraction.height);
  202. this.score.y = 99;
  203. this.score.anchorOffsetX = this.score.width / 2;
  204. this.score.x = this.stage.stageWidth / 2;
  205. this.addChild(this.score);
  206. this.grade = new egret.TextField();
  207. this.grade.text = '1';
  208. this.grade.textColor = 0x422904;
  209. this.grade.size = 30;
  210. this.grade.y = offsetY + 50;
  211. this.grade.x = 20;
  212. //this.addChild(this.grade);
  213. this.gradeScore = new egret.TextField();
  214. this.gradeScore.text = '0/0';
  215. this.gradeScore.textColor = 0x422904;
  216. this.gradeScore.size = 30;
  217. this.gradeScore.y = offsetY + 100;
  218. this.gradeScore.x = 20;
  219. //this.addChild(this.gradeScore);
  220. this.mapBg = new egret.Bitmap();
  221. this.mapBg.touchEnabled = true;
  222. this.mapBg.texture = RES.getRes("map_png");
  223. this.mapBg.width = 600;
  224. this.mapBg.height = 900;
  225. this.mapBg.anchorOffsetX = 300;
  226. this.mapBg.anchorOffsetY = 450;
  227. this.mapBg.x = 300;
  228. this.mapBg.y = 450;
  229. this.ballServe = new egret.Bitmap();
  230. this.ballServe.texture = RES.getRes("serve_png");
  231. this.ballServe.width = 141;
  232. this.ballServe.height = 190;
  233. this.ballServe.anchorOffsetX = 70.5;
  234. this.ballServe.anchorOffsetY = 95;
  235. this.ballServe.x = 300;
  236. this.ballServe.y = 700;
  237. this.boom = new egret.Bitmap();
  238. this.boom.texture = RES.getRes("boomprop_png");
  239. this.boom.width = 70;
  240. this.boom.height = 70;
  241. this.boom.anchorOffsetX = 35;
  242. this.boom.anchorOffsetY = 35;
  243. this.boom.x = this.stage.width / 2 - 85;
  244. this.boom.y = this.stage.height - 70;
  245. this.boom.touchEnabled = true;
  246. this.addChild(this.boom);
  247. this.boom.addEventListener(egret.TouchEvent.TOUCH_TAP,async()=>{
  248. let result: any = await Network.Instance.share(4);
  249. let shareConfig = DataBus.Instance.share_config['4'];
  250. let data = await platform.shareAppMessage(true, shareConfig.title, shareConfig.image, '');
  251. Network.Instance.shareSuccess();
  252. if(data.shareTickets){
  253. let rebackData= await platform.getShareInfo(data.shareTickets[0]);
  254. let result=await Network.Instance.checkGroupShareCount(rebackData.encryptedData,rebackData.iv);
  255. if(result==1){
  256. platform.showToast('领取道具成功',false);
  257. //群分享成功
  258. if(this.playerBall){
  259. if(this.playerBall.x == this.mapConfig.playerX && this.playerBall.y == this.mapConfig.playerY){
  260. this.playerBall.changeBoom();
  261. }else{
  262. this.nextPlayerBall.changeBoom();
  263. }
  264. }
  265. }else{
  266. platform.showToast('换个群试试~',false);
  267. }
  268. }else{
  269. platform.showToast('请分享到群',false);
  270. }
  271. },this);
  272. this.back = new egret.Bitmap();
  273. this.back.texture = RES.getRes("backprop_png");
  274. this.back.width = 70;
  275. this.back.height = 70;
  276. this.back.anchorOffsetX = 35;
  277. this.back.anchorOffsetY = 35;
  278. this.back.touchEnabled = true;
  279. this.back.x = this.stage.width / 2 + 65;
  280. this.back.y = this.stage.height - 70;
  281. this.addChild(this.back);
  282. this.back.addEventListener(egret.TouchEvent.TOUCH_TAP,async()=>{
  283. let result: any = await Network.Instance.share(4);
  284. let shareConfig = DataBus.Instance.share_config['4'];
  285. let data = await platform.shareAppMessage(true, shareConfig.title, shareConfig.image, '');
  286. Network.Instance.shareSuccess();
  287. if(data.shareTickets){
  288. let rebackData= await platform.getShareInfo(data.shareTickets[0]);
  289. let result=await Network.Instance.checkGroupShareCount(rebackData.encryptedData,rebackData.iv);
  290. if(result==1){
  291. //群分享成功
  292. platform.showToast('领取道具成功',false);
  293. this.timer.stop();
  294. this.backIndex = this.balls_pool.length - 2;
  295. this.backstartIndex = this.index;
  296. this.delNum = 10 * (~~(this.playerBall.width / this.backSpeed));
  297. this.startIndex -= 10;
  298. this.numji = 0;
  299. if(!this.backTimerInit){
  300. this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => {
  301. this.backTween();
  302. }, this);
  303. this.backTimerInit = true;
  304. }
  305. this.backTimer.start();
  306. }else{
  307. platform.showToast('换个群试试~',false);
  308. }
  309. }else{
  310. platform.showToast('请分享到群',false);
  311. }
  312. },this);
  313. this.lianjiEffect = new egret.BitmapText();
  314. this.lianjiEffect.text = '';
  315. this.lianjiEffect.x = 100;
  316. this.lianjiEffect.y = 100;
  317. this.lianjiEffect.font = RES.getRes('lianji_fnt');
  318. this.dengji = new egret.BitmapText();
  319. this.dengji.text = '1级';
  320. this.dengji.x = this.stage.width / 2 - 15;
  321. this.dengji.y = 152;
  322. this.dengji.font = RES.getRes('dengji_fnt');
  323. this.addChild(this.dengji);
  324. this.touchArea.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (e: egret.TouchEvent) => {
  325. //if (this.canPlay) {
  326. this.targetX = e.localX;
  327. this.targetY = e.localY;
  328. this.ballServe.rotation = this.getAngle(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY);
  329. this.cos(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY);
  330. this.haveDot = true;
  331. //}
  332. }, this);
  333. this.touchArea.addEventListener(egret.TouchEvent.TOUCH_MOVE, (e: egret.TouchEvent) => {
  334. //if (this.canPlay) {
  335. this.targetX = e.localX;
  336. this.targetY = e.localY;
  337. this.ballServe.rotation = this.getAngle(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY);
  338. this.cos(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY);
  339. //}
  340. }, this);
  341. this.touchArea.addEventListener(egret.TouchEvent.TOUCH_END, (e: egret.TouchEvent) => {
  342. //if(this.canPlay){
  343. this.boomMC.x = 300;
  344. this.boomMC.y = 800;
  345. this.haveDot = false;
  346. this.targetX = e.localX;
  347. this.targetY = e.localY;
  348. if(this.canFire && this.inited){
  349. this.playNumEnterFrame = 0;
  350. this.canFire = false;
  351. this.playerBall = this.nextPlayerBall;
  352. this.cos(this.playerBall.x,this.playerBall.y,this.targetX,this.targetY);
  353. this.createPlayerBall();
  354. this.playerBall.sinValue = this.sinValue;
  355. this.playerBall.cosValue = this.cosValue;
  356. this.fireBalls.push(this.playerBall);
  357. this.isAttack = true;
  358. }
  359. //}
  360. }, this);
  361. this.timer.addEventListener(egret.TimerEvent.TIMER, this.moveStage, this);
  362. this.insertTimer.addEventListener(egret.TimerEvent.TIMER, this.insertTween, this);
  363. this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
  364. /*let blackBg = new egret.Bitmap(RES.getRes('bg_black1_png'));
  365. blackBg.x = 0;
  366. blackBg.y = 0;
  367. blackBg.width = this.stage.stageWidth;
  368. blackBg.height = this.stage.stageHeight;
  369. course.addChild(blackBg);
  370. let hand = new egret.Bitmap(RES.getRes('hand1_png'));
  371. hand.x = this.stage.stageWidth / 2 - 200 + 34;
  372. hand.y = this.stage.stageHeight / 2 - 80;
  373. hand.width = 80;
  374. hand.height = 130;
  375. course.addChild(hand);
  376. var handBg = new egret.Bitmap(RES.getRes('hand_bg_png'));
  377. handBg.x = this.stage.stageWidth / 2 - 200 + 34;
  378. handBg.y = this.stage.stageHeight / 2 - 82;
  379. handBg.width = 334;
  380. handBg.height = 32;
  381. course.addChild(handBg);
  382. course.addChild(Player.Instance);
  383. var textCouse = new egret.BitmapText();
  384. textCouse.text = '边打边躲才能取得高分哦!~';
  385. textCouse.font = RES.getRes('wwt_font_6_fnt');
  386. textCouse.x = (this.stage.stageWidth - textCouse.width) / 2;
  387. textCouse.y = this.stage.stageHeight / 2 - 200 + 10;
  388. course.addChild(textCouse);
  389. var textCouse1 = new egret.BitmapText();
  390. textCouse1.text = '滑动屏幕来射击';
  391. textCouse1.font = RES.getRes('wwt_font_7_fnt');
  392. textCouse1.x = (this.stage.stageWidth - textCouse1.width) / 2;
  393. textCouse1.y = this.stage.stageHeight / 2 - 140 + 6;
  394. course.addChild(textCouse1);
  395. egret.Tween.get(hand, {
  396. loop: true, onChange: () => {
  397. Player.Instance.setX(hand.x);
  398. Player.Instance.update();
  399. }
  400. }).to({ x: handBg.x + handBg.width - hand.width }, 1000).to({ x: handBg.x }, 1000);
  401. blackBg.touchEnabled = true;
  402. blackBg.addEventListener(egret.TouchEvent.TOUCH_TAP, (event: egret.TouchEvent) => {
  403. event.stopImmediatePropagation();
  404. egret.Tween.removeTweens(hand);
  405. this.removeChild(course);
  406. let offsetY = 0
  407. if (window.innerWidth == 375 && window.innerHeight == 812) {
  408. offsetY = 40;
  409. }
  410. let touchArea = new egret.Bitmap();
  411. touchArea.touchEnabled = true;
  412. touchArea.width = this.stage.width;
  413. touchArea.height = this.stage.height;
  414. this.addChild(touchArea);
  415. this.score = new egret.BitmapText();
  416. this.score.font = RES.getRes('wwt_score_fnt');
  417. this.score.text = '0';
  418. this.score.anchorOffsetY = this.score.height / 2;
  419. this.score.y = 116 + this.score.height / 2
  420. this.addChild(this.score);
  421. this.smallBg = new egret.Bitmap(RES.getRes('boll_home_smallbg_png'));
  422. this.smallBg.x = 28;
  423. this.smallBg.y = 49 + offsetY;
  424. this.smallBg.width = 144;
  425. this.smallBg.height = 34;
  426. this.addChild(this.smallBg);
  427. this.powerBg = new egret.Bitmap(RES.getRes('boll_home_energy_png'));
  428. this.powerBg.x = 28;
  429. this.powerBg.y = 44 + offsetY;
  430. this.powerBg.width = 44;
  431. this.powerBg.height = 44;
  432. this.addChild(this.powerBg);
  433. this.powerEffect = new egret.Bitmap(RES.getRes('circular_yellow_png'));
  434. this.powerEffect.x = 50;
  435. this.powerEffect.y = 66 + offsetY;
  436. this.powerEffect.anchorOffsetX = 22;
  437. this.powerEffect.anchorOffsetY = 22;
  438. this.powerEffect.width = 44;
  439. this.powerEffect.height = 44;
  440. // this.powerEffect.visible = false;
  441. this.addChild(this.powerEffect);
  442. this.smallBg1 = new egret.Bitmap(RES.getRes('boll_home_smallbg_png'));
  443. this.smallBg1.x = 190;
  444. this.smallBg1.y = 49 + offsetY;
  445. this.smallBg1.width = 144;
  446. this.smallBg1.height = 34;
  447. this.addChild(this.smallBg1);
  448. this.percentBg = new egret.Bitmap(RES.getRes('boll_home_fair_png'));
  449. this.percentBg.x = 190;
  450. this.percentBg.y = 44 + offsetY;
  451. this.percentBg.width = 44;
  452. this.percentBg.height = 44;
  453. this.addChild(this.percentBg);
  454. this.percentEffect = new egret.Bitmap(RES.getRes('circular_red_png'));
  455. this.percentEffect.x = 212;
  456. this.percentEffect.y = 66 + offsetY;
  457. this.percentEffect.anchorOffsetX = 22;
  458. this.percentEffect.anchorOffsetY = 22;
  459. this.percentEffect.width = 44;
  460. this.percentEffect.height = 44;
  461. // this.percentEffect.visible = false;
  462. this.addChild(this.percentEffect);
  463. this.power = new egret.TextField();
  464. this.power.size = 24;
  465. this.power.x = 80;
  466. this.power.y = 56 + offsetY;
  467. this.power.textColor = 0x191F48;
  468. this.power.fontFamily = "Arial";
  469. this.addChild(this.power);
  470. this.percent = new egret.TextField();
  471. this.percent.size = 24;
  472. this.percent.x = 242;
  473. this.percent.y = 56 + offsetY;
  474. this.percent.textColor = 0x191F48;
  475. this.percent.fontFamily = "Arial";
  476. this.addChild(this.percent);
  477. this.addChild(Player.Instance);
  478. this.addChild(this.rewardContainer);
  479. this.addChild(this.enemyContainer);
  480. this.addChild(this.bulletContainer);
  481. this.addChild(this.enemyContainer);
  482. touchArea.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (e: egret.TouchEvent) => {
  483. if (!this.dead) {
  484. this.fireing = true;
  485. Player.Instance.setX(e.stageX);
  486. Player.Instance.setFire(this.fireing);
  487. }
  488. }, this);
  489. touchArea.addEventListener(egret.TouchEvent.TOUCH_MOVE, (e: egret.TouchEvent) => {
  490. if (!this.dead) {
  491. this.fireing = true;
  492. Player.Instance.setFire(this.fireing);
  493. Player.Instance.setX(e.stageX);
  494. }
  495. }, this);
  496. touchArea.addEventListener(egret.TouchEvent.TOUCH_END, () => {
  497. this.fireing = false;
  498. Player.Instance.setFire(this.fireing);
  499. }, this);
  500. this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
  501. DataBus.Instance.course += 1;
  502. }, this);*/
  503. }, this);
  504. }
  505. public getAngle(px,py,mx,my){//获得人物中心和鼠标坐标连线,与y轴正半轴之间的夹角,设置炮台方向
  506. var x = Math.abs(px-mx);
  507. var y = Math.abs(py-my);
  508. var z = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
  509. var cos = y/z;
  510. var radina = Math.acos(cos);//用反三角函数求弧度
  511. var angle = Math.floor(180/(Math.PI/radina));//将弧度转换成角度
  512. if(mx>px&&my>py){//鼠标在第四象限
  513. angle = 180 - angle;
  514. }
  515. if(mx==px&&my>py){//鼠标在y轴负方向上
  516. angle = 180;
  517. }
  518. if(mx>px&&my==py){//鼠标在x轴正方向上
  519. angle = 90;
  520. }
  521. if(mx<px&&my>py){//鼠标在第三象限
  522. angle = 180+angle;
  523. }
  524. if(mx<px&&my==py){//鼠标在x轴负方向
  525. angle = 270;
  526. }
  527. if(mx<px&&my<py){//鼠标在第二象限
  528. angle = 360 - angle;
  529. }
  530. return angle;
  531. }
  532. public restart(reborn:number = 0,level:number = 1) {
  533. //每次随机一个背景
  534. BackGround.Instance.resetBg();
  535. this.ballContainer.removeChildren();
  536. this.balls_pool.length = 0;
  537. DataBus.Instance.reset(reborn);
  538. //this.grade.text ='等级' + DataBus.Instance.grade + '';
  539. this.dengji.text = DataBus.Instance.grade + '级';
  540. this.mapConfig = RES.getRes(this.levelConfig[this.levelIndex].map);
  541. this.mapBg.texture = RES.getRes(this.mapConfig.bgUrl);
  542. this.ballContainer.addChild(this.mapBg);
  543. this.ballContainer.addChild(this.mapend);
  544. this.ballContainer.addChild(this.blast);
  545. this.ballContainer.addChild(this.ballServe);
  546. this.ballContainer.addChild(this.touchArea);
  547. this.ballContainer.addChild(this.boomMC);
  548. this.ballContainer.addChild(this.lianjiEffect);
  549. this.fireBalls.length = 0;
  550. this.ballServe.rotation = 0;
  551. this.ballServe.x = this.mapConfig.playerX;
  552. this.ballServe.y = this.mapConfig.playerY;
  553. this.haveDot = false;
  554. for(let i = 0;i < this.levelConfig[this.levelIndex].startLength; i++){
  555. if(i == 0){
  556. this.createBall(0);
  557. }else if(i == this.levelConfig[this.levelIndex].startLength - 1){
  558. this.createBall(0);
  559. }
  560. else
  561. this.createBall(1);
  562. }
  563. this.playerBall = null;
  564. this.createPlayerBall();
  565. this.dotContainer.removeChildren();
  566. //初始化准线
  567. for(let i = 0;i < this.dotLength; i++){
  568. let dot = new Dot();
  569. dot.x = this.playerBall.x;
  570. dot.y = this.playerBall.y;
  571. dot.visible = false;
  572. this.dotContainer.addChild(dot);
  573. }
  574. this.startTween();
  575. this.dead = false;
  576. Main.Instance.addChild(this);
  577. this.updateScore(0);
  578. }
  579. //刷新游戏道具
  580. public freshProp(){
  581. this.back.x = this.mapConfig.backX;
  582. this.back.y = this.mapConfig.backY;
  583. this.boom.x = this.mapConfig.boomX;
  584. this.boom.y = this.mapConfig.boomY;
  585. this.ballContainer.addChild(this.back);
  586. this.ballContainer.addChild(this.boom);
  587. }
  588. //开始时创建球
  589. public createBall(id:number = 1){
  590. if(this.end){
  591. return;
  592. }
  593. let ball:Ball = new Ball();
  594. //ball.type = id;
  595. //ball.texture = RES.getRes(Game.Instance.ballConfig[id].url);
  596. if(this.balls_pool.length == 0){
  597. ball.createType(this.levelConfig[this.levelIndex].typeNum[DataBus.Instance.nanduIndex],-1);
  598. }else{
  599. ball.createType(this.levelConfig[this.levelIndex].typeNum[DataBus.Instance.nanduIndex],this.balls_pool[this.balls_pool.length - 1].type);
  600. }
  601. ball.x = this.mapConfig.startX;
  602. ball.y = this.mapConfig.startY;
  603. ball.angle = this.mapConfig.startAngle;
  604. this.balls_pool.push(ball);
  605. this.ballContainer.addChild(ball);
  606. }
  607. //创建玩家要发射的球
  608. public createPlayerBall(){
  609. this.nextPlayerBall = new Ball();
  610. this.nextPlayerBall.createType(this.levelConfig[this.levelIndex].typeNum[DataBus.Instance.nanduIndex],-1);
  611. this.nextPlayerBall.x = this.mapConfig.playerX;
  612. this.nextPlayerBall.y = this.mapConfig.playerY;
  613. this.nextPlayerBall.scaleX = 0;
  614. this.nextPlayerBall.scaleY = 0;
  615. this.ballContainer.addChild(this.nextPlayerBall);
  616. egret.Tween.get(this.nextPlayerBall).to({scaleX:1,scaleY:1},100).call(()=>{
  617. this.canFire = true;
  618. });
  619. if(this.playerBall == null){
  620. this.playerBall = this.nextPlayerBall;
  621. }
  622. }
  623. //开局动画
  624. public startTween(){
  625. this.timer.reset();
  626. this.timer.start();
  627. }
  628. public backStartIndex(index:number){
  629. let startindex:number = index;
  630. for(let i = startindex; i > 0 ; i--){
  631. //如果后面运动的球的距离与前面静止的球的距离小于球的宽度,则前面的球也要运动
  632. if(this.checkDifMax(this.balls_pool[i],this.balls_pool[i-1])){
  633. startindex = i - 1;
  634. continue;
  635. }
  636. break;
  637. }
  638. console.log("倒退的startindex:" + startindex);
  639. return startindex;
  640. }
  641. //倒退动画
  642. public backTween(){
  643. console.log("进入backtween");
  644. let balls_pool:Array<Ball> = this.balls_pool;
  645. //如果倒退的不是最后一段,也就是有间隔的地方,进行如下判定
  646. if(this.backIndex!= this.balls_pool.length-2 && this.checkDif(balls_pool[this.backIndex],balls_pool[this.backIndex+1])){
  647. this.backTimer.stop();
  648. GameUtil.Sound.playEffect("ball_wav");
  649. //如果不能消除
  650. if(!this.checkCanMove(this.backIndex)){
  651. for(let i = this.backIndex;i < this.balls_pool.length - 2; i++){
  652. if(this.checkDif(balls_pool[i],balls_pool[i+1])){
  653. this.backIndex = i + 1;
  654. }else{
  655. break;
  656. }
  657. }
  658. this.backTimer.start();
  659. DataBus.Instance.scorePower = 0;
  660. this.numji = 0;
  661. if(!this.startjian && this.backIndex == this.balls_pool.length-2){
  662. this.startIndex -= ~~(this.delNum / ~~(this.playerBall.width / this.backSpeed));
  663. this.startjian = true;
  664. }
  665. }
  666. //this.backSpeed = 5;
  667. console.log("碰到了");
  668. return;
  669. }
  670. if(this.numji >= 0 && this.numji <= ~~(this.playerBall.width / this.backSpeed)){
  671. this.numji++;
  672. this.delNum--;
  673. console.log("delNum:" + this.delNum);
  674. }
  675. //如果回退一个单位球
  676. if(this.numji == ~~(this.playerBall.width / this.backSpeed)){
  677. this.numji = 0;
  678. //this.startIndex--;
  679. }
  680. //倒退结束处理
  681. if(this.delNum == -1){
  682. console.log("timeIndex:" + this.timeIndex);
  683. this.startjian = false;
  684. this.backTimer.stop();
  685. this.timer.start();
  686. this.numji = -1;
  687. this.canPlay = true;
  688. if(DataBus.Instance.scorePower > 1){
  689. DataBus.Instance.lianjiBigTwoNum++;
  690. }
  691. DataBus.Instance.scorePower = 0;
  692. this.checkMin(0,this.startIndex - 1);
  693. return;
  694. }
  695. let endindex = this.backIndex + 1;
  696. for(let i = this.backstartIndex ;i < endindex ; i++){
  697. let ball:Ball = balls_pool[i];
  698. let crux:any;
  699. //如果在直线状态
  700. if(!ball.inCrux){
  701. //如果是起点那一段直线
  702. if(ball.cruxIndex == 0){
  703. let dif = this.diffPoint(ball.x,ball.y,this.mapConfig.startX,this.mapConfig.startY);
  704. ball.x += (this.backSpeed / dif) * (this.mapConfig.startX - ball.x);
  705. ball.y += (this.backSpeed / dif) * (this.mapConfig.startY - ball.y);
  706. //如果回退到开始处了
  707. if(Math.abs(this.mapConfig.crux[0].startpoint.x - ball.x)> Math.abs(this.mapConfig.crux[0].startpoint.x - this.mapConfig.startX)||Math.abs(this.mapConfig.crux[0].startpoint.y - ball.y)> Math.abs(this.mapConfig.crux[0].startpoint.y - this.mapConfig.startY)){
  708. //this.startIndex--;
  709. ball.x = this.mapConfig.startX;
  710. ball.y = this.mapConfig.startY;
  711. }else{
  712. }
  713. }else{
  714. crux = this.mapConfig.crux[ball.cruxIndex - 1];
  715. let dif = this.diffPoint(ball.x,ball.y,crux.endpoint.x,crux.endpoint.y);
  716. ball.x += (this.backSpeed / dif) * (crux.endpoint.x - ball.x);
  717. ball.y += (this.backSpeed / dif) * (crux.endpoint.y - ball.y);
  718. //如果是终点那一段直线
  719. if(ball.cruxIndex == this.mapConfig.crux.length){
  720. if(this.diffPoint(ball.x,ball.y,this.mapConfig.endX,this.mapConfig.endY) >= this.diffPoint(crux.endpoint.x,crux.endpoint.y,this.mapConfig.endX,this.mapConfig.endY)){
  721. ball.x = crux.endpoint.x;
  722. ball.y = crux.endpoint.y;
  723. ball.inCrux = true;
  724. ball.cruxIndex--;
  725. ball.startAngle = ~~ball.angle - crux.angle;
  726. }
  727. }
  728. else{
  729. //如果移动到指定地点,则计算误差
  730. let difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex].startpoint.x,this.mapConfig.crux[ball.cruxIndex].startpoint.y);
  731. let difCon = this.diffPoint(crux.endpoint.x,crux.endpoint.y,this.mapConfig.crux[ball.cruxIndex].startpoint.x,this.mapConfig.crux[ball.cruxIndex].startpoint.y);
  732. if(difBall >= difCon){
  733. ball.difLine = difBall - difCon;
  734. ball.x = crux.endpoint.x;
  735. ball.y = crux.endpoint.y;
  736. ball.inCrux = true;
  737. ball.startAngle = ~~ball.angle - crux.angle;
  738. ball.cruxIndex--;
  739. crux = this.mapConfig.crux[ball.cruxIndex];
  740. //圆心的坐标
  741. let circleX = crux.circle.x;
  742. let circleY = crux.circle.y;
  743. //半径
  744. let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY);
  745. //要移动的弧长,根据弧长和速度计算移动时间
  746. let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2));
  747. let time = perimeter / this.backSpeed;
  748. let difAngle = (ball.startAngle - ball.angle) / time;
  749. let wuAngle = ball.difLine / this.backSpeed * difAngle;
  750. ball.difLine = 0;
  751. ball.angle += wuAngle;
  752. let dify = radius * Math.cos(Math.PI * ball.angle / 180);
  753. let difx = radius * Math.sin(Math.PI * ball.angle / 180);
  754. if(crux.angle < 0){
  755. ball.y = circleY - dify;
  756. ball.x = circleX - difx;
  757. }else{
  758. ball.y = circleY + dify;
  759. ball.x = circleX + difx;
  760. }
  761. }
  762. }
  763. }
  764. }else{
  765. //如果在拐歪状态
  766. crux = this.mapConfig.crux[ball.cruxIndex];
  767. //圆心的坐标
  768. let circleX = crux.circle.x;
  769. let circleY = crux.circle.y;
  770. //半径
  771. let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY);
  772. //要移动的弧长,根据弧长和速度计算移动时间
  773. let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2));
  774. let time = perimeter / this.backSpeed;
  775. let difAngle = (ball.startAngle - ball.angle) / time;
  776. ball.difLine = 0;
  777. //如果刚好到关键点的开始处就后退,处理这个误差
  778. if(ball.startAngle == ball.angle){
  779. ball.difLine = this.backSpeed;
  780. }else{
  781. ball.angle += difAngle;
  782. }
  783. if(crux.angle < 0){
  784. //旋转结束
  785. if((ball.angle - crux.angle) >= (ball.startAngle - crux.angle)){
  786. ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2)) || ball.difLine;
  787. ball.inCrux = false;
  788. ball.angle = ball.startAngle;
  789. ball.targetAngle = ball.angle;
  790. if(ball.angle == 180){
  791. ball.x = crux.startpoint.x + ball.difLine;
  792. ball.y = crux.startpoint.y;
  793. }else if(ball.angle == 360){
  794. ball.x = crux.startpoint.x - ball.difLine;
  795. ball.y = crux.startpoint.y;
  796. }else if(ball.angle == 270){
  797. ball.x = crux.startpoint.x;
  798. ball.y = crux.startpoint.y - ball.difLine;
  799. }else if(ball.angle == 450){
  800. ball.x = crux.startpoint.x;
  801. ball.y = crux.startpoint.y + ball.difLine;
  802. }else{
  803. ball.x = crux.startpoint.x;
  804. ball.y = crux.startpoint.y;
  805. }
  806. continue;
  807. }
  808. }else{
  809. //旋转结束
  810. if((ball.angle - crux.angle) <= (ball.startAngle - crux.angle)){
  811. ball.inCrux = false;
  812. ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2));
  813. ball.angle = ball.startAngle;
  814. ball.targetAngle = ball.angle;
  815. if(ball.angle == 180){
  816. ball.x = crux.startpoint.x + ball.difLine;
  817. ball.y = crux.startpoint.y;
  818. }else if(ball.angle == 360){
  819. ball.x = crux.startpoint.x - ball.difLine;
  820. ball.y = crux.startpoint.y;
  821. }else if(ball.angle == 270){
  822. ball.x = crux.startpoint.x;
  823. ball.y = crux.startpoint.y - ball.difLine;
  824. }else if(ball.angle == 450){
  825. ball.x = crux.startpoint.x;
  826. ball.y = crux.startpoint.y + ball.difLine;
  827. }else{
  828. ball.x = crux.startpoint.x;
  829. ball.y = crux.startpoint.y;
  830. }
  831. continue;
  832. }
  833. }
  834. let dify = radius * Math.cos(Math.PI * ball.angle / 180);
  835. let difx = radius * Math.sin(Math.PI * ball.angle / 180);
  836. if(crux.angle < 0){
  837. ball.y = circleY - dify;
  838. ball.x = circleX - difx;
  839. }else{
  840. ball.y = circleY + dify;
  841. ball.x = circleX + difx;
  842. }
  843. }
  844. }
  845. }
  846. //校验小球之间的距离过大
  847. public check(){
  848. let balls_pool:Array<Ball> = this.balls_pool;
  849. if(this.index > this.startIndex){
  850. this.index = this.startIndex;
  851. console.log("thisindex:" + this.index);
  852. }
  853. //如果两个小球之间的间距过大,则前面的球先停下,由于速度过快,玩家并不会有察觉
  854. for(let i = this.startIndex - 1; i > this.index; i--){
  855. let dif = this.diffPoint(balls_pool[i].x,balls_pool[i].y,balls_pool[i-1].x,balls_pool[i-1].y);
  856. if(dif > this.playerBall.width + 3){
  857. console.log("距离过大:" + i);
  858. this.index = i;
  859. break;
  860. }
  861. }
  862. }
  863. //校验两个小球之间的间距过小
  864. public checkMin(index:number,endindex:number){
  865. let balls_pool:Array<Ball> = this.balls_pool;
  866. //如果两个小球之间的间距小于一个球的宽度,说明两个球重合了,进行处理
  867. for(let i = endindex; i > index; i--){
  868. let dif = this.diffPoint(balls_pool[i].x,balls_pool[i].y,balls_pool[i-1].x,balls_pool[i-1].y);
  869. if( dif < this.playerBall.width - 5){
  870. console.log("距离过小" + i);
  871. for(let j = 0; j < (this.playerBall.width - dif) / this.speed;j++){
  872. this.moveTween(index,i);
  873. }
  874. }
  875. }
  876. }
  877. //移动动画,start为前面运动的球的下标,index为后面的球的下标
  878. public moveTween(start:number,index:number,inPause?:boolean){
  879. if(this.end){
  880. return;
  881. }
  882. let speed = this.speed;
  883. let balls_pool:Array<Ball> = this.balls_pool;
  884. if(!inPause){
  885. for(let i = start; i > 0 ; i--){
  886. //如果后面运动的球的距离与前面静止的球的距离小于球的宽度,则前面的球也要运动
  887. //console.log("start:" + start + " i:" + i);
  888. if(this.checkDif(balls_pool[i],balls_pool[i-1])){
  889. this.index = i-1;
  890. start = i - 1;
  891. continue;
  892. }
  893. break;
  894. }
  895. }
  896. for(let i = start ;i < index ; i++){
  897. let ball:Ball = balls_pool[i];
  898. let crux:any;
  899. //获取配置文件的拐点坐标
  900. if(ball.cruxIndex < this.mapConfig.crux.length){
  901. crux = this.mapConfig.crux[ball.cruxIndex];
  902. }else{
  903. //如果已经走过所有节点,则直接向终点移动
  904. let dif = this.diffPoint(ball.x,ball.y,this.mapConfig.endX,this.mapConfig.endY);
  905. ball.x += (speed / dif) * (this.mapConfig.endX - ball.x);
  906. ball.y += (speed / dif) * (this.mapConfig.endY - ball.y);
  907. let difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  908. let difCon = this.diffPoint(this.mapConfig.endX,this.mapConfig.endY,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  909. if(difBall >= difCon){
  910. this.end = true;
  911. this.canPlay = false;
  912. ball.x = this.mapConfig.endX;
  913. ball.y = this.mapConfig.endY;
  914. }
  915. continue;
  916. }
  917. //如果在直线状态
  918. if(!ball.inCrux ){
  919. let dif = this.diffPoint(ball.x,ball.y,crux.startpoint.x,crux.startpoint.y);
  920. ball.x += (speed / dif) * (crux.startpoint.x - ball.x);
  921. ball.y += (speed / dif) * (crux.startpoint.y - ball.y);
  922. let difBall;
  923. let difCon;
  924. //如果是刚开始那一段直线
  925. if(ball.cruxIndex == 0){
  926. difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.startX,this.mapConfig.startY);
  927. difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.startX,this.mapConfig.startY);
  928. }
  929. else{
  930. //如果移动到指定地点,则将坐标锁定在指定地点
  931. difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  932. difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  933. }
  934. if(difBall >= difCon){
  935. ball.difLine = difBall - difCon;
  936. ball.x = crux.startpoint.x;
  937. ball.y = crux.startpoint.y;
  938. ball.inCrux = true;
  939. ball.startAngle = ball.angle;
  940. ball.targetAngle = ~~ball.angle + crux.angle;
  941. //圆心的坐标
  942. let circleX = crux.circle.x;
  943. let circleY = crux.circle.y;
  944. //半径
  945. let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY);
  946. //要移动的弧长,根据弧长和速度计算移动时间
  947. let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2));
  948. let time = perimeter / speed;
  949. let difAngle = (ball.targetAngle - ball.angle) / time;
  950. let wuAngle = ball.difLine / speed * difAngle;
  951. ball.difLine = 0;
  952. ball.angle += wuAngle;
  953. let dify = radius * Math.cos(Math.PI * ball.angle / 180);
  954. let difx = radius * Math.sin(Math.PI * ball.angle / 180);
  955. if(crux.angle < 0){
  956. ball.y = circleY - dify;
  957. ball.x = circleX - difx;
  958. }else{
  959. ball.y = circleY + dify;
  960. ball.x = circleX + difx;
  961. }
  962. }
  963. }else{
  964. //如果在拐歪状态
  965. let circleX = crux.circle.x;
  966. let circleY = crux.circle.y;
  967. //半径
  968. let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY);
  969. ball.difLine = 0;
  970. //如果刚好到关键点的开始处就后退,处理这个误差
  971. if(ball.targetAngle == ball.angle){
  972. ball.difLine = speed;
  973. }
  974. //要移动的弧长,根据弧长和速度计算移动时间
  975. if(ball.targetAngle != ball.angle){
  976. let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2));
  977. let time = perimeter / speed;
  978. let difAngle = (ball.targetAngle - ball.angle) / time;
  979. ball.angle += difAngle;
  980. }
  981. if(crux.angle < 0){
  982. //旋转结束
  983. if((ball.angle - crux.angle) <= (ball.targetAngle - crux.angle)){
  984. ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine;
  985. ball.inCrux = false;
  986. ball.x = crux.endpoint.x;
  987. ball.y = crux.endpoint.y;
  988. ball.angle = ball.targetAngle;
  989. ball.cruxIndex++;
  990. if(ball.angle == 180){
  991. ball.x = crux.endpoint.x - ball.difLine;
  992. ball.y = crux.endpoint.y;
  993. }else if(ball.angle == 360){
  994. ball.x = crux.endpoint.x + ball.difLine;
  995. ball.y = crux.endpoint.y;
  996. }else if(ball.angle == 270){
  997. ball.x = crux.endpoint.x;
  998. ball.y = crux.endpoint.y + ball.difLine;
  999. }else if(ball.angle == 450){
  1000. ball.x = crux.endpoint.x;
  1001. ball.y = crux.endpoint.y - ball.difLine;
  1002. }else{
  1003. ball.x = crux.endpoint.x;
  1004. ball.y = crux.endpoint.y;
  1005. }
  1006. continue;
  1007. }
  1008. }else{
  1009. //旋转结束
  1010. if((ball.angle - crux.angle) >= (ball.targetAngle - crux.angle)){
  1011. ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine;
  1012. ball.inCrux = false;
  1013. ball.x = crux.endpoint.x;
  1014. ball.y = crux.endpoint.y;
  1015. ball.angle = ball.targetAngle;
  1016. ball.cruxIndex++;
  1017. if(ball.angle == 180){
  1018. ball.x = crux.endpoint.x - ball.difLine;
  1019. ball.y = crux.endpoint.y;
  1020. }else if(ball.angle == 360){
  1021. ball.x = crux.endpoint.x + ball.difLine;
  1022. ball.y = crux.endpoint.y;
  1023. }else if(ball.angle == 270){
  1024. ball.x = crux.endpoint.x;
  1025. ball.y = crux.endpoint.y + ball.difLine;
  1026. }else if(ball.angle == 450){
  1027. ball.x = crux.endpoint.x;
  1028. ball.y = crux.endpoint.y - ball.difLine;
  1029. }else{
  1030. ball.x = crux.endpoint.x;
  1031. ball.y = crux.endpoint.y;
  1032. }
  1033. continue;
  1034. }
  1035. }
  1036. let dify = radius * Math.cos(Math.PI * ball.angle / 180);
  1037. let difx = radius * Math.sin(Math.PI * ball.angle / 180);
  1038. if(crux.angle < 0){
  1039. ball.y = circleY - dify;
  1040. ball.x = circleX - difx;
  1041. }else{
  1042. ball.y = circleY + dify;
  1043. ball.x = circleX + difx;
  1044. }
  1045. }
  1046. }
  1047. if(this.end){
  1048. this.gameOver();
  1049. this.timer.stop();
  1050. }
  1051. }
  1052. public moveStage(){
  1053. if(this.end){
  1054. //处理游戏结束效果
  1055. //this.timer.stop();
  1056. }
  1057. this.timeIndex++;
  1058. if(this.timer.delay == 20 && this.startIndex == this.balls_pool.length - 1){
  1059. this.createBall();
  1060. }
  1061. //每执行一个球的距离,开始坐标+1
  1062. if(this.timeIndex % (~~(this.playerBall.width / this.speed))==0 && !this.end){
  1063. this.timeIndex = 0;
  1064. this.startIndex ++;
  1065. //如果还未初始化并且长度到达最后,则放慢移动速度,初始化动画播放完成
  1066. //console.log("this.startIndex:" + this.startIndex);
  1067. if(!this.inited && this.startIndex == this.balls_pool.length - 1){
  1068. this.timer.delay = 20;
  1069. this.canPlay = true;
  1070. this.inited = true;
  1071. this.speed = this.levelConfig[this.levelIndex].speed[DataBus.Instance.nanduIndex];
  1072. this.timer.stop();
  1073. this.timer.reset();
  1074. this.timer.start();
  1075. this.checkMin(this.index,this.balls_pool.length -2);
  1076. }
  1077. }
  1078. //console.log("index:" + this.index + " startIndex:" + this.startIndex);
  1079. if(this.startIndex < 0 ){
  1080. this.startIndex = 0;
  1081. }
  1082. this.check();
  1083. //this.checkMin();
  1084. //最后球全部弹进去之后,进行一次矫正
  1085. /*if(this.startIndex < this.index){
  1086. this.startIndex++;
  1087. }*/
  1088. this.moveTween(this.index,this.startIndex);
  1089. }
  1090. public inserttween(start:number,index:number,inPause?:boolean){
  1091. if(this.end){
  1092. return;
  1093. }
  1094. let speed = this.insertSpeed;
  1095. let balls_pool:Array<Ball> = this.balls_pool;
  1096. if(!inPause){
  1097. for(let i = start; i > 0 ; i--){
  1098. //如果后面运动的球的距离与前面静止的球的距离小于球的宽度,则前面的球也要运动
  1099. if(this.checkDif(balls_pool[i],balls_pool[i-1])){
  1100. this.index = i-1;
  1101. start = i - 1;
  1102. continue;
  1103. }
  1104. break;
  1105. }
  1106. }
  1107. for(let i = start ;i < index ; i++){
  1108. let ball:Ball = balls_pool[i];
  1109. let crux:any;
  1110. //获取配置文件的拐点坐标
  1111. if(ball.cruxIndex < this.mapConfig.crux.length){
  1112. crux = this.mapConfig.crux[ball.cruxIndex];
  1113. }else{
  1114. //如果已经走过所有节点,则直接向终点移动
  1115. let dif = this.diffPoint(ball.x,ball.y,this.mapConfig.endX,this.mapConfig.endY);
  1116. ball.x += (speed / dif) * (this.mapConfig.endX - ball.x);
  1117. ball.y += (speed / dif) * (this.mapConfig.endY - ball.y);
  1118. let difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  1119. let difCon = this.diffPoint(this.mapConfig.endX,this.mapConfig.endY,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  1120. if(difBall >= difCon){
  1121. this.end = true;
  1122. this.canPlay = false;
  1123. ball.x = this.mapConfig.endX;
  1124. ball.y = this.mapConfig.endY;
  1125. }
  1126. continue;
  1127. }
  1128. //如果在直线状态
  1129. if(!ball.inCrux ){
  1130. let dif = this.diffPoint(ball.x,ball.y,crux.startpoint.x,crux.startpoint.y);
  1131. ball.x += (speed / dif) * (crux.startpoint.x - ball.x);
  1132. ball.y += (speed / dif) * (crux.startpoint.y - ball.y);
  1133. let difBall;
  1134. let difCon;
  1135. //如果是刚开始那一段直线
  1136. if(ball.cruxIndex == 0){
  1137. difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.startX,this.mapConfig.startY);
  1138. difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.startX,this.mapConfig.startY);
  1139. }
  1140. else{
  1141. //如果移动到指定地点,则将坐标锁定在指定地点
  1142. difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  1143. difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y);
  1144. }
  1145. if(difBall >= difCon){
  1146. ball.difLine = difBall - difCon;
  1147. ball.x = crux.startpoint.x;
  1148. ball.y = crux.startpoint.y;
  1149. ball.inCrux = true;
  1150. ball.startAngle = ball.angle;
  1151. ball.targetAngle = ~~ball.angle + crux.angle;
  1152. //圆心的坐标
  1153. let circleX = crux.circle.x;
  1154. let circleY = crux.circle.y;
  1155. //半径
  1156. let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY);
  1157. //要移动的弧长,根据弧长和速度计算移动时间
  1158. let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2));
  1159. let time = perimeter / speed;
  1160. let difAngle = (ball.targetAngle - ball.angle) / time;
  1161. let wuAngle = ball.difLine / speed * difAngle;
  1162. ball.difLine = 0;
  1163. ball.angle += wuAngle;
  1164. let dify = radius * Math.cos(Math.PI * ball.angle / 180);
  1165. let difx = radius * Math.sin(Math.PI * ball.angle / 180);
  1166. if(crux.angle < 0){
  1167. ball.y = circleY - dify;
  1168. ball.x = circleX - difx;
  1169. }else{
  1170. ball.y = circleY + dify;
  1171. ball.x = circleX + difx;
  1172. }
  1173. }
  1174. }else{
  1175. //如果在拐歪状态
  1176. let circleX = crux.circle.x;
  1177. let circleY = crux.circle.y;
  1178. //半径
  1179. let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY);
  1180. ball.difLine = 0;
  1181. //如果刚好到关键点的开始处就后退,处理这个误差
  1182. if(ball.targetAngle == ball.angle){
  1183. ball.difLine = speed;
  1184. }
  1185. //要移动的弧长,根据弧长和速度计算移动时间
  1186. if(ball.targetAngle != ball.angle){
  1187. let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2));
  1188. let time = perimeter / speed;
  1189. let difAngle = (ball.targetAngle - ball.angle) / time;
  1190. ball.angle += difAngle;
  1191. }
  1192. if(crux.angle < 0){
  1193. //旋转结束
  1194. if((ball.angle - crux.angle) <= (ball.targetAngle - crux.angle)){
  1195. ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine;
  1196. ball.inCrux = false;
  1197. ball.x = crux.endpoint.x;
  1198. ball.y = crux.endpoint.y;
  1199. ball.angle = ball.targetAngle;
  1200. ball.cruxIndex++;
  1201. if(ball.angle == 180){
  1202. ball.x = crux.endpoint.x - ball.difLine;
  1203. ball.y = crux.endpoint.y;
  1204. }else if(ball.angle == 360){
  1205. ball.x = crux.endpoint.x + ball.difLine;
  1206. ball.y = crux.endpoint.y;
  1207. }else if(ball.angle == 270){
  1208. ball.x = crux.endpoint.x;
  1209. ball.y = crux.endpoint.y + ball.difLine;
  1210. }else if(ball.angle == 450){
  1211. ball.x = crux.endpoint.x;
  1212. ball.y = crux.endpoint.y - ball.difLine;
  1213. }else{
  1214. ball.x = crux.endpoint.x;
  1215. ball.y = crux.endpoint.y;
  1216. }
  1217. continue;
  1218. }
  1219. }else{
  1220. //旋转结束
  1221. if((ball.angle - crux.angle) >= (ball.targetAngle - crux.angle)){
  1222. ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine;
  1223. ball.inCrux = false;
  1224. ball.x = crux.endpoint.x;
  1225. ball.y = crux.endpoint.y;
  1226. ball.angle = ball.targetAngle;
  1227. ball.cruxIndex++;
  1228. if(ball.angle == 180){
  1229. ball.x = crux.endpoint.x - ball.difLine;
  1230. ball.y = crux.endpoint.y;
  1231. }else if(ball.angle == 360){
  1232. ball.x = crux.endpoint.x + ball.difLine;
  1233. ball.y = crux.endpoint.y;
  1234. }else if(ball.angle == 270){
  1235. ball.x = crux.endpoint.x;
  1236. ball.y = crux.endpoint.y + ball.difLine;
  1237. }else if(ball.angle == 450){
  1238. ball.x = crux.endpoint.x;
  1239. ball.y = crux.endpoint.y - ball.difLine;
  1240. }else{
  1241. ball.x = crux.endpoint.x;
  1242. ball.y = crux.endpoint.y;
  1243. }
  1244. continue;
  1245. }
  1246. }
  1247. let dify = radius * Math.cos(Math.PI * ball.angle / 180);
  1248. let difx = radius * Math.sin(Math.PI * ball.angle / 180);
  1249. if(crux.angle < 0){
  1250. ball.y = circleY - dify;
  1251. ball.x = circleX - difx;
  1252. }else{
  1253. ball.y = circleY + dify;
  1254. ball.x = circleX + difx;
  1255. }
  1256. }
  1257. }
  1258. if(this.end){
  1259. this.gameOver();
  1260. this.timer.stop();
  1261. }
  1262. }
  1263. public checkBack(index:number){
  1264. console.log("insertend:" + index);
  1265. if(index == 0 || index >= this.startIndex - 1){
  1266. return;
  1267. }
  1268. //如果插入的球和前面的球有间隔,且插入的球与前面的球类型相同,则后退操作
  1269. if(!this.checkDif(this.balls_pool[index],this.balls_pool[index - 1])){
  1270. if(this.balls_pool[index].type == this.balls_pool[index - 1].type){
  1271. console.log("进来了");
  1272. this.backIndex = index - 1;
  1273. this.timer.stop();
  1274. this.backstartIndex = this.backStartIndex(index - 1);
  1275. if(!this.backTimerInit){
  1276. this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => {
  1277. this.backTween();
  1278. }, this);
  1279. this.backTimerInit = true;
  1280. }
  1281. this.delNum = Math.floor(2/2) * (~~(this.playerBall.width / this.backSpeed));
  1282. this.backTimer.reset();
  1283. this.backTimer.start();
  1284. //this.isAttack = false;
  1285. }
  1286. }else if(!this.checkDif(this.balls_pool[index],this.balls_pool[index + 1])){
  1287. if(this.balls_pool[index].type == this.balls_pool[index + 1].type){
  1288. console.log("进来了");
  1289. this.backIndex = index;
  1290. this.timer.stop();
  1291. this.backstartIndex = this.backStartIndex(index);
  1292. if(!this.backTimerInit){
  1293. this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => {
  1294. this.backTween();
  1295. }, this);
  1296. this.backTimerInit = true;
  1297. }
  1298. this.delNum = Math.floor(2/2) * (~~(this.playerBall.width / this.backSpeed));
  1299. this.backTimer.reset();
  1300. this.backTimer.start();
  1301. //this.isAttack = false;
  1302. }
  1303. }
  1304. }
  1305. //插入动画
  1306. public insertTween(){
  1307. if(this.insertNum == ~~(this.playerBall.width / this.insertSpeed)){
  1308. this.insertNum = 0;
  1309. console.log("insertend:" + this.insertend);
  1310. this.checkCanMove(this.insertCheckIndex);
  1311. this.insertTimer.stop();
  1312. console.log("checkminend:" + this.insertend);
  1313. this.checkMin(this.insertstart,this.startIndex - 1);
  1314. this.checkBack(this.insertCheckIndex);
  1315. return;
  1316. }
  1317. this.insertNum++;
  1318. this.inserttween(this.insertstart,this.insertend+1);
  1319. }
  1320. //将球插入到已有数组
  1321. public insertBall(index:number,ball:Ball,isTail:boolean){
  1322. console.log("index " + index + " insertball");
  1323. this.balls_pool.splice(index,0,ball);
  1324. if(isTail){
  1325. this.balls_pool[index].moveBy(this.balls_pool[index - 1]);
  1326. this.insertend = index - 1;
  1327. }else{
  1328. console.log("不是尾部");
  1329. this.balls_pool[index].moveBy(this.balls_pool[index + 1]);
  1330. this.insertend = index;
  1331. }
  1332. if(index >= this.backIndex){
  1333. //this.backIndex--;
  1334. }else{
  1335. this.backIndex++;
  1336. }
  1337. let frontBreakIndex:number = 0;
  1338. this.insertCheckIndex = index;
  1339. this.insertstart = this.index;
  1340. //this.insertend = index;
  1341. //如果插入的是移动队列中
  1342. console.log("this.index:" + this.index);
  1343. if(index >= this.index){
  1344. this.insertTimer.start();
  1345. }
  1346. else{
  1347. console.log("index:" + index + " this.index:" + this.index);
  1348. //如果插入到前面已经停止的队列上,进行如下处理
  1349. let i;
  1350. //计算当前这个停止队列的首个球的下标
  1351. for(i = index; i > 0 ; i--){
  1352. if(!this.checkDif(this.balls_pool[i],this.balls_pool[i-1])){
  1353. frontBreakIndex = i;
  1354. break;
  1355. }
  1356. }
  1357. if(i == 0 ){
  1358. frontBreakIndex = 0;
  1359. }
  1360. //如果在最前面插入
  1361. if(index == frontBreakIndex){
  1362. this.balls_pool[frontBreakIndex].moveBy(this.balls_pool[frontBreakIndex + 1]);
  1363. }
  1364. this.insertstart = frontBreakIndex;
  1365. this.insertTimer.start();
  1366. this.index++;
  1367. }
  1368. this.startIndex++;
  1369. }
  1370. private async gameOver() {
  1371. //游戏结束给开放数据域发送当前用户信息进行历史最高分比较
  1372. if (!DataBus.Instance.userInfo) {
  1373. DataBus.Instance.power_added = 0;
  1374. Main.Instance.login();
  1375. return;
  1376. }
  1377. if (DataBus.Instance.buff_expire - new Date().getMilliseconds() < 0) {
  1378. Network.Instance.getHelpPowerInfo().catch(e => {
  1379. DataBus.Instance.power_added = 0;
  1380. DataBus.Instance.percent_added = 0;
  1381. });
  1382. }
  1383. await Network.Instance.gameOver().then((game_info: any) => {
  1384. console.log(game_info);
  1385. DataBus.Instance.game_id = game_info ? game_info.game_id : 0;
  1386. if (game_info.user_info) {
  1387. let uper:boolean = false;
  1388. if(DataBus.Instance.rebornCount < 3){
  1389. uper = true;
  1390. }
  1391. DataBus.Instance.userInfo = game_info.user_info;
  1392. platform.openDataContext.postMessage({
  1393. text: 'score',
  1394. score: DataBus.Instance.userInfo.max_score >= DataBus.Instance.score ? DataBus.Instance.userInfo.max_score : DataBus.Instance.score,
  1395. open_id: DataBus.Instance.userInfo.open_id,
  1396. uper: uper,
  1397. current_score: DataBus.Instance.score
  1398. });
  1399. }
  1400. }).catch(e => { console.log(e) });
  1401. Main.Instance.removeChild(Game.Instance);
  1402. if (DataBus.Instance.rebornCount < 3) {
  1403. Main.Instance.addChild(GameOver1.Instance);
  1404. } else {
  1405. platform.openDataContext.postMessage({
  1406. text: 'overRank',
  1407. });
  1408. Main.Instance.addChild(RebackFirstPage.Instance);
  1409. }
  1410. }
  1411. public cos(x,y,x1,y1){
  1412. let difX = x1 - x;
  1413. let difY = y1 - y;
  1414. let z = Math.sqrt(Math.pow(difX, 2) + Math.pow(difY, 2));
  1415. this.sinValue = difX / z;
  1416. this.cosValue = difY / z;
  1417. }
  1418. //检测是否相撞
  1419. public checkAttack(index1:number){
  1420. let length = this.startIndex;
  1421. let playerBall = this.fireBalls[index1];
  1422. for(let i = 0 ; i < length ; i++){
  1423. let dif = this.diffPoint(this.balls_pool[i].x,this.balls_pool[i].y,this.fireBalls[index1].x,this.fireBalls[index1].y);
  1424. //如果相撞,进行碰撞处理
  1425. if(dif < this.playerBall.width * 2 / 3){
  1426. GameUtil.Sound.playEffect("ball_wav");
  1427. let index = i;
  1428. //向上运动
  1429. if(this.balls_pool[i].angle % 360 == 90){
  1430. if(this.playerBall.y > this.balls_pool[i].y){
  1431. index++;
  1432. }
  1433. }else if(this.balls_pool[i].angle % 360 == 270){
  1434. if(this.playerBall.y < this.balls_pool[i].y){
  1435. index++;
  1436. }
  1437. }else if(this.balls_pool[i].angle % 360 > 90 && this.balls_pool[i].angle % 360 < 270){
  1438. if(this.playerBall.x > this.balls_pool[i].x){
  1439. index++;
  1440. }
  1441. }else{
  1442. if(this.playerBall.x < this.balls_pool[i].x){
  1443. index++;
  1444. }
  1445. }
  1446. //是否是尾部插入
  1447. let isTail:boolean = false;
  1448. if(index == i + 1){
  1449. //如果插入地方的两个球距离小,则不是在后面插入
  1450. if(this.checkDif(this.balls_pool[i],this.balls_pool[index])){
  1451. isTail = false;
  1452. }else{
  1453. isTail = true;
  1454. }
  1455. }
  1456. this.isAttack = false;
  1457. //如果是爆裂球
  1458. if(playerBall.type == GameProp.propType.boom){
  1459. this.booms(playerBall,index);
  1460. }else{
  1461. this.insertBall(index,playerBall,isTail);
  1462. }
  1463. this.fireBalls.splice(index1,1);
  1464. //this.checkCanMove(i);
  1465. break;
  1466. }
  1467. }
  1468. }
  1469. //爆裂球
  1470. public booms(ball:Ball,boomindex:number){
  1471. this.ballContainer.removeChild(ball);
  1472. let index:number = 0;
  1473. let idArr:number[] = new Array();
  1474. let indexjian:number = 0;
  1475. for(let i = 0;i < this.startIndex - 1;i++){
  1476. if(this.diffPoint(this.balls_pool[i].x,this.balls_pool[i].y,ball.x,ball.y) < 100){
  1477. let obj = this.balls_pool[i];
  1478. egret.Tween.get(obj).to({scaleX:1.2,scaleY:1.2},100).to({scaleX:0.1,scaleY:0.1},100).call(() => {
  1479. obj.visible = false;
  1480. });
  1481. idArr.push(i);
  1482. index = i;
  1483. }
  1484. }
  1485. this.boomEffect(boomindex,idArr.length);
  1486. this.updateScore(idArr.length);
  1487. for(let i = this.startIndex - 1;i > 0 ;i--){
  1488. for(let j = 0; j < idArr.length;j++){
  1489. if(idArr[j]==i){
  1490. this.balls_pool.splice(i,1);
  1491. this.startIndex--;
  1492. if(i > this.index){
  1493. indexjian++;
  1494. }
  1495. break;
  1496. }
  1497. }
  1498. }
  1499. if(index > this.index){
  1500. this.index = index + 1;
  1501. this.index -= indexjian;
  1502. }
  1503. }
  1504. //检测两个球的距离是否相邻
  1505. public checkDif(ball1:Ball,ball2:Ball){
  1506. if(ball1 == null || ball2 == null){
  1507. return false;
  1508. }
  1509. if(this.diffPoint(ball1.x,ball1.y,ball2.x,ball2.y) > this.playerBall.width + 3){
  1510. return false;
  1511. }
  1512. return true;
  1513. }
  1514. public checkDifMax(ball1:Ball,ball2:Ball){
  1515. if(this.diffPoint(ball1.x,ball1.y,ball2.x,ball2.y) > this.playerBall.width + 5){
  1516. return false;
  1517. }
  1518. return true;
  1519. }
  1520. //检测是否可消除
  1521. public checkCanMove(index:number){
  1522. if(this.end){
  1523. return;
  1524. }
  1525. console.log("checkCanMove");
  1526. let i:number = index - 1;
  1527. let j:number = index + 1;
  1528. let startIndex:number = index;
  1529. let num:number = 1;
  1530. let length = this.balls_pool.length;
  1531. for(i ; i >= 0 ;i--){
  1532. if(this.balls_pool[i].type == this.balls_pool[index].type && this.checkDif(this.balls_pool[i],this.balls_pool[i + 1])){
  1533. startIndex = i;
  1534. num++;
  1535. }else{
  1536. break;
  1537. }
  1538. }
  1539. for(j ; j < this.startIndex - 1; j++){
  1540. if(this.balls_pool[j].type == this.balls_pool[index].type && this.checkDif(this.balls_pool[j],this.balls_pool[j - 1])){
  1541. num++;
  1542. }else{
  1543. break;
  1544. }
  1545. }
  1546. if(num > 2){
  1547. console.log("xiaochu");
  1548. //GameUtil.Sound.playEffect("glass_wav");
  1549. this.boomEffect(index,num);
  1550. for(let i = 0;i<num; i++){
  1551. let obj = this.balls_pool[startIndex+i];
  1552. egret.Tween.get(obj).to({scaleX:1.2,scaleY:1.2},100).to({scaleX:0.1,scaleY:0.1},100).call(() => {
  1553. obj.visible = false;
  1554. });
  1555. console.log("startIndex:" + (startIndex+i))
  1556. }
  1557. //如果不是首
  1558. //j!= this.startIndex - 1 &&
  1559. if(i!= -1){
  1560. //如果消除后边上的两个球要是相同颜色,则前面的球后退
  1561. console.log("i:" + i);
  1562. console.log("j:" + j);
  1563. if(this.balls_pool[i].type == this.balls_pool[j].type){
  1564. this.balls_pool.splice(startIndex,num);
  1565. this.insertend -= num;
  1566. this.backIndex = i;
  1567. this.timer.stop();
  1568. this.backstartIndex = this.backStartIndex(i);
  1569. if(!this.backTimerInit){
  1570. this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => {
  1571. //this.backIndex = this.balls_pool.length - 2;
  1572. this.backTween();
  1573. }, this);
  1574. this.backTimerInit = true;
  1575. }
  1576. this.updateScore(num);
  1577. this.delNum = Math.floor(num/2) * (~~(this.playerBall.width / this.backSpeed));
  1578. DataBus.Instance.scorePower++;
  1579. if(DataBus.Instance.scorePower > 1){
  1580. DataBus.Instance.lianjiNum++;
  1581. }
  1582. this.backTimer.reset();
  1583. this.backTimer.start();
  1584. //this.isAttack = false;
  1585. this.startIndex -= num;
  1586. return true;
  1587. }else{
  1588. //前面的球不动
  1589. console.log("qianmianbudong");
  1590. this.balls_pool.splice(startIndex,num);
  1591. this.insertend -= num;
  1592. this.updateScore(num);
  1593. //如果插入的坐标处于正在移动的对列
  1594. if(index >= this.index){
  1595. this.index = i + 1;
  1596. }
  1597. this.delNum = 1;
  1598. //this.isAttack = false;
  1599. this.canPlay = true;
  1600. }
  1601. }else if(i == -1){
  1602. //如果是头
  1603. this.balls_pool.splice(startIndex,num);
  1604. this.insertend -= num;
  1605. if(index >= this.index){
  1606. this.index = i + 1;
  1607. }
  1608. this.delNum = 1;
  1609. this.updateScore(num);
  1610. //this.isAttack = false;
  1611. this.canPlay = true;
  1612. }
  1613. this.startIndex -= num;
  1614. this.backIndex -= num;
  1615. //return true;
  1616. }else{
  1617. //this.isAttack = false;
  1618. this.canPlay = true;
  1619. return false;
  1620. }
  1621. }
  1622. public boomByIndex(index:number){
  1623. this.boomMC.x = this.balls_pool[index].x;
  1624. this.boomMC.y = this.balls_pool[index].y;
  1625. //this.ballContainer.swapChildren(this.boomMC,this.balls_pool[this.startIndex - 1]);
  1626. this.boomMC.visible = true;
  1627. this.boomMC.gotoAndPlay(1,1);
  1628. }
  1629. public boomEffect(index:number,num:number){
  1630. this.boomMC.x = this.balls_pool[index].x;
  1631. this.boomMC.y = this.balls_pool[index].y;
  1632. this.ballContainer.swapChildren(this.boomMC,this.balls_pool[this.startIndex - 1]);
  1633. this.boomMC.visible = true;
  1634. this.boomMC.gotoAndPlay(1,1);
  1635. if(DataBus.Instance.scorePower > 0){
  1636. if(DataBus.Instance.shock == GameProp.shockType.on){
  1637. platform.vibrate(false).catch((e) => { console.log(e) });
  1638. }
  1639. //this.ballContainer.swapChildren(this.lianjiEffect,this.balls_pool[this.startIndex]);
  1640. this.lianjiEffect.text = "连击x" + (DataBus.Instance.scorePower + 1) + "\n +" + num *10 * Math.pow(2,DataBus.Instance.scorePower);;
  1641. this.lianjiEffect.x = this.balls_pool[index].x;
  1642. this.lianjiEffect.y = this.balls_pool[index].y - 20;
  1643. this.lianjiEffect.scaleX = 0;
  1644. this.lianjiEffect.scaleY = 0;
  1645. egret.Tween.removeTweens(this.lianjiEffect);
  1646. egret.Tween.get(this.lianjiEffect).to({scaleX:1.2,scaleY:1.2},200).wait(500).to({scaleX:0,scaleY:0},100);
  1647. }else{
  1648. this.ballContainer.swapChildren(this.lianjiEffect,this.balls_pool[this.startIndex]);
  1649. this.lianjiEffect.text = " " + "\n+" + num *10 * Math.pow(2,DataBus.Instance.scorePower);;
  1650. this.lianjiEffect.x = this.balls_pool[index].x;
  1651. this.lianjiEffect.y = this.balls_pool[index].y - 20;
  1652. this.lianjiEffect.scaleX = 0;
  1653. this.lianjiEffect.scaleY = 0;
  1654. egret.Tween.removeTweens(this.lianjiEffect);
  1655. egret.Tween.get(this.lianjiEffect).to({scaleX:1.2,scaleY:1.2},200).wait(500).to({scaleX:0,scaleY:0},100);
  1656. }
  1657. }
  1658. //更新游戏分数,并根据分数调整游戏难度
  1659. public updateScore(num:number){
  1660. DataBus.Instance.score += num *10 * Math.pow(2,DataBus.Instance.scorePower);
  1661. DataBus.Instance.gradeScore += num *10 * Math.pow(2,DataBus.Instance.scorePower);
  1662. DataBus.Instance.ballNum += num;
  1663. for(let i = this.levelConfig[this.levelIndex].score.length - 1; i > 0;i--){
  1664. if(DataBus.Instance.score > this.levelConfig[this.levelIndex].score[i]){
  1665. DataBus.Instance.nanduIndex = i;
  1666. if(this.inited){
  1667. this.speed = this.levelConfig[this.levelIndex].speed[i];
  1668. }break;
  1669. }
  1670. }
  1671. this.exper.mask = new egret.Rectangle(0,0,DataBus.Instance.gradeScore/DataBus.Instance.gradeNeedScore*this.stage.width,DataBus.Instance.gradeScore/DataBus.Instance.gradeNeedScore*this.stage.width);
  1672. this.score.text = '' + DataBus.Instance.score;
  1673. this.gradeScore.text = DataBus.Instance.gradeScore + '/' + DataBus.Instance.gradeNeedScore;
  1674. this.score.anchorOffsetX = this.score.width / 2;
  1675. egret.Tween.get(this.score).to({scaleX:1.2,scaleY:1.2},100).to({scaleX:1,scaleY:1},100);
  1676. if(DataBus.Instance.gradeScore >= DataBus.Instance.gradeNeedScore && !this.backTimer.running){
  1677. //通关
  1678. this.end = true;
  1679. this.timer.stop();
  1680. this.backTimer.stop();
  1681. this.insertTimer.stop();
  1682. this.pass = true;
  1683. //this.passThrough();
  1684. }
  1685. }
  1686. public passThrough(){
  1687. /*for(let i = 1;i < this.startIndex - 1;i = i + 3){
  1688. setTimeout(()=>{
  1689. this.boomMC.x = this.balls_pool[1].x;
  1690. this.boomMC.y = this.balls_pool[1].y;
  1691. //this.ballContainer.swapChildren(this.boomMC,this.balls_pool[this.startIndex - 1]);
  1692. this.boomMC.visible = true;
  1693. this.boomMC.gotoAndPlay(1,1);
  1694. },this,1000);
  1695. }*/
  1696. this.addChild(this.throughContainer);
  1697. DataBus.Instance.grade++;
  1698. //等级缓存到本地
  1699. //egret.localStorage.setItem("grade",DataBus.Instance.grade + '');
  1700. let blackBg = new egret.Bitmap(RES.getRes('bg_black1_png'));
  1701. blackBg.x = 0;
  1702. blackBg.y = 0;
  1703. blackBg.width = this.stage.stageWidth;
  1704. blackBg.height = this.stage.stageHeight;
  1705. this.throughContainer.addChild(blackBg);
  1706. blackBg.touchEnabled = true;
  1707. blackBg.addEventListener(egret.TouchEvent.TOUCH_TAP, (event: egret.TouchEvent) => {
  1708. event.stopImmediatePropagation();
  1709. this.throughContainer.removeChildren();
  1710. this.removeChild(this.throughContainer);
  1711. this.restart(-1);
  1712. }, this);
  1713. let through = new egret.Bitmap(RES.getRes('through_png'));
  1714. through.width = 640;
  1715. through.height = 841;
  1716. through.x = this.stage.stageWidth;
  1717. through.y = 64;
  1718. this.throughContainer.addChild(through);
  1719. egret.Tween.get(through).to({x:0},1000,egret.Ease.bounceOut);
  1720. let ball = new egret.TextField();
  1721. ball.text = '+' + DataBus.Instance.ballNum;
  1722. ball.y = this.stage.stageHeight;
  1723. ball.size=25;
  1724. ball.bold = true;
  1725. ball.textColor=0x422904;
  1726. ball.x = this.stage.stageWidth / 2 - 300 + 346;
  1727. ball.fontFamily="Arial";
  1728. this.throughContainer.addChild(ball);
  1729. egret.Tween.get(ball).to({y:518},1200,egret.Ease.bounceInOut);
  1730. let lianji = new egret.TextField();
  1731. lianji.text = '+' + DataBus.Instance.lianjiNum * 10;
  1732. lianji.y = this.stage.stageHeight;
  1733. lianji.size=25;
  1734. lianji.bold = true;
  1735. lianji.textColor=0x422904;
  1736. lianji.x = this.stage.stageWidth / 2 - 300 + 346
  1737. lianji.fontFamily="Arial";
  1738. this.throughContainer.addChild(lianji);
  1739. egret.Tween.get(lianji).to({y:579},1400,egret.Ease.bounceInOut);
  1740. let crazy = new egret.TextField();
  1741. crazy.text = '+' + DataBus.Instance.lianjiBigTwoNum * 100;
  1742. crazy.y = this.stage.stageHeight;
  1743. crazy.size=25;
  1744. crazy.bold = true;
  1745. crazy.textColor=0x422904;
  1746. crazy.x = this.stage.stageWidth / 2 - 300 + 346;
  1747. crazy.fontFamily="Arial";
  1748. this.throughContainer.addChild(crazy);
  1749. egret.Tween.get(crazy).to({y:645},1600,egret.Ease.bounceInOut);
  1750. let juli = new egret.TextField();
  1751. juli.text = '+' + (this.mapConfig.crux.length - this.balls_pool[0].cruxIndex) * 10;
  1752. juli.y = this.stage.stageHeight;
  1753. juli.size=25;
  1754. juli.bold = true;
  1755. juli.textColor=0x422904;
  1756. juli.x = this.stage.stageWidth / 2 - 300 + 346;
  1757. juli.fontFamily="Arial";
  1758. this.throughContainer.addChild(juli);
  1759. egret.Tween.get(juli).to({y:709},1800,egret.Ease.bounceInOut);
  1760. let zong = new egret.TextField();
  1761. zong.text = '+' + (DataBus.Instance.ballNum + DataBus.Instance.lianjiNum * 10 + DataBus.Instance.lianjiBigTwoNum * 100 + (this.mapConfig.crux.length - this.balls_pool[0].cruxIndex) * 10);
  1762. zong.y = this.stage.stageHeight;
  1763. zong.size=30;
  1764. zong.bold = true;
  1765. zong.textColor=0xf9372d;
  1766. zong.x = this.stage.stageWidth / 2 - 300 + 346;
  1767. zong.fontFamily="Arial";
  1768. this.throughContainer.addChild(zong);
  1769. egret.Tween.get(zong).to({y:785},2000,egret.Ease.bounceInOut);
  1770. let jixu = new egret.TextField();
  1771. jixu.text = '点击任意位置继续';
  1772. jixu.y = 900;
  1773. jixu.size=25;
  1774. jixu.textColor=0xffffff;
  1775. jixu.anchorOffsetX = jixu.width / 2;
  1776. jixu.anchorOffsetY = jixu.height / 2;
  1777. jixu.scaleX = 1;
  1778. jixu.scaleY = 1;
  1779. jixu.x = this.stage.stageWidth / 2;
  1780. jixu.fontFamily="Arial";
  1781. this.throughContainer.addChild(jixu);
  1782. egret.Tween.get(jixu,{loop:true}).to({scaleX:1.1,scaleY:1.1},1000).to({scaleX:1.0,scaleY:1.0},1000);
  1783. DataBus.Instance.score += DataBus.Instance.ballNum + DataBus.Instance.lianjiNum * 10 + DataBus.Instance.lianjiBigTwoNum * 100 + (this.mapConfig.crux.length - this.balls_pool[0].cruxIndex) * 10;
  1784. }
  1785. //计算玩家的球与目标球的距离
  1786. public diffPoint(x,y,x1,y1){
  1787. let difX = x - x1;
  1788. let difY = y - y1;
  1789. //如果两点相同,则返回1进行误差处理
  1790. if(difX == 0 && difY == 0){
  1791. return 1;
  1792. }
  1793. return Math.sqrt(Math.pow(difX, 2) + Math.pow(difY, 2));
  1794. }
  1795. private enterFrame(event: egret.TouchEvent) {
  1796. DataBus.Instance.frame++;
  1797. this.playNumEnterFrame++;
  1798. if(this.pass){
  1799. this.passFrame++;
  1800. if(this.passIndex > this.balls_pool.length - 2){
  1801. this.pass = false;
  1802. this.passIndex = 2;
  1803. for(let i = 0; i < this.balls_pool.length - 1;i++){
  1804. this.balls_pool[i].visible = false;
  1805. }
  1806. this.passThrough();
  1807. return;
  1808. }
  1809. if(this.passFrame > 10){
  1810. this.boomByIndex(this.passIndex);
  1811. for(let i = 0; i < this.passIndex;i++){
  1812. this.balls_pool[i].visible = false;
  1813. }
  1814. //this.balls_pool.splice(0,5);
  1815. this.passIndex += 5;
  1816. this.passFrame = 0;
  1817. }
  1818. }
  1819. if(this.end){
  1820. return ;
  1821. }
  1822. //console.log(DataBus.Instance.frame);
  1823. //结束特效
  1824. /*if (DataBus.Instance.frame >= 10){
  1825. DataBus.Instance.frame = 0;
  1826. if(this.balls_pool[0].cruxIndex == this.mapConfig.crux.length && this.balls_pool[0].x < 400){
  1827. this.blast.visible = !this.blast.visible;
  1828. this.mapend.visible = !this.mapend.visible;
  1829. }else{
  1830. this.blast.visible = false;
  1831. this.mapend.visible = false;
  1832. }
  1833. }*/
  1834. if(this.haveDot){
  1835. this.dotContainer.getChildAt(0).visible = true;
  1836. let sinValue = this.sinValue;
  1837. let cosValue = this.cosValue;
  1838. let i:number;
  1839. for(i = 0;i < this.dotLength - 1;i++){
  1840. this.dotContainer.getChildAt(i+1).x = this.dotContainer.getChildAt(i).x + this.playerBall.width / 3 * sinValue * 2;
  1841. this.dotContainer.getChildAt(i+1).y = this.dotContainer.getChildAt(i).y + this.playerBall.width / 3 * cosValue * 2;
  1842. this.dotContainer.getChildAt(i+1).visible = true;
  1843. let j:number;
  1844. for(j = 0; j < this.balls_pool.length;j++){
  1845. if(this.diffPoint(this.dotContainer.getChildAt(i+1).x,this.dotContainer.getChildAt(i+1).y,this.balls_pool[j].x,this.balls_pool[j].y) < this.playerBall.width / 2
  1846. || this.dotContainer.getChildAt(i+1).x < 0
  1847. || this.dotContainer.getChildAt(i+1).x > this.ballContainer.width
  1848. || this.dotContainer.getChildAt(i+1).y < 0
  1849. || this.dotContainer.getChildAt(i+1).y > this.ballContainer.height){
  1850. this.dotContainer.getChildAt(i+1).visible = false;
  1851. break;
  1852. }
  1853. }
  1854. if(j != this.balls_pool.length){
  1855. break;
  1856. }
  1857. /*if( this.dotContainer.getChildAt(i+1).x < 0 ){
  1858. sinValue = -this.sinValue;
  1859. this.dotContainer.getChildAt(i+1).x = -this.dotContainer.getChildAt(i+1).x;
  1860. }else if(this.dotContainer.getChildAt(i+1).x > this.ballContainer.width ){
  1861. sinValue = -this.sinValue;
  1862. this.dotContainer.getChildAt(i+1).x =this.ballContainer.width * 2 - this.dotContainer.getChildAt(i+1).x;
  1863. }
  1864. //边界处理
  1865. if(this.dotContainer.getChildAt(i+1).y < 0 ){
  1866. cosValue = -this.cosValue;
  1867. this.dotContainer.getChildAt(i+1).y = -this.dotContainer.getChildAt(i+1).y;
  1868. }else if(this.dotContainer.getChildAt(i+1).y > this.ballContainer.height){
  1869. cosValue = -this.cosValue;
  1870. this.dotContainer.getChildAt(i+1).y = this.ballContainer.height * 2 - this.dotContainer.getChildAt(i+1).y;
  1871. }*/
  1872. }
  1873. //如果前面的点与球有碰撞,则后面都不显示
  1874. for(i;i < this.dotLength - 1;i++){
  1875. this.dotContainer.getChildAt(i+1).visible = false;
  1876. }
  1877. }else{
  1878. if(this.dotContainer.getChildAt(0).visible == true){
  1879. for(let i = 0;i < this.dotLength ;i++){
  1880. this.dotContainer.getChildAt(i).visible = false;
  1881. }
  1882. }
  1883. }
  1884. //if(this.isAttack){
  1885. for(let i = 0;i < this.fireBalls.length; i++){
  1886. let playerBall = this.fireBalls[i];
  1887. this.canPlay = false;
  1888. playerBall.x += playerBall.speed * playerBall.sinValue;
  1889. playerBall.y += playerBall.speed * playerBall.cosValue;
  1890. //this.playerBall.speed = this.playerBall.speed + 0.1;
  1891. //边界处理
  1892. if(playerBall.x < 0 || playerBall.x > this.ballContainer.width){
  1893. //this.sinValue = -this.sinValue;
  1894. this.ballContainer.removeChild(playerBall);
  1895. this.fireBalls.splice(i,1);
  1896. this.canPlay = true;
  1897. this.isAttack = false;
  1898. break;
  1899. }
  1900. //边界处理
  1901. if(playerBall.y < 0 || playerBall.y > this.ballContainer.height){
  1902. //this.cosValue = -this.cosValue;
  1903. this.ballContainer.removeChild(playerBall);
  1904. this.fireBalls.splice(i,1);
  1905. this.canPlay = true;
  1906. this.isAttack = false;
  1907. break;
  1908. }
  1909. this.checkAttack(i);
  1910. }
  1911. // }
  1912. }
  1913. public shakeBg(time = 0.1, rate = 60, distance = 2) {
  1914. ShakeTool.getInstance().shakeObj(BackGround.Instance, time, rate, distance);
  1915. }
  1916. }
  1917. class BgEffect extends egret.Sprite {
  1918. private static shapes: Array<egret.Texture>;
  1919. private static lines: Array<egret.Texture>;
  1920. private shape: egret.Bitmap;
  1921. public constructor() {
  1922. super();
  1923. this.once(egret.Event.ADDED_TO_STAGE, () => {
  1924. BgEffect.shapes = new Array();
  1925. BgEffect.lines = new Array();
  1926. for (let i = 1; i < 7; i++) {
  1927. BgEffect.shapes.push(RES.getRes('eff' + i + '_png'));
  1928. BgEffect.lines.push(RES.getRes('eff' + i + '_' + i + '_png'));
  1929. }
  1930. let rnd = ~~(Math.random() * BgEffect.shapes.length);
  1931. this.alpha = 0;
  1932. this.shape = new egret.Bitmap(BgEffect.shapes[rnd]);
  1933. this.shape.x = 140;
  1934. this.shape.y = 140;
  1935. this.anchorOffsetX = this.shape.anchorOffsetX = 140;
  1936. this.anchorOffsetY = this.shape.anchorOffsetY = 140;
  1937. this.addChild(this.shape);
  1938. for (let i = 0; i < 8; i++) {
  1939. let angle = Math.random() * Math.PI * 2;
  1940. let line = new LineSprite(BgEffect.lines[rnd]);
  1941. line.x = 140;
  1942. line.y = 140;
  1943. line.speedx = Math.cos(angle) * 8 * this.scaleX;
  1944. line.speedy = Math.sin(angle) * 8 * this.scaleX;
  1945. line.rotation = angle / Math.PI * 180;
  1946. this.addChild(line);
  1947. }
  1948. }, this);
  1949. }
  1950. }
  1951. class LineSprite extends egret.Bitmap {
  1952. speedx: number;
  1953. speedy: number;
  1954. update() {
  1955. this.x += this.speedx;
  1956. this.y += this.speedy;
  1957. this.scaleX -= 0.03;
  1958. }
  1959. }
  1960. /**
  1961. * 震动工具
  1962. * @author chenkai
  1963. * [url=home.php?mod=space&uid=81950]@since[/url] 2017/5/24
  1964. *
  1965. * Example:
  1966. * 震动目标obj,1秒内震动10次,震动最大距离10
  1967. * ShakeTool.getInstance().shakeObj(obj, 1, 10, 10);
  1968. */
  1969. class ShakeTool {
  1970. private static instance: ShakeTool; //单例
  1971. private initX: number; //初始位置
  1972. private initY: number;
  1973. private target: egret.DisplayObject; //震动目标
  1974. private maxDis: number; //震动距离
  1975. private count: number = 0; //计时器次数
  1976. private rate: number; //一秒震动次数
  1977. private timer: egret.Timer = new egret.Timer(200);
  1978. public static getInstance(): ShakeTool {
  1979. if (this.instance == null) {
  1980. this.instance = new ShakeTool();
  1981. }
  1982. return this.instance;
  1983. }
  1984. /**
  1985. * 震动显示对象
  1986. * @param target 震动目标对象
  1987. * @param time 震动持续时长(秒)
  1988. * @param rate 震动频率(一秒震动多少次)
  1989. * @param maxDis 震动最大距离
  1990. */
  1991. public shakeObj(target: egret.DisplayObject, time: number, rate: number, maxDis: number): void {
  1992. if (this.target) {
  1993. return;
  1994. }
  1995. this.target = target;
  1996. this.initX = target.x;
  1997. this.initY = target.y;
  1998. this.maxDis = maxDis;
  1999. this.count = time * rate;
  2000. this.rate = rate;
  2001. this.timer.delay = 200 / rate;
  2002. this.timer.repeatCount = this.count;
  2003. this.timer.addEventListener(egret.TimerEvent.TIMER, this.shaking, this);
  2004. this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.shakeComplete, this);
  2005. this.timer.reset();
  2006. this.timer.start();
  2007. }
  2008. private shaking(): void {
  2009. egret.Tween.removeTweens(this.target);
  2010. this.target.x = this.initX - this.maxDis + Math.random() * this.maxDis * 2;
  2011. this.target.y = this.initY - this.maxDis + Math.random() * this.maxDis * 2;
  2012. egret.Tween.get(this.target).to({ x: this.initX, y: this.initY }, 999 / this.rate);
  2013. }
  2014. private shakeComplete(): void {
  2015. if (this.target) {
  2016. egret.Tween.removeTweens(this.target);
  2017. this.target.x = this.initX;
  2018. this.target.y = this.initY;
  2019. this.target = null;
  2020. }
  2021. this.timer.removeEventListener(egret.TimerEvent.TIMER, this.shaking, this);
  2022. this.timer.removeEventListener(egret.TimerEvent.TIMER_COMPLETE, this.shakeComplete, this);
  2023. }
  2024. /**停止震动 */
  2025. public stop() {
  2026. this.shakeComplete();
  2027. }
  2028. }