您的位置:首页 > 运维架构

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

2016-12-22 11:03 281 查看
在场景的onOpen里定义几个函数:

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

[javascript] view
plain copy

 





var win = this;  

function isObjInvisible(obj) {return !obj.visible;}  

//当炮弹和鱼移动到场景之外时,把它们设置为不可见不可用,后面重用这些对象,以减小内存开销。  

function handleOnMoved() {  

    var x = this.x, y = this.y, r = x + this.w, b = y + this.h, w = win.w, h = win.h;  

    if(x > w || y > h || r < 0 || b < 0) {  

        this.setVisible(false).setEnable(false);  

    }  

}  

//定时产生鱼  

function makeFish() {  

    if(!win.info) return;  

    var info = win.info,cannon = info.cannon;  

    var fish = info.fishs.find(isObjInvisible);  

    if(!fish && info.fishs.length < 20) {  

        var index = Math.floor(10 * Math.random())+1;  

        fish = win.dupChild("ui-fish"+index);  

          

    }  

    if(fish) {  

        info.fishs.push(fish);  

        var y = Math.max(0, Math.floor((win.h - info.bottom.h) * Math.random() - fish.h));  

        var x =  y%2 ? -fish.w : win.w;  

        var vx = x > 0 ? -1 : 1;  

        var rotation = x > 0 ? Math.PI : 0;  

        fish.setPosition(x, y).setVisible(true).setEnable(true).setV(vx, 0).setRotation(rotation).play("live");  

    }  

    setTimeout(makeFish, 500);  

}  

//初始化游戏  

win.initGame = function() {  

    var info = {bullets:[], fishs:[], cannonType:1, scoreValue:0};  

    var cannon = this.find("ui-cannon", true);  

    var p = cannon.getPositionInWindow();   

    cannon.center ={x: p.x + (cannon.w >> 1), y: p.y + (cannon.h >> 1)};      

    info.cannon = cannon;  

  

    info.bottom = this.find("ui-image");  

    var totalScore = this.find("ui-total-score", true);  

    p = totalScore.getPositionInWindow();   

    totalScore.center ={x: p.x + (totalScore.w >> 1), y: p.y + (totalScore.h >> 1)};      

    info.totalScore = totalScore;  

      

    var bullet = this.find("ui-bullet").setVisible(false).setEnable(false);  

    bullet.handleOnMoved = handleOnMoved;  

    info.bullets.push(bullet);  

      

    info.coin = this.find("ui-coin").setVisible(false);  

    info.score = this.find("ui-score").setVisible(false);  

    for(var i = 0; i < this.children.length; i++) {  

        var iter = this.children[i];  

        if(iter.name.indexOf("ui-fish") >= 0) {  

            iter.handleOnMoved = handleOnMoved;  

            iter.setEnable(false).setVisible(false);  

            info.fishs.push(iter);  

        }  

    }  

    info.timerID = setTimeout(makeFish, 1000);  

    this.info = info;  

}  

//改变大炮类型  

win.changeCannon = function(delta) {  

    var info = this.info,cannon = info.cannon;  

    info.cannonType = Math.max(1, Math.min(7, info.cannonType + delta));  

    cannon.play("default"+info.cannonType, 100000);  

}  

//炮弹打中鱼时,炮弹变成网,鱼播放die动画,金币从炮弹处移动到总金币处。  

win.onContacted = function(bullet, fish) {  

    var info = this.info,cannon = info.cannon;  

    bullet.setEnable(false).play("web"+info.cannonType, 1, function() {bullet.setVisible(false);});  

    fish.setEnable(false).play("die", 1, function() {fish.setVisible(false)});  

    info.scoreValue += 100;  

    info.totalScore.setValue(info.scoreValue);  

    info.coin.setVisible(true).animate({xStart:bullet.x, yStart:bullet.y, xEnd:info.totalScore.x, yEnd:info.totalScore.y-20});  

}  

//点击场景时,调整大炮位置,发射炮弹,大炮播放射击动画。  

win.handleClick = function(point) {  

    var info = this.info,cannon = info.cannon;  

    if(this.targetShape != this.info.bottom) {  

        var angle = Math.lineAngle(cannon.center, point) - 1.5 * Math.PI;  

        cannon.setRotation(angle);  

        var bullet = info.bullets.find(isObjInvisible);  

        if(!bullet)  {  

            bullet = win.dupChild("ui-bullet",0);  

            info.bullets.push(bullet);  

        }  

        var x = cannon.center.x - (bullet.w >> 1), y = cannon.center.y - (bullet.h >> 1);  

        bullet.setPosition(x, y).setRotation(angle).setVisible(true).setEnable(true).setV(5*Math.sin(angle),-5*Math.cos(angle));  

        bullet.play("bullet"+info.cannonType);  

        cannon.play("fire"+info.cannonType, 1);  

        console.log(angle);  

    }  

}  

  

this.initGame();
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: