Fruit Ninja (Fruit Ninja) game source code download and Analysis (medium) --- run Android, Ios, Window, Mac, Html5 platforms

Source: Internet
Author: User

Background:

In the previous article, the effects and the starting interface of continuously throwing fruit are realized. This article analyzes how to cut fruit and get scores. To run the demo, you must configure CocosEditor. Other tools are not supported currently. Demo is cross-platform, can be transplanted to run android, ios, html5 web pages, code is based on javascript language, cocos2d-x game engine, CocosEditor mobile game development tools to complete.


Source code download:

Please go to code concentration camp download (Fruit Ninja cocos2d-JS source code): http://blog.makeapp.co /? P = 319


For different platforms:


Windows




Html5 webpage



Android platform






Code Analysis:


1. Create a static fruit array. Each fruit has four parameters: ID, score, image, and removed fragment. The parameters are defined here and are directly used below;

FRUIT_DATA = [    {        num: 0,        score: 1,        fruitImage: "apple.png",        cutImage: ["apple-1.png", "apple-2.png"]    },    {        num: 1,        score: 2,        fruitImage: "banana.png",        cutImage: ["banana-1.png", "banana-2.png"]    },    {        num: 2,        score: 2,        fruitImage: "basaha.png",        cutImage: ["basaha-1.png", "basaha-2.png"]    },    {        num: 3,        score: 3,        fruitImage: "peach.png",        cutImage: ["peach-1.png", "peach-2.png"]    },    {        num: 4,        score: 5,        fruitImage: "sandia.png",        cutImage: ["sandia-1.png", "sandia-2.png"]    },    {        num: 5,        score: 0,        fruitImage: "boom.png",        cutImage: "boomlight1.png"    }];



2. Check collision events;

# The Touch has onTouchBegan and onTouchMoved, which are two points. You can determine the direction of the blade, as mentioned in the previous section. var flash = cc. mySprite. createFlash (this. rootNode, "flash.png", this. pBegin, loc );

# First check the collision. cc. rectContainsPoint (fruit. getBoundingBox (), loc)

# PlayEffect;

# Creating flash

# The fruit disappears and is cut into two halves. fruit slice

# Scoretip


MainLayer.prototype.onTouchesMoved = function (touches, event){    if (this.gameStatus == OVER) {        return;    }    var loc = touches[0].getLocation();    for (var i = 0; i < this.fruitList.length; i++) {        var fruit = this.fruitList[i];        if (fruit && fruit.cleanuped == false) {            if (cc.rectContainsPoint(fruit.getBoundingBox(), loc)) {                //splatter audio                cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.splatter, false);                //flash                var flash = cc.MySprite.createFlash(this.rootNode, "flash.png", this.pBegin, loc);                //Fruit Slice                this.newSliceFruit(fruit.num, fruit.getPosition(), flash.getRotation());                fruit.runAction(cc.ScaleTo.create(0, 0));                fruit.cleanuped = true;                //scoreTip                var fruitScore = FRUIT_DATA[fruit.num].score;                this.scoreTip(fruit.getPosition(), fruitScore + "");                this.totalScore += Number(fruitScore);                this.totalScoreFont.setString(this.totalScore);            }        }        else {            this.fruitList.splice(i, 1);        }    }};


3. Cut fruit into half

# Three parameters: fruit number num, removed position fruitPosition, and rotation angle rotation;

# Retrieve two fruit fragments from the array and create two cut1 cut2 genie

# Create bezier1 bezier2, which is in the opposite direction.

# RunAction ();

# The result of a juice splash on the wall was shadow, because no suitable image code was found and commented out.

MainLayer.prototype.newSliceFruit = function (num, fruitPosition, rotation){    //one fruit sliced two piece,cut1 and cut2    var data = FRUIT_DATA[num];    var cutImages = data.cutImage;    var cut1 = cc.MySprite.create(this.rootNode, cutImages[0], fruitPosition, 100);    var cut2 = cc.MySprite.create(this.rootNode, cutImages[1], fruitPosition, 100);    cut1.setRotation(rotation);    cut2.setRotation(rotation);    var controlPoints1 = [        fruitPosition,        fruitPosition,        cc.p(fruitPosition.x - 200, -100)    ];    var bezier1 = cc.BezierTo.create(1, controlPoints1);    var controlPoints2 = [        fruitPosition,        fruitPosition,        cc.p(fruitPosition.x + 200, -100)    ];    var bezier2 = cc.BezierTo.create(1, controlPoints2);    var action2 = cc.RotateBy.create(1, getRandom(360));    cut1.runAction(cc.Spawn.create(cc.Sequence.create(bezier1, cc.CleanUp.create(cut1)), action2));    cut2.runAction(cc.Spawn.create(cc.Sequence.create(bezier2, cc.CleanUp.create(cut2)), action2.clone()));    //fruit shadow    /*var shadowImages = ["shadow2.png"];     var colors = [cc.c4b(200, 0, 0, 20), cc.c4b(0, 200, 0, 20), cc.c4b(0, 0, 200, 20), cc.c4b(200, 200, 0, 20)];     var shadow = cc.MySprite.create(this.rootNode, shadowImages[getRandom(shadowImages.length)], fruitPosition, 10);     shadow.setColor(colors[getRandom(colors.length)]);     shadow.setScale(0.5);     shadow.runAction(cc.Sequence.create(cc.DelayTime.create(2), cc.CleanUp.create(shadow)));*/};



4. Prompt scores. This is very simple. It is just a method for creating text labels.

MainLayer.prototype.scoreTip = function (p, message){    var director = cc.Director.getInstance();    var size = director.getWinSize();    var label = cc.LabelTTF.create(message, "Arial", 30);    label.setPosition(cc.p(p.x, p.y + 50));    label.setColor(cc.c3b(0, 255, 0));    label.setZOrder(10000);    this.rootNode.addChild(label);    label.runAction(cc.Sequence.create(cc.DelayTime.create(0.8), cc.CleanUp.create(label)));    return label;};

This article is here;



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 /;



FruitNinja Series

Fruit Ninja game source code download and Analysis (I)

Fruit Ninja game source code download and Analysis (medium)

Fruit Ninja (Fruit Ninja) game source code download and Analysis (II)



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


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)



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 CocosEditor game source code coming soon;

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



Related Article

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.