大家乐捕鱼游戏里的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();
原文: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();
相关文章推荐
- andriod工程(游戏sdk)onKeyDown函数不响应问题
- 【cocos2d-x IOS游戏开发-捕鱼达人5】C++中函数代理与信号插槽机制
- Flash游戏开发常用函数代
- openssl之EVP系列之13---EVP_Open系列函数介绍
- comments on open source groupware projects
- window.open函数
- window.open函数祥解
- [导入][链接] IBM votes NO on Open XML in ECMA
- comments on several open source groupware
- javascript 中的window.open()函数用法
- 手机游戏软件开发的前景 The future of development of game software on mobiles
- Opensource Project Proposal on JXTA.org: JXTA VoIP
- Delphi中捕获音频函数OnWaveIn的使用小记
- javascript 中的window.open()函数用法
- JS中open()函数介绍!
- How to resolve maximum open cursors exceeded on Oracle(ORA-01000)-- 1
- JOSSO (Java Open Single Sign-On)
- window.open函数
- window.open()函数参数说明
- You do not have permissions to open this file on Excel Services