同一个scene第一次运行,无论跑多久都没问题。第二次运行一段时间一定会强制停止。而且没有打印错误信息。该类代码如下:
var NewGameLayer = cc.Layer.extend({
gameManager:
null,
space:
null,
fish_num:20,
step:0,
current_level:1,
pengZhuangShapes:],
id:0,
all_score:0,
isWin:0,*//1;win,-1:faile,0:defalt
** *ctor:
function (level) {
this._super();
this.id=FishId++;
var size = cc.winSize;
this.gameManager= GameManager.getInstance();
this.space =
new cp.Space();
*// 2. setup the Gravity
this.space.gravity = cp.v(0, 0);
this.space.sleepTimeThreshold = 0.5;
this.space.collisionSlop = 0.5;
var _debugNode =
new cc.PhysicsDebugNode(
this.space);
*// Parallax ratio and offset
this.addChild(_debugNode,3);
var user =
new MyFish(
this.space);
*// this.addChild(user);
this.gameManager.setUserFish(user);
this.gameManager.init();
this.space.addCollisionHandler(1, 1,
this.collisionCoinBegin.bind(
this),
null,
null,
null);
this.space.addCollisionHandler(2, 1,
this.collisionCoinBegin2.bind(
this),
null,
null,
null);
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches:
true,
onTouchBegan:
this.onTouchBegan,
onTouchMoved:
this.onTouchMoved,
onTouchEnded:
this.onTouchEnded
},
this);
cc.log(
“game id:d%”,
this.id);
this.scheduleUpdate();
return true;
},
onEnter:
function () {
this._super();
cc.log(
“fish size:d%”,
this.gameManager.getFishes().length);
for(
var i =0;i<20;i++){
this.addPcFish();
}
},
GameWin:
function(){
*// var map1 = ccs.load(res.game_win);
**// this.addChild(map1.node);
**// var button = ccui.helper.seekWidgetByName(map1.node, “win_back”);
**// button.addTouchEventListener(this.nextGame, this);
**// var button2 = ccui.helper.seekWidgetByName(map1.node, “win_next”);
**// button2.addTouchEventListener(this.nextGame, this);
this.clearFishes();
var scene =
new ListScene();
var transition =
new cc.TransitionFade(0.5,scene);
cc.director.runScene(transition);
},
GamneOver:
function(){
var map1 = ccs.load(res.game_over);
this.addChild(
this.map1.node);
var button = ccui.helper.seekWidgetByName(map1.node,
“over_back”);
button.addTouchEventListener(nextGame,
this);
var button2 = ccui.helper.seekWidgetByName(map1.node,
“over_restart”);
button2.addTouchEventListener(
this.nextGame,
this);
},
nextGame:
function(sender, type){
cc.log(
“button:s%”,sender.getName());
var name = sender.getName();
switch (type) {
case ccui.Widget.TOUCH_BEGAN:
cc.log(
“Touch Down”);
break;
case ccui.Widget.TOUCH_ENDED:
cc.log(
“Touch end”);
this.clearFishes();
if(name ==
“win_back” || name ==
“over_back”){
var scene =
new MainScene();
var transition =
new cc.TransitionFade(0.5,scene);
cc.director.runScene(transition);
}
else if(name ==
“win_next”){
var scene =
new ListScene();
*// scene.startLvel(this.current_level ++,0);
var transition =
new cc.TransitionFade(0.5,scene);
cc.director.runScene(transition);
}
else if(name ==
“over_restart”){
var scene =
new ListScene();
*// scene.startLvel(this.current_level ++,0);
var transition =
new cc.TransitionFade(0.5,scene);
cc.director.runScene(transition);
}
break;
default:
break;
}
},
clearFishes:
function(){
cc.log(
“1111111fish size:d%”,
this.gameManager.getFishes().length);
for(
var i = 0; i <
this.gameManager.getFishes().length; i++) {
var obj =
this.gameManager.getFishes();
cc.log(
“clear life:d%,d%”,i,obj.getLife());
obj.setLife(Life.died);
obj.destroy(
this.space);
}
cc.log(
“22222222fish size:d%”,
this.gameManager.getFishes().length);
this.gameManager.getUserFish().destroy(
this.space);
this.gameManager.clear();
cc.log(
“3333333333fish size:d%”,
this.gameManager.getFishes().length);
},
addPcFish:
function(){
var user =
new PCFish(
this.space,1);
*// this.addChild(user);
this.gameManager.getFishes().push(user);
},
update:
function(dt){
if(
this.gameManager.getWinType() != 0){
return;
}
this.step++;
this.space.step(dt);
this.gameManager.getUserFish().update(dt);
for(
var i = 0; i <
this.gameManager.getFishes().length; i++) {
var obj =
this.gameManager.getFishes()*;
for*(
var j=0;j<
this.pengZhuangShapes.length;j++){
if(obj.getShapes() ==
this.pengZhuangShapes){
cc.log(
“die11111111”);
if(obj.getFishType()<
this.gameManager.getUserFish().getFishType()){
obj.setLife(Life.died);
this.all_score += obj.getScore();
cc.log(
“die:d%”,
this.all_score);
*// obj.addTexiao();
**// this.user_fish.eatFish(this.all_score);
** *}
else if(obj.getFishType()>
this.gameManager.getUserFish().getFishType() ){
*// this.gameManager.getUserFish().downLife(this);
**// obj.eatFish();
** *}
}
}
if(obj.getLife()==Life.died){
}
else{
*// obj.update(dt);
** *}
}
for(
var i = 0; i <
this.gameManager.getFishes().length; i++) {
var obj =
this.gameManager.getFishes()*;
if*(obj.getLife()==Life.died){
obj.destroy(
this.space);
this.gameManager.getFishes().splice(i, 1);
}
}
for(
var i = 0; i <
this.gameManager.getFishes().length; i++) {
var obj =
this.gameManager.getFishes()*;
if*(obj.getLife()!=Life.died){
obj.update(dt);
}
}
if(
this.step%5 == 0){
if(
this.gameManager.getFishes().length <
this.fish_num){
this.addPcFish();
}
}
if(
this.all_score >500){
this.gameManager.setWinType(1);
this.GameWin();
}
},
collisionCoinBegin:
function (arbiter, space) {
var shapes = arbiter.getShapes();
var a = arbiter.a.collision_type;
arbiter.ignore();
},
collisionCoinBegin2:
function (arbiter, space) {
arbiter.ignore();
var shapes = arbiter.getShapes();
var a = arbiter.a.collision_type;
var b = arbiter.b.collision_type;
cc.log(
“collisionCoinBegin2”);
if(a == 1){
this.pengZhuangShapes.push(arbiter.a);
}
else if(b == 1){
this.pengZhuangShapes.push(arbiter.b);
}
},
pre_pos:
null,
onTouchBegan:
function(touch, event) {
pre_pos = touch.getLocation();
return true;
},
onTouchMoved:
function(touch, event) {
if( GameManager.getInstance().getWinType() == 0){
*// var game = cc.director.getRunningScene().getChildByTag(1);
**// if(game.all_score < game.level_json"user_fish"]“fish_body”]“eat_score”]){
var pos = touch.getLocation();
var tem = cc.p(pos.x-pre_pos.x,pos.y-pre_pos.y);
*// game.user_fish.updatePos(tem);
** *GameManager.getInstance().getUserFish().updatePos(tem);
pre_pos = pos;
*// }
** *}
},
onTouchEnded:
function(touch, event) {
},
onExit:
function () {
this._super();
cc.log(
“onExit”);
}
});
var NewGameScane = cc.Scene.extend({
onEnter:
function () {
this._super();
cc.log(
“onEnter:”);
var layer =
new NewGameLayer(1);
this.addChild(layer,1,1);
},
});
有没有人能给个分析啊。到底什么原因。郁闷死了。