var PlayLayer = cc.Layer.extend({
bgSprite:null,
scoreLabel:null,
score:0,
timeoutLabel:null,
timeout:2,
SushiSprites:null,
ctor:function () {
// ////////////////////////////
// 1. super init first
this._super();
var size = cc.winSize;
this.SushiSprites = ];
cc.spriteFrameCache.addSpriteFrames(res.sushi_plist);
// add bg
this.bgSprite = new cc.Sprite(res.background_png);
this.bgSprite.attr({
x: size.width / 2,
y: size.height / 2,
// scale: 0.5,
rotation: 180
});
this.addChild(this.bgSprite, 0);
this.scoreLabel = new cc.LabelTTF("分数:0", "微软雅黑", 30);
this.scoreLabel.attr({
x:size.width/2,
y:size.height-20
});
this.addChild(this.scoreLabel, 5);
// timeout 60
this.timeoutLabel = cc.LabelTTF.create("倒计时" + this.timeout, "微软雅黑", 30);
this.timeoutLabel.x = 20;
this.timeoutLabel.y = size.height - 20;
this.addChild(this.timeoutLabel, 5);
// schedule(callback_fn, interval, repeat, delay)
this.schedule(this.update,1,16*1024,1);
this.schedule(this.timer,1,this.timeout,1);
return true;
},
update : function() {
this.addSushi();
this.removeSushi();
},
timer : function() {
if (this.timeout == 0) {
var gameOver = new cc.LayerColor(cc.color(225,225,225,100));
var size = cc.winSize;
var titleLabel = new cc.LabelTTF("游戏结束", "微软雅黑", 38);
titleLabel.attr({
x:size.width/2 ,
y:size.height/2
});
gameOver.addChild(titleLabel, 5);
var TryAgainItem = new cc.MenuItemFont(
"再试一次",
function () {
cc.log("Menu is clicked!");
// var transition= cc.TransitionFade(1, new
// PlayScene(),cc.color(255,255,255,255));
cc.director.runScene(new PlayScene());
}, this);
TryAgainItem.attr({
x: size.width/2,
y: size.height / 2 - 60,
anchorX: 0.5,
anchorY: 0.5
});
TryAgainItem.setColor(255,255,255);
var menu = new cc.Menu(TryAgainItem);
menu.x = 0;
menu.y = 0;
gameOver.addChild(menu, 1);
this.getParent().addChild(gameOver);
this.unschedule(this.update);
this.unschedule(this.timer);
return;
}
this.timeout -=1;
this.timeoutLabel.setString("" + this.timeout);
},
addSushi : function() {
var sushi = new SushiSprite("#sushi_1n.png");
// var sushi = new cc.Sprite("#sushi_1n.png");
var size = cc.winSize;
var x = sushi.width+size.width/2*cc.random0To1();
sushi.attr({
x:x,
y:size.height - 20
});
// sushi.retain();
this.SushiSprites.push(sushi);
sushi.index = this.SushiSprites.length;
this.addChild(sushi,5);
var dorpAction = cc.MoveTo.create(4, cc.p(sushi.x,-30));
sushi.runAction(dorpAction);
},
removeSushiByindex : function(dx) {
if(isNaN(dx)||dx>this.SushiSprites.length){return false;}
for(var i=0,n=0;i<this.length;i++)
{
if(this.SushiSprites*!=this)
{
this.SushiSprites=this.SushiSprites*
}
}
this.SushiSprites.length-=1
},
removeSushi : function() {
for (var i = 0; i < this.SushiSprites.length; i++) {
cc.log("removeSushi.........");
if(-30 == this.SushiSprites*.y) {
this.SushiSprites*.removeFromParent();
this.SushiSprites* = undefined;
this.SushiSprites.splice(i,1);
i= i-1;
}
}
},
addScore:function(){
this.score +=1;
this.scoreLabel.setString("分数:" + this.score);
}
});
var PlayScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new PlayLayer();
this.addChild(layer);
}
});