Download, analyze, and port the game source code across platforms-Article 4 (checkpoints)

Source: Internet
Author: User

Background:

I was going to merge Article 3 and Article 4 together, but I had to separate the previous plan. Generally, when a game involves checkpoints, an array is usually created to store various definition parameters, it is easier to eliminate the star level, and there is no need to create an array. There are only two parameters: level and target. The two parameters also have a functional relationship: target = 1000 * (level + 1) * level/2: You can obtain the target score of the level. For example, if you know the level 3, the target score is 1000*(3 + 1) * 3/2 = 6000; because of this function relationship, you will find that the more difficult the future is, the more strange the author has never reached 10000 points;

Ps:

1. CocosEditor has released a new version and now provides six practical demos, including flappy, popstar, fruitninja, moonwarroris, fruitattack, and testjavascript;

2 code is based on javascript language, cocos2d-x game engine, cocos2d-x editor mobile game development tools completed;

3. CocosEditor must be configured to run the demo. Other tools are not supported. The demo is cross-platform and can be transplanted to android, ios, html5 webpages, etc.


Source code download:

Go to the Code concentration camp to download (the third four merge scores and levels): http://blog.makeapp.co /? P = 319


(Windows, html5, and android)


Windows



Mac Platform


Html5 webpage



Android platform




Code Analysis:


1. Global parameter. js defines the scores of the current level and the current level as follows. If the game does not exit, the two parameter values remain unchanged. You can also use this method to pass values between the two scenarios;

currentLevel = 1;currentLevelScore = 0;


2 MainLayer. in js, The onEnter function is initialized, and the current level and target score are obtained. The target score is the above-mentioned function this.tar getScore = 1000*(1 + currentLevel) * currentLevel/2;

MainLayer.prototype.onEnter = function (){    cc.log("onEnter");    this.pauseNode.setZOrder(120);    //init stars    this.initStarTable();    //stage    this.stageFont.setString(currentLevel + "");    //target  score    this.targetScore = 1000 * (1 + currentLevel) * currentLevel / 2;    this.targetFont.setString(this.targetScore + "");    //score    this.totalScore = currentLevelScore;    this.scoreFont.setString(this.totalScore + "");    //score tip    this.scoreTipLabel.setVisible(false);    this.tipLabel.setVisible(false);    this.tipLabel.setZOrder(10);    //best score    this.bestScore = sys.localStorage.getItem("starBestScore");    if (this.bestScore != null && this.bestScore != undefined) {        this.bestScore = Number(this.bestScore);    }    else {        this.bestScore = 0;    }    this.bestScoreFont.setString(this.bestScore + "");}


3. Check whether the game is successful at the end of the game;

If you win: Add 1 next, currentLevel + = 1; the basic score of the next level is the total score of this level, currentLevelScore = this. totalScore; In MainLayer. in js, I have defined the level genie nextSprite, which will be displayed in 3 seconds. There is also a mobile animation in it; 7 s and then the next MainLayer. js;

If the initialization fails: The levels and scores are cleared and the initialization interface is returned;

MainLayer.prototype.winStar = function (){    if (this.isClear == true) {        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.win);        cc.Toast.create(this.rootNode, "Win", 3);        currentLevel += 1;        currentLevelScore = this.totalScore;        this.nextSprite.setZOrder(100);        var that = this;        this.rootNode.scheduleOnce(function ()        {            that.nextLevelLabel.setString("level " + currentLevel + "");            that.nextTargetLabel.setString("target " + 1000 * (1 + currentLevel) * currentLevel / 2);            that.nextSprite.runAction(cc.Sequence.create(                    cc.MoveTo.create(1, cc.p(0, 0)),                    cc.DelayTime.create(2),                    cc.MoveTo.create(1, cc.p(-730, 0))            ))        }, 3);        this.rootNode.scheduleOnce(function ()        {            cc.BuilderReader.runScene("", "MainLayer");        }, 7);    }    else {        cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.gameover);        currentLevel = 1;        currentLevelScore = 0;        cc.Toast.create(this.rootNode, "lost", 2);        this.rootNode.scheduleOnce(function ()        {            cc.BuilderReader.runScene("", "StartLayer");        }, 2)    }    if (this.totalScore > this.bestScore) {        sys.localStorage.setItem("starBestScore", this.totalScore + "");    }}

It's still that simple;-D



Cocos2d-x cross-platform game engine
Cocos2d-x is a world-renowned game engine, engine in the world has a large number of developers, covering all well-known game developers at home and abroad. At present, Cocos2d-x engine has been achieved across ios, Android, Bada, MeeGo, BlackBerry, Marmalade, Windows, Linux and other platforms. Write once, run everywhere, divided into two versions of cocos2d-c ++ and cocos2d-html5 this article uses the latter; cocos2d-x Official Website: http://cocos2d-x.org/cocos2d-x data download http://cocos2d-x.org/download


CocosEditor development tool:

CocosEditor, it is the development of cross-platform mobile game tools, run Windows/mac system, javascript scripting language, based on cocos2d-x cross-platform game engine, set code editing, Scene Design, animation production, font design, as well as particle, physical system, MAP and so on, and easy debugging, and real-time simulation;

Download CocosEditor, introduction and Tutorial: http://blog.csdn.net/touchsnow/article/details/41070665;

CocosEditor blog: http://blog.makeapp.co /;



PopStar blog series:

Source code download, analysis, and cross-platform migration for PopStar games-Article 1 (UI)

Download, analyze, and port the game source code across platforms-Article 2 (algorithm)

Download, analyze, and port the game source code across platforms-Article 3 (score)

Download, analyze, and port the game source code across platforms-Article 4 (checkpoints)

Download, analyze, and port the game source code across platforms-Article 5 (transplantation)



Portal (premium blog ):

Flappy bird game source code revealing and downloading

Flappy bird game source code exposure and download follow-up-porting to android real machine

Flappy bird game source code exposure and download follow-up-porting to html5 Web Browser

Flappy bird game source code revealing and downloading follow-up-the secret of about $50 thousand daily AdMob ads


Author's note:

For more information, please go to the official blog. The latest blog and code will be launched on the official blog. Please stay tuned for more information. The source code of the cocos2dx editor game will be released soon;

Contact me: zuowen@makeapp.co (Mailbox) QQ group: 232361142


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.