Hub

Fruit Ninja For Cocos2d-JS

Thumb_qq截图20140329202607

Brief

A Fruit Ninja kind of demo made in JavaScript,base on Cocos2d-html5,Develop by CocosEditor, Runs on Android, iOS,Html5 & PC.

Description

Follow My Github,Many Cocos2d-js Games To Learn

FlappyBird: [https://github.com/makeapp/cocoseditor-flappybird]
PopStar: [https://github.com/makeapp/cocoseditor-popstar]
FruitNinja: [https://github.com/makeapp/cocoseditor-fruitninja]
MoonWarriors: [https://github.com/makeapp/cocoseditor-moonwarriors]
FruitAttack: [https://github.com/makeapp/cocoseditor-fruitattack]
TestsJavascript: [https://github.com/makeapp/cocoseditor-tests]
 
 

Fruit Ninja Code Run

Base infomation about popstar on my csdn page: [http://blog.csdn.net/touchsnow/article/category/2153369]

Run the demo,you must download and configure CocosEditor: [http://blog.csdn.net/touchsnow/article/details/19070665]
 
 

Fruit Ninja Code Download

Gihtub:[https://github.com/makeapp/cocoseditor-popstar]

SkyDrive:[http://blog.makeapp.co/?p=319]

 
 

Fruit Ninja Code Analysis

 

1 create one new fruit per 0.5s

MainLayer.prototype.newFruit = function ()
{
    //create new fruit per 0.5s
    var random = getRandom(FRUIT_DATA.length);
    var data = FRUIT_DATA[random];
    var fruit = cc.Sprite.createWithSpriteFrameName(data.fruitImage);
    var x = 300 + getRandomOffset(200);
    fruit.setPosition(cc.p(x, -100));
    fruit.setAnchorPoint(cc.p(0.5, 0.5));
    this.fruitZOrder -= 1;
    fruit.setZOrder(this.fruitZOrder);
    fruit.cleanuped = false;
    fruit.num = data.num;
    var offSetX = getRandomOffset(100);
    var topY = 900 + getRandom(100);
    var controlPoints = [
        cc.p(x, -100),
        cc.p(x - offSetX, topY),
        cc.p(x - offSetX * 2, -100)
    ];
    var fruitTime = 3;
    var bezier = cc.BezierTo.create(fruitTime, controlPoints);
    var action2 = cc.RotateBy.create(fruitTime, getRandom(360));
    var upActions = cc.Spawn.create(cc.Sequence.create(bezier, cc.CleanUp.create(fruit)), action2);
    fruit.runAction(upActions);
    this.rootNode.addChild(fruit);
    this.fruitList.push(fruit);
    cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.throw, false);
};

 
2 one fruit sliced two piece,cut1 and cut2
 

MainLayer.prototype.newSliceFruit = function (num, fruitPosition, rotation)
{

    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()));



};

 
3 touch and collision detection
 

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)) {

                //if bomb
                if (fruit.num == 5) {
                    this.gameStatus = OVER;
                    cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.boom, false);
                    var light = cc.MySprite.create(this.rootNode, FRUIT_DATA[5].cutImage, loc, 1100);
                    light.runAction(cc.Sequence.create(
                            cc.Spawn.create(cc.ScaleTo.create(2, 10), cc.RotateBy.create(1, 360)),
                            cc.CleanUp.create(light),
                            cc.CallFunc.create(function ()
                            {
                                this.gameOver();
                            }, this)
                    ));
                    return;
                }

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

Comments

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.