大家乐捕鱼游戏里的onOpen函数

在场景的onOpen里定义几个函数:

原文:http://www.javasfs.com/sf/28.html

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. var win = this;  
  2. function isObjInvisible(obj) {return !obj.visible;}  
  3. //当炮弹和鱼移动到场景之外时,把它们设置为不可见不可用,后面重用这些对象,以减小内存开销。  
  4. function handleOnMoved() {  
  5.     var x = this.x, y = this.y, r = x + this.w, b = y + this.h, w = win.w, h = win.h;  
  6.     if(x > w || y > h || r < 0 || b < 0) {  
  7.         this.setVisible(false).setEnable(false);  
  8.     }  
  9. }  
  10. //定时产生鱼  
  11. function makeFish() {  
  12.     if(!win.info) return;  
  13.     var info = win.info,cannon = info.cannon;  
  14.     var fish = info.fishs.find(isObjInvisible);  
  15.     if(!fish && info.fishs.length < 20) {  
  16.         var index = Math.floor(10 * Math.random())+1;  
  17.         fish = win.dupChild("ui-fish"+index);  
  18.           
  19.     }  
  20.     if(fish) {  
  21.         info.fishs.push(fish);  
  22.         var y = Math.max(0, Math.floor((win.h - info.bottom.h) * Math.random() - fish.h));  
  23.         var x =  y%2 ? -fish.w : win.w;  
  24.         var vx = x > 0 ? -1 : 1;  
  25.         var rotation = x > 0 ? Math.PI : 0;  
  26.         fish.setPosition(x, y).setVisible(true).setEnable(true).setV(vx, 0).setRotation(rotation).play("live");  
  27.     }  
  28.     setTimeout(makeFish, 500);  
  29. }  
  30. //初始化游戏  
  31. win.initGame = function() {  
  32.     var info = {bullets:[], fishs:[], cannonType:1, scoreValue:0};  
  33.     var cannon = this.find("ui-cannon"true);  
  34.     var p = cannon.getPositionInWindow();   
  35.     cannon.center ={x: p.x + (cannon.w >> 1), y: p.y + (cannon.h >> 1)};      
  36.     info.cannon = cannon;  
  37.   
  38.     info.bottom = this.find("ui-image");  
  39.     var totalScore = this.find("ui-total-score"true);  
  40.     p = totalScore.getPositionInWindow();   
  41.     totalScore.center ={x: p.x + (totalScore.w >> 1), y: p.y + (totalScore.h >> 1)};      
  42.     info.totalScore = totalScore;  
  43.       
  44.     var bullet = this.find("ui-bullet").setVisible(false).setEnable(false);  
  45.     bullet.handleOnMoved = handleOnMoved;  
  46.     info.bullets.push(bullet);  
  47.       
  48.     info.coin = this.find("ui-coin").setVisible(false);  
  49.     info.score = this.find("ui-score").setVisible(false);  
  50.     for(var i = 0; i < this.children.length; i++) {  
  51.         var iter = this.children[i];  
  52.         if(iter.name.indexOf("ui-fish") >= 0) {  
  53.             iter.handleOnMoved = handleOnMoved;  
  54.             iter.setEnable(false).setVisible(false);  
  55.             info.fishs.push(iter);  
  56.         }  
  57.     }  
  58.     info.timerID = setTimeout(makeFish, 1000);  
  59.     this.info = info;  
  60. }  
  61. //改变大炮类型  
  62. win.changeCannon = function(delta) {  
  63.     var info = this.info,cannon = info.cannon;  
  64.     info.cannonType = Math.max(1, Math.min(7, info.cannonType + delta));  
  65.     cannon.play("default"+info.cannonType, 100000);  
  66. }  
  67. //炮弹打中鱼时,炮弹变成网,鱼播放die动画,金币从炮弹处移动到总金币处。  
  68. win.onContacted = function(bullet, fish) {  
  69.     var info = this.info,cannon = info.cannon;  
  70.     bullet.setEnable(false).play("web"+info.cannonType, 1, function() {bullet.setVisible(false);});  
  71.     fish.setEnable(false).play("die", 1, function() {fish.setVisible(false)});  
  72.     info.scoreValue += 100;  
  73.     info.totalScore.setValue(info.scoreValue);  
  74.     info.coin.setVisible(true).animate({xStart:bullet.x, yStart:bullet.y, xEnd:info.totalScore.x, yEnd:info.totalScore.y-20});  
  75. }  
  76. //点击场景时,调整大炮位置,发射炮弹,大炮播放射击动画。  
  77. win.handleClick = function(point) {  
  78.     var info = this.info,cannon = info.cannon;  
  79.     if(this.targetShape != this.info.bottom) {  
  80.         var angle = Math.lineAngle(cannon.center, point) - 1.5 * Math.PI;  
  81.         cannon.setRotation(angle);  
  82.         var bullet = info.bullets.find(isObjInvisible);  
  83.         if(!bullet)  {  
  84.             bullet = win.dupChild("ui-bullet",0);  
  85.             info.bullets.push(bullet);  
  86.         }  
  87.         var x = cannon.center.x - (bullet.w >> 1), y = cannon.center.y - (bullet.h >> 1);  
  88.         bullet.setPosition(x, y).setRotation(angle).setVisible(true).setEnable(true).setV(5*Math.sin(angle),-5*Math.cos(angle));  
  89.         bullet.play("bullet"+info.cannonType);  
  90.         cannon.play("fire"+info.cannonType, 1);  
  91.         console.log(angle);  
  92.     }  
  93. }  
  94.   
  95. this.initGame();  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值