温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)

发布时间:2020-03-03 16:17:52 来源:网络 阅读:552 作者:makeapp628 栏目:移动开发

背景:

  本来打算把第三篇和第四篇合并都一起,但以前计划分开,就还是分来吧;一般的游戏涉及到关卡的话,一般都会建立一个数组来存放各种定义参数,消灭星星关卡比较容易,不需要建立数组,只有两个参数level和target,而且这两个参数还存在函数关系:target=1000*(level+1)*level/2,只要知道第几关就可以得到该关的目标分数,比如第三关,目标分数就是 1000*(3+1)*3/2=6000;  因为这样的函数关系,你会发现越往后越难过关,怪不得笔者一直达不到10000分;

ps:

1 CocosEditor已发布新版本,现在提供6个实战demo学习,包括flappy ,popstar ,fruitninja,moonwarroris,fruitattack,testjavascript;

2 代码是基于javascript语言,cocos2d-x游戏引擎,cocos2d-x editor手游开发工具完成的;

3 运行demo需要配置好CocosEditor,暂不支持其他工具。demo是跨平台的,可移植运行android,ios,html5网页等。



源代码下载:

请到代码集中营下载(第三四篇合并  分数和关卡):http://blog.makeapp.co/?p=319



不同平台下的效果图:(windows、html5、android)


windows

PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)



mac平台

PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)


html5网页

PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)


android平台


PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)


代码分析:


1 全局参数,在主函数Main.js 如下定义当前关卡和当前关卡得到的分数;如果游戏没有退出,两个参数值一直保持不变,也可以通过这样的方法在两个场景之间传递值;


[javascript]view plaincopyPopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)
  1. currentLevel = 1;  

  2. currentLevelScore = 0;  




2 MainLayer.js里面onEnter函数初始化,当前关卡和目标分数,获得的总分;目标分数就是上面说的函数 this.targetScore = 1000 * (1 + currentLevel) * currentLevel / 2;


[javascript]view plaincopyPopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)
  1. MainLayer.prototype.onEnter = function ()  

  2. {  

  3.    cc.log("onEnter");  

  4. this.pauseNode.setZOrder(120);  

  5. //init stars

  6. this.initStarTable();  

  7. //stage

  8. this.stageFont.setString(currentLevel + "");  

  9. //target  score

  10. this.targetScore = 1000 * (1 + currentLevel) * currentLevel / 2;  

  11. this.targetFont.setString(this.targetScore + "");  

  12. //score

  13. this.totalScore = currentLevelScore;  

  14. this.scoreFont.setString(this.totalScore + "");  

  15. //score tip

  16. this.scoreTipLabel.setVisible(false);  

  17. this.tipLabel.setVisible(false);  

  18. this.tipLabel.setZOrder(10);  

  19. //best score

  20. this.bestScore = sys.localStorage.getItem("starBestScore");  

  21. if (this.bestScore != null && this.bestScore != undefined) {  

  22. this.bestScore = Number(this.bestScore);  

  23.    }  

  24. else {  

  25. this.bestScore = 0;  

  26.    }  

  27. this.bestScoreFont.setString(this.bestScore + "");  

  28. }  




3 游戏结束时,检测是否胜利;

 如果胜利:下一个加1,currentLevel += 1; 下一关基础分数是这关的总分,currentLevelScore = this.totalScore;  在MainLayer.js里面,笔者已经定义过关卡精灵nextSprite,3秒后让它显示,里面还有一个移动动画;7s后重新进入下一关MainLayer.js;

如果失败:关卡和分数都清空初始化,回到开始界面;


[javascript]view plaincopyPopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)PopStar(消灭星星)游戏源代码下载、分析及跨平台移植---第四篇(关卡)
  1. MainLayer.prototype.winStar = function ()  

  2. {  

  3. if (this.isClear == true) {  

  4.        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.win);  

  5.        cc.Toast.create(this.rootNode, "Win", 3);  

  6.        currentLevel += 1;  

  7.        currentLevelScore = this.totalScore;  

  8. this.nextSprite.setZOrder(100);  

  9. var that = this;  

  10. this.rootNode.scheduleOnce(function ()  

  11.        {  

  12.            that.nextLevelLabel.setString("level " + currentLevel + "");  

  13.            that.nextTargetLabel.setString("target " + 1000 * (1 + currentLevel) * currentLevel / 2);  

  14.            that.nextSprite.runAction(cc.Sequence.create(  

  15.                    cc.MoveTo.create(1, cc.p(0, 0)),  

  16.                    cc.DelayTime.create(2),  

  17.                    cc.MoveTo.create(1, cc.p(-730, 0))  

  18.            ))  

  19.        }, 3);  

  20. this.rootNode.scheduleOnce(function ()  

  21.        {  

  22.            cc.BuilderReader.runScene("", "MainLayer");  

  23.        }, 7);  

  24.    }  

  25. else {  

  26.        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.gameover);  

  27.        currentLevel = 1;  

  28.        currentLevelScore = 0;  

  29.        cc.Toast.create(this.rootNode, "lost", 2);  

  30. this.rootNode.scheduleOnce(function ()  

  31.        {  

  32.            cc.BuilderReader.runScene("", "StartLayer");  

  33.        }, 2)  

  34.    }  

  35. if (this.totalScore > this.bestScore) {  

  36.        sys.localStorage.setItem("starBestScore", this.totalScore + "");  

  37.    }  

  38. }  


就这些,还是这么简单;:-D



cocos2d-x跨平台游戏引擎

cocos2d-x是全球知名的游戏引擎 ,引擎在全球范围内拥有众多开发者,涵盖国内外各知名游戏开发商。目前Cocos2d-x引擎已经实现横跨ios、Android、Bada、MeeGo、BlackBerry、Marmalade、Windows、Linux等平台。编写一次,到处运行,分为两个版本 cocos2d-c++和cocos2d-html5 本文使用了后者;
cocos2d-x 官网:http://cocos2d-x.org/
cocos2d-x 资料下载  http://cocos2d-x.org/download




CocosEditor开发工具:

CocosEditor,它是开发跨平台的手机游戏工具,运行window/mac系统上,javascript脚本语言,基于cocos2d-x跨平台游戏引擎, 集合代码编辑,场景设计,动画制作,字体设计,还有粒子,物理系统,地图等等的,而且调试方便,和实时模拟;

CocosEditor 下载,介绍和教程:http://blog.csdn.net/touchsnow/article/details/19070665;

CocosEditor官方博客:http://blog.makeapp.co/;


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI