如何用Cocos2d-JS制作一个《你是我的小羊驼》游戏
声明: 发个广告贴~~~这个真的只是个广告哟 · 原文链接:http://edu.9miao.com/group/1/thread/87 同时9秒课堂还有很多优质课程,欢迎大家学习~~~
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------华丽丽的分割线
一夜之间,微信上一款叫《围住神经猫》的小游戏火了。它的玩法很简单,用最少的步数把一只神经兮兮的猫围死。 7月22号上线以来,3天、500万用户和1亿访问,想必各位程序猿都按耐不住了,想实现自己的神经猫游戏。在这篇教程里,我会教大家如何用Cocos2d-JS来实现一个神经猫这样的游戏。让我们先看下游戏最后完成了的效果图: ? 1 2 3 4 $cd cocos2d-js-v3.0-rc1/tools/cocos2d-console/bin $./cocos new -l js --no-native $cd MyJSGame/ $../cocos run -p web 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cc.game.onStart = function(){ // 1. cc.view.adjustViewPort(true);
// 2. if (cc.sys.isMobile) cc.view.setDesignResolutionSize(320,500,cc.ResolutionPolicy.FIXED_WIDTH); else cc.view.setDesignResolutionSize(320,480,cc.ResolutionPolicy.SHOW_ALL); cc.view.resizeWithBrowserSize(true); // 3. cc.LoaderScene.preload(resources,function () { // 4. gameScene = new GameScene(); cc.director.runScene(gameScene); },this); }; cc.game.run(); 21 22 23 24 var GameScene = cc.Scene.extend({ onEnter : function () { this._super(); var bg = new cc.Sprite(res.bg); bg.attr({ anchorX : 0.5, anchorY : 0.5,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> x : cc.winSize.width/2,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> y : cc.winSize.height/2 }); this.addChild(bg); layers.game = new GameLayer(); this.addChild(layers.game); layers.startUI = new StartUI(); this.addChild(layers.startUI); layers.winUI = new ResultUI(true); layers.loseUI = new ResultUI(false); layers.shareUI = new ShareUI(); } 25 var StartUI = cc.Layer.extend({ ctor : function () { var start = new cc.Sprite(res.start); start.x = cc.winSize.width/2; start.y = cc.winSize.height/2 + 20; this.addChild(start); cc.eventManager.addListener({ event: cc.EventListener.TOUCH_ALL_AT_ONCE,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> onTouchesEnded: function (touches,event) { var touch = touches[0]; var pos = touch.getLocation(); if (pos.y < cc.winSize.height/3) { layers.game.initGame(); layers.startUI.removeFromParent(); var ox = x = y = 0,odd = false,block,tex = this.batch.texture; for (var r = 0; r < ROW; r++) { y = BLOCK_YREGION * r; ox = odd * OFFSET_ODD; for (var c = 0; c < COL; c++) { x = ox + BLOCK_XREGION * c; block = new cc.Sprite(tex,BLOCK2_RECT); block.attr({ anchorX : 0,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> anchorY : 0,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> x : x,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> y : y,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> width : BLOCK_W,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> height : BLOCK_H this.batch.addChild(block); odd = !odd; 26 27 28 29 30 31 32 initGame : function() { if (this.inited) return; this.player_c = this.player_r = 4; this.step = 0; for (var i = 0,l = this.active_nodes.length; i < l; i++) { this.active_nodes.removeFromParent(); this.active_nodes = []; this.active_blocks[r][c] = false; this.randomBlocks(); this.player.attr({ x : OFFSET_X + BLOCK_XREGION * this.player_c + BLOCK_W/2,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> y : OFFSET_Y + BLOCK_YREGION * this.player_r - 5 this.player.stopAllActions(); this.player.runAction(this.moving_action); this.inited = true; onTouchesBegan: function (touches,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> var target = event.getCurrentTarget(); if (!target.inited) return; pos.y -= OFFSET_Y; var r = Math.floor(pos.y / BLOCK_YREGION); pos.x -= OFFSET_X + (r%2==1) * OFFSET_ODD; var c = Math.floor(pos.x / BLOCK_XREGION); if (c >= 0 && r >= 0 && c < COL && r < ROW) { if (target.activateBlock(r,c)) { target.step ++; target.movePlayer(); var getDistance = function (r,c,dir_choices,activate_blocs,passed,cost) { passed[r][c] = true; if (r <= 0 || r >= ROW_MINUS_1 || c <= 0 || c >= COL_MINUS_1) { return [r,cost]; var odd = (r % 2 == 1) ? 1 : 0; var choices = dir_choices[odd]; var nextr,nextc,result; nextr = r + choices[0]; nextc = c + choices[1]; if (!activate_blocs[nextr][nextc] && !passed[nextr][nextc]) { cost ++; result = getDistance(nextr,cost); if (result != -1) { result[0] = nextr; result[1] = nextc; return result; return -1; ctor : function (win) { this.win = win; if (win) { this.winPanel = new cc.Sprite(res.succeed); this.winPanel.x = cc.winSize.width/2; this.winPanel.anchorY = 0.2; this.winPanel.y = cc.winSize.height/2; this.addChild(this.winPanel); else { this.losePanel = new cc.Sprite(res.failed); this.losePanel.x = cc.winSize.width/2; this.losePanel.anchorY = 0.2; this.losePanel.y = cc.winSize.height/2; this.addChild(this.losePanel); if (this.win) { this.winPanel.removeAllChildren(); var w = this.winPanel.width,h = this.winPanel.height; var label = new cc.LabelTTF("继续刷屏!n"+step+"步推倒我的小羊驼n打败"+percent+"%朋友圈的人!n你能超过我吗?","宋体",20); label.x = w/2; label.y = h/4; label.textAlign = cc.LabelTTF.TEXT_ALIGNMENT_CENTER; //label.boundingWidth = w; label.width = w; label.color = cc.color(0,0); this.winPanel.addChild(label); this.losePanel.removeAllChildren(); var w = this.losePanel.width,h = this.losePanel.height; label = new cc.LabelTTF("我滴小羊驼呀它又跑掉了nT_T 快帮我抓回来!",97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> label.y = h/4+5; this.losePanel.addChild(label,10); gameScene.addChild(layers.shareUI,100); target.win ? share(1,step,percent) : share(2); target.win ? layers.winUI.removeFromParent() : layers.loseUI.removeFromParent(); ctor: function () { this._super(cc.color(0,188),cc.winSize.width,cc.winSize.height); var arrow = new cc.Sprite(res.arrow); arrow.anchorX = 1; arrow.anchorY = 1; arrow.x = cc.winSize.width - 15; arrow.y = cc.winSize.height - 5; this.addChild(arrow); var label = new cc.LabelTTF("请点击右上角的菜单按钮n再点"分享到朋友圈"n让好友们挑战你的分数!",20,cc.size(cc.winSize.width*0.7,250),cc.TEXT_ALIGNMENT_CENTER); label.x = cc.winSize.width/2; label.y = cc.winSize.height - 100; label.anchorY = 1; this.addChild(label); onEnter: function () { event: cc.EventListener.TOUCH_ONE_BY_ONE,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> onTouchBegan: function (touch,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> layers.shareUI.removeFromParent(); "appid":theData.appId ? theData.appId : '',97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> "img_url":theData.imgUrl,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> "link":theData.link,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> "desc":theData.desc,97); font-family:'microsoft yahei'; font-size:14px; line-height:20px"> "title":theData.title,// 注意这里要分享出去的内容是desc function (resp) { switch (resp.err_msg) { // share_timeline:cancel 用户取消 case 'share_timeline:cancel': callbacks.cancel && callbacks.cancel(resp); break; // share_timeline:fail 发送失败 case 'share_timeline:fail': callbacks.fail && callbacks.fail(resp); // share_timeline:confirm 发送成功 case 'share_timeline:confirm': case 'share_timeline:ok': callbacks.confirm && callbacks.confirm(resp); // 无论成功失败都会执行的回调 callbacks.all && callbacks.all(resp); <font size=""2"">WeixinJSBridge</font> WeixinJSBridge.on('menu:share:timeline',function (argv) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); shareTimeline(newData); // 然后就绪 callbacks.ready && callbacks.ready(argv); } else { // 就绪状态 shareTimeline(data); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |