【Cocos2d-js之旅】1-贪吃蛇

【Cocos2d-js之旅】1-贪吃蛇,第1张

概述这两天前辈要我用cocos2d-js做一个贪吃蛇,记录一下代码 joyStick.js var joyStick = cc.Layer.extend({ctor:function() { this._super(); this.initGame(); //add 4 sprite this.addChild(JoyStickBg); this.addChild(joyS

这两天前辈要我用cocos2d-Js做一个贪吃蛇,记录一下代码

joyStick.Js

var joyStick = cc.Layer.extend({ctor:function()	{		this._super();		this.initGame();		//add 4 sprite		this.addChild(JoyStickBg);		this.addChild(joyStickPt);		this.addChild(hellolabel);		this.addChild(score_label);				GetDirection=function(a,b)		{			var pt = cc.p(cc.pSub(a,b));			if(Math.abs(pt.x)<0.000001&&Math.abs(pt.y)<0.000001)  //judge if equal				return cc.p(0,0);			return cc.pnormalize(cc.pSub(a,b));		},return true;	},update:function(){				var newPos = cc.pAdd(snake_List[0][0].getposition(),cc.pMult(snake_List[0][1],movespeed));		//judge if touch the wall		if(newPos.x<0)		{			this.SetGameState(2);			newPos.x = 0;		}		if(newPos.x>winSize.wIDth)		{			this.SetGameState(2);			newPos.x= winSize.wIDth;		}		if(newPos.y<0)		{			this.SetGameState(2);			newPos.y = 0;		}		if(newPos.y>winSize.height)		{			this.SetGameState(2);			newPos.y = winSize.height;		}		snake_List[0][0].setposition(newPos);  //set position of snake head		//set position and direction of child snake node		for (var i = snake_List.length - 1; i >= 1; i--) {			snake_List[i][1] = snake_List[i][2];			snake_List[i][2] = snake_List[i-1][1];			var dPos = cc.pAdd(snake_List[i-1][0].getposition(),cc.pMult(snake_List[i-1][1],-movespeed));			//var dPos = cc.pAdd(snake_List[i][0].getposition(),cc.pMult(snake_List[i][1],movespeed));			snake_List[i][0].setposition(dPos);		}		//judge if eat food		if(cc.pdistance(newPos,cur_food.getposition())<cur_food._contentSize.wIDth)		{			this.AddNewFood();			this.AddSnakeNode(5);		}		//check collision		for (var i = snake_List.length - 1; i >= 2; i--) {			if(cc.pdistance(snake_List[i][0].getposition(),newPos)<5)			{				this.SetGameState(2);			}		}	},SetGameState:function(flag)		//set game state flag=1 for win while flag =2 for fail	{		if(flag == 1)		{			cc.log("set win");			//set win		}		else		{			cc.log("set fail");			//remove 4 sprite for next use			this.removeChild(joyStickPt);			this.removeChild(JoyStickBg);			this.removeChild(hellolabel);			this.removeChild(score_label);			this.unscheduleUpdate();			var endGameScene = new endGame();			cc.director.runScene(endGameScene);						//set fail		}	},AddNewFood :function()		//add a new food on the map	{		if (cur_food==null)		{			cur_food = new cc.Sprite("res/pt.png");			this.addChild(cur_food);		}		var rd_spawn = cc.p(cc.random0To1()*(winSize.wIDth-cur_food._contentSize.wIDth)+cur_food._contentSize.wIDth/2,cc.random0To1()*(winSize.height-cur_food._contentSize.wIDth/2)+cur_food._contentSize.wIDth/2);		cur_food.setcolor(cc.color(255,255,0));		cur_food.setposition(rd_spawn);			},AddSnakeNode:function(sNum)	//according to the giving sNum,add snake length and score	{		snake_length += sNum;		var spritename = "";		database.score+=sNum;		score_label.string = database.score		if( snake_length>1)		{			spritename = "res/body.png";		}		else		{			spritename = "res/pt.png";		}		for (var i=0;i<sNum;i++)		{			var sLength = snake_List.length-1;			var newNode = [				sprite = new cc.Sprite(spritename),dir = snake_List[sLength][1],nextdir = snake_List[sLength][1]							];			snake_List.push(newNode);			var sLength = snake_List.length-1;			this.addChild(snake_List[sLength][0]);			var newPos = cc.pAdd(snake_List[sLength-1][0].getposition(),cc.pMult(snake_List[sLength-1][1],-snake_List[0][0]._contentSize.wIDth-5));			snake_List[sLength][0].setposition(newPos);		}			},initGame:function()	{		this.scheduleUpdate();		bIsActived = false;		radius = 48;		snake_length = 1;		curDirection = cc.p(1,0);		game_state = 0;		movespeed = 5;		snake_List = [];		snake_head_pos = cc.p(200,200);		database.score = 0;		var snake_head = [			sprite = new cc.Sprite("res/pt.png"),dir = cc.p(1,0),nextdir = null		];		snake_List.push(snake_head);		cur_food =null;		score_label.string = database.score;		curposition = cc.p(JoyStickBg.x,JoyStickBg.y);		centerPostion = cc.p(JoyStickBg.x,JoyStickBg.y);		this.addChild(snake_List[0][0]);		snake_List[0][0].setposition(snake_head_pos);		joyStickPt.x = 58;		joyStickPt.y = 58;		cc.eventManager.addListener(Listenr_touch,this);		this.AddNewFood();		this.AddSnakeNode(100);	}});bIsActived = false;radius = 48;snake_length = 1;curDirection = cc.p(1,0);game_state = 0;movespeed = 5;snake_List = [];snake_head_pos = cc.p(200,200);cur_food =null;Listenr_touch = cc.EventListener.create({	event:cc.EventListener.touch_ONE_BY_ONE,swallowtouches:true,ontouchBegan:function(touch,event)	{		var target = event.getCurrentTarget();		var locationInNode = target.convertToNodeSpace(touch.getLocation());    		if(cc.pdistance(centerPostion,locationInNode)<radius)		{			bIsActived = true;		}		return true;	},ontouchmoved:function(touch,event)	{		if (bIsActived) {			var target = event.getCurrentTarget();			var locationInNode = target.convertToNodeSpace(touch.getLocation());   			var delta = touch.getDelta();			curposition = cc.p(locationInNode.x+delta.x,locationInNode.y+delta.y);						if(cc.pdistance(centerPostion,curposition)>radius)			{				curposition = cc.pAdd(centerPostion,cc.pMult(GetDirection(locationInNode,centerPostion),radius));			}			joyStickPt.setposition(curposition);			curDirection = GetDirection(curposition,centerPostion);			if((snake_List[0][1].x*curDirection.x+snake_List[0][1].y*curDirection.y)>=0.00001)			{				snake_List[0][1] = GetDirection(curposition,centerPostion);			}		}		},ontouchended:function(touch,event)	{		bIsActived = false;		curposition = centerPostion;		joyStickPt.setposition(curposition);	}});addFlag = 0;JoyStickBg = new cc.Sprite("res/bg.png");JoyStickBg.x = 58;JoyStickBg.y = 58;JoyStickBg.opacity = 100;joyStickPt = new cc.Sprite("res/pt.png");joyStickPt.x = 58;joyStickPt.y = 58;var hellolabel = new cc.LabelTTF("score:","",20);hellolabel.x = 300;hellolabel.y = 50;score_label = new cc.LabelTTF("0",38);score_label.x = 400;score_label.y = 50;curposition = cc.p(JoyStickBg.x,JoyStickBg.y);centerPostion = cc.p(JoyStickBg.x,JoyStickBg.y);winSize = cc.winSize;


其中分数是采用一个在main.Js中声明的全局数组记录的

startScene.Js

var startLayer = cc.Layer.extend(	{ctor:function()		{			this._super();			var size = cc.winSize;			var hellolabel = new cc.LabelTTF("hello Word",38);			hellolabel.x = size.wIDth/2;			hellolabel.y = size.height/2;			this.addChild(hellolabel);			return true;		}});var startScene = cc.Scene.extend(	{		onEnter:function(){			this._super();			//var newLayer = new startLayer();			var newLayer = ccs.load("res/startGame.Json").node;			var btn_start = ccui.helper.seekWidgetByname(newLayer,"button_5");			btn_start.addtouchEventListener(				function (sender,type)				{					if(type==ccui.Widget.touch_ENDED)					{						cc.director.runScene(new gameScene());					}				}				);			this.addChild(newLayer);		}	});


gameScene.Js


var gameLayer = cc.Layer.extend(	{ctor:function()		{			this._super();			var joyStick = new joyStick();			this.addChild(joyStick);			return true;		}});var gameScene = cc.Scene.extend(	{		onEnter:function(){			this._super();			var newLayer = new joyStick();			this.addChild(newLayer);		}	});


endGame.Js
var endGame = cc.Scene.extend({ctor:function()	{		this._super();		var endGameLayer = ccs.load("res/endGame.Json").node;		var label_score = ccui.helper.seekWidgetByname(endGameLayer,"label_score");		var btn_restart = ccui.helper.seekWidgetByname(endGameLayer,"button_5");		btn_restart.addtouchEventListener(			function (sender,type)			{				if(type==ccui.Widget.touch_ENDED)				{										this.removeFromParent();					var newGame = new cc.Scene();					newGame.addChild(new joyStick());					cc.director.runScene(newGame);				}			}		);		this.addChild(endGameLayer);		label_score.string = database.score;		return true;	}});
总结

以上是内存溢出为你收集整理的【Cocos2d-js之旅】1-贪吃蛇全部内容,希望文章能够帮你解决【Cocos2d-js之旅】1-贪吃蛇所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1082331.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存