cocos2d-js 翻版神棍傳說demo

翻版cocos商店裏的神棍傳說小遊戲---作爲練手的小遊戲還是不錯的

var PublicData = {//配置數據
	pillarScaleX : 10,//木樁橫向放大比例
	lineIncrease : 0.01,//橋樑增長比例
	
	timeMoveMonkeyLeft : 1,//主角移動到最左邊使用時間
	timeMoveMonkeyNext : 1//主角移動到下一個木樁使用時間
}

var GameStartMenuLayer = cc.Layer.extend({
	curPillar : null,//當前木樁
	nextPillar : null,//下一個木裝
	menu : null,//開始菜單
	lable : null,//遊戲標題
	line : null,//橋樑精靈
	monkey : null,//主角精靈
	
	originMonkey : null,//主角初始屬性
	origincurPillar : null,//主角站立木樁的屬性
	originnextPillar : null,//下一個木樁的屬性
	originline : null,//橋樑初始屬性
	
	layerGameOver : null,//遊戲結束界面容器
	isGameOver : false,//標記遊戲是否已經結束
	
	score : null,
	
	ctor : function(){
		this._super();
		
		var size = cc.winSize;
		var self = this;
		
		layer_bg = new cc.Sprite(res.Background2_png);
		layer_bg.attr({
			anchorX : 0,
			anchorY :0
		});
		
		this.addChild(layer_bg);
		
		var but_start = new cc.MenuItemImage(
			res.Start_normal_png,
			res.Start_select_png,
			function(){
				self.removeChild(self.menu);
				self.removeChild(self.label);
				
				self.curPillar.runAction(cc.moveTo(1,cc.p(self.curPillar.width * 10/2,0)));
				this.monkey.runAction(cc.moveTo(PublicData.timeMoveMonkeyLeft,cc.p(this.monkey.width / 2,this.originMonkey.y)));
				self.crateNextPillar(self);
				
				self.addScoreItem();
			},
			this);
			
		but_start.attr({
			x : size.width / 2,
			y : size.height / 2,
            anchorX: 0.5,
            anchorY: 0.5,
            scale : 3,
            opacity : 0
		});
		
		cc.eventManager.addListener({
        	event:cc.EventListener.TOUCH_ONE_BY_ONE,
        	swallowTouches: true,
        	onTouchBegan:  function(touch, event){
        		if(self.isGameOver){
        			return false;
        		}
        		
	        	self.scheduleUpdate();
	        	return true;
	    	},
	    	onTouchEnded : function(touch, event){
	            self.unscheduleAllCallbacks();
	            
	            self.line.runAction(cc.sequence(cc.rotateTo(0.5,90),cc.callFunc(self.moveMonkey,self,self)));
	    	}
        },this);
		
		
		this.menu = new cc.Menu(but_start);
		this.menu.x = 0;
		this.menu.y = 0;
		
		this.addChild(this.menu,1);
		but_start.runAction(
			cc.spawn(
				cc.fadeIn(1),
				cc.scaleTo(1,1)
				)
			);
			
		this.label = cc.LabelTTF.create("神棍傳奇","Aria",64);
		this.label.x = size.width / 2;
		this.label.y = size.height / 3 * 2;
		this.label.color = cc.color(0,0,0);
		this.label.runAction(
			cc.jumpTo(0.6, cc.p(size.width / 2, size.height / 4 * 3) , 50 , 4)
		);
		
		this.addChild(this.label,1);
		
		this.init();
	},
	
	/*添加分數提示*/
	addScoreItem : function(){
		var layer = new cc.LayerColor(cc.color(124, 118, 118,200),150,100);
		layer.attr({
			x : cc.winSize.width / 2 - 75,
			y : cc.winSize.height - 200,
		});
		
		this.addChild(layer);
		
		this.score = cc.LabelTTF.create("0","Aria",32);
		this.score.color = cc.color(255,255,255);
		this.score.attr({
			x : layer.width / 2,
			y : layer.height / 2
		});
		layer.addChild(this.score);
	},
	
	addScore : function(){
		this.score.string = parseInt(this.score.string) + 1;
		this.score.runAction(cc.sequence(cc.scaleTo(0.1,2),cc.scaleTo(0.1,1)));
	},
	
	/*初始化操作*/
	init : function(){
		var size = cc.winSize;
		
		isGameOver = false;
		//遊戲主頁面精靈添加begin=================================
		this.curPillar = cc.Sprite.create(res.Stick_black_png);//主角站立的木樁
		this.origincurPillar = {
			x : size.width / 2,
			anchorY : 0,
			y : 0,
			scaleX : PublicData.pillarScaleX
		}
		this.curPillar.attr(this.origincurPillar);
		this.addChild(this.curPillar,2);
	
		this.line = cc.Sprite.create(res.Stick_black_png);//橋樑
		this.originline = {
			x : this.curPillar.width * PublicData.pillarScaleX,
			y : this.curPillar.height,
			anchorX : 1,
			anchorY : 0,
			scaleY : 0
		}
		this.line.attr(this.originline);
		this.addChild(this.line,2);
		
		this.monkey = new cc.Sprite("#z0001.png");
		this.originMonkey = {
			x : size.width / 2,
			y : this.curPillar.height,
			anchorY : 0
		}
		this.monkey.attr(this.originMonkey);
		this.addChild(this.monkey,3);
	},
	
	initBegin : function(){
		
	},
	
	/*創建下一個木樁*/
	crateNextPillar : function(self){
		var size = cc.director.getWinSize();
		
		self.nextPillar = cc.Sprite.create(res.Stick_black_png);
		
		var newX = Math.floor(Math.random() * (size.width - this.nextPillar.width * PublicData.pillarScaleX * 2)) ;
		newX = newX + this.nextPillar.width * PublicData.pillarScaleX / 2 * 3;
		self.nextPillar.x = size.width + newX ;
		self.nextPillar.anchorY = 0;
		self.nextPillar.y = 0;
		self.nextPillar.scaleX = PublicData.pillarScaleX;
		self.addChild(self.nextPillar,2);
		
		self.nextPillar.runAction(cc.moveTo(1,cc.p(newX, 0)));
	},
	
	update : function(dt){
		this.line.scaleY += PublicData.lineIncrease;
	},
	
	/*移動精靈至下一個木樁*/
	moveMonkey : function(self,data){//回調函數-data指向傳遞的數據,這裏使用this指示GameStartMenuLayer對象
		var move = cc.moveTo(0.5,cc.p(self.scaleY * self.height + data.monkey.width ,data.monkey.y));
		var callback = cc.callFunc(data.moveToInit,this);
		data.monkey.runAction(cc.sequence(move,callback));
	},
	
	moveToInit : function(){
		if(this.nextPillar.x - this.curPillar.x < this.line.height * this.line.scaleY
			||this.nextPillar.x - this.curPillar.x - this.nextPillar.width * PublicData.pillarScaleX > this.line.height * this.line.scaleY){
			this.monkey.rotation = 90;
			this.monkey.runAction(cc.moveBy(0.5,cc.p(0,-this.curPillar.height)));
			this.showGameOver();
			return ;
		}
			
		this.addScore();
		
		this.removeChild(this.curPillar);
		this.curPillar = this.nextPillar;
		this.curPillar.runAction(cc.moveTo(1,cc.p(this.curPillar.width * 10/2,0)));
		this.monkey.runAction(cc.moveTo(1,cc.p(this.monkey.width / 2,this.originMonkey.y)));
		this.crateNextPillar(this);
		
		this.line.scaleY = 0;
		this.line.rotation = 0;
	},
	
	/*顯示遊戲結束畫面*/
	showGameOver : function(){
		this.isGameOver = true;
		
		var size  = cc.director.getWinSize();
		this.layerGameOver = cc.LayerColor.create(cc.color(255,255,255,200));
		this.addChild(this.layerGameOver,10);
		
		var label = cc.LabelTTF.create("再來一次","Aria",64);
		label.color = cc.color(0,0,0);
		
		var mlabel = new cc.MenuItemLabel(label,function(){
			cc.director.pushScene(new GameStartMenuScene());
		});
		
		mlabel.x = size.width / 2;
		mlabel.y = size.height / 3 * 2;
		
		var draw = new cc.DrawNode();
		draw.drawRect(cc.p(0,0),cc.p(label.width,label.height),cc.color(173,90,92));
		label.addChild(draw,-1);
		
		var menuAgain = new cc.Menu(mlabel);
		menuAgain.x = 0;
		menuAgain.y = 0;
		this.layerGameOver.addChild(menuAgain);
		
	}
});

var GameStartMenuScene = cc.Scene.extend({
	onEnter: function(){
		this._super();
		
		var layer = new GameStartMenuLayer();
		this.addChild(layer);
	}
});

附贈效果圖
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章