Hub

KillStar

Thumb_豌豆荚截图20140312110713

Brief

A PopStar 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]
 
 

Killstar Code Run

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

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

Killstar Code Download

Github:[https://github.com/makeapp/cocoseditor-fruitninja]

SkyDrive:[http://share.weiyun.com/77b7f8ca11ec0451906148f70e8b7363]

 
 

Killstar Code Analysis

1 touch event

 

    MainLayer.prototype.onTouchesBegan = function (touches, event) {
    var loc = touches[0].getLocation();
    this.ccTouchBeganPos = loc;

    for (var i = 0; i < this.starTable.length; i++) {
        var sprites = this.starTable[i];
        for (var j = 0; j < sprites.length; j++) {
            var pSprite0 = sprites[j];
            if (pSprite0) {
                var ccRect = pSprite0.getBoundingBox();
                if (isInRect(ccRect, this.ccTouchBeganPos)) {
                    if (this.sameColorList.length > 1) {
                        if (this.sameColorList.contains(pSprite0)) {
                            cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.broken, false);
                            this.removeSameColorStars();
                        } else {
                            for (var k = 0; k < this.sameColorList.length; k++) {
                                if (this.sameColorList[k]) {
                                    this.sameColorList[k].runAction(cc.ScaleTo.create(0.1, 1));
                                }
                            }
                            this.checkSameColorStars(pSprite0);
                            if (this.sameColorList.length > 1) {
                                cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.select, false);
                            }
                        }
                    } else {
                        this.checkSameColorStars(pSprite0);
                        if (this.sameColorList.length > 1) {
                            cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.select, false);
                        }
                    }

                    break;
                }
            }
        }
    }
};

 

2 check one star four side

 

MainLayer.prototype.checkOneStarFourSide = function (sprite) {
    if (sprite == null) {
        return;
    }
    // cc.log("checkOneStarFourSide");
    var fourSideSpriteList = [];
    var color = sprite.starData.color;
    var col = sprite.starData.indexOfColumn;
    var row = sprite.starData.indexOfRow;

    //up
    if (row < 9) {
        var upSprite = this.starTable[col][row + 1];
        if (upSprite != null && upSprite.starData.color == color) {
            fourSideSpriteList.push(upSprite);
        }
    }

    //down
    if (row > 0) {
        var downSprite = this.starTable[col][row - 1];
        if (downSprite != null && downSprite.starData.color == color) {
            fourSideSpriteList.push(downSprite);
        }
    }

    //left
    if (col > 0) {
        var leftSprite = this.starTable[col - 1][row];
        if (leftSprite != null && leftSprite.starData.color == color) {
            fourSideSpriteList.push(leftSprite);
        }
    }

    //right
    if (col < 9) {
        var rightSprite = this.starTable[col + 1][row];
        if (rightSprite != null && rightSprite.starData.color == color) {
            fourSideSpriteList.push(rightSprite);
        }
    }
    return fourSideSpriteList;
}

 

3 check same color stars

 

MainLayer.prototype.checkSameColorStars = function (sprite) {
    if (sprite == null) {
        return;
    }
    this.sameColorList = [];
    this.sameColorList.push(sprite);
    var newSameColorList = [];
    newSameColorList.push(sprite);

    //by logic ,check the same color star list
    while (newSameColorList.length > 0) {
        for (var i = 0; i < newSameColorList.length; i++) {
            var fourSide = this.checkOneStarFourSide(newSameColorList[i]);
            if (fourSide.length > 0) {
                for (var j = 0; j < fourSide.length; j++) {
                    if (!this.sameColorList.contains(fourSide[j])) {
                        this.sameColorList.push(fourSide[j]);
                        newSameColorList.push(fourSide[j]);
                    }
                }
            }
            newSameColorList.splice(i, 1);
        }
    }
    cc.log("sameColorList length==" + this.sameColorList.length);
    if (this.sameColorList.length > 1) {
        for (var k = 0; k < this.sameColorList.length; k++) {
            var simpleStar = this.sameColorList[k];
            if (simpleStar) {
                simpleStar.runAction(cc.ScaleTo.create(0.1, 1.08));
            }
        }
    }
}

 

4 remove same color stars

 

MainLayer.prototype.removeSameColorStars = function () {
    for (var k = 0; k < this.sameColorList.length; k++) {
        var simpleStar = this.sameColorList[k];
        if (simpleStar) {
            var col = simpleStar.starData.indexOfColumn;
            var row = simpleStar.starData.indexOfRow;
            this.starTable[col].splice(row, 1, null);
            this.rootNode.removeChild(simpleStar);
            if (sys.platform != 'browser') {
                var starParticle = cc.StarParticle.create(this.rootNode, (36 + col * this.starSize), (36 + row * this.starSize), "spark");
                starParticle.runAction(cc.Sequence.create(cc.DelayTime.create(0.8), cc.CleanUp.create(starParticle)));
            }
        }
    }
    this.sameColorList = [];
    this.fallStar();
}

 

5 fall stars

 

MainLayer.prototype.fallStar = function () {
    for (var i = 0; i < this.starTable.length; i++) {
        var sprites = this.starTable[i];
        var length = sprites.length;
        for (var j = 0; j < length; j++) {
            var pSprite0 = sprites[j];
            if (pSprite0 == null) {
                var k = j + 1;
                while (k < length) {
                    var upSprite = sprites[k];
                    if (upSprite != null) {
                        upSprite.starData.indexOfColumn = i;
                        upSprite.starData.indexOfRow = j;
                        this.starTable[i].splice(j, 1, upSprite);
                        this.starTable[i].splice(k, 1, null);
                        k = length;
                        var flowTime = 0.2;
                        var fallAction = cc.MoveTo.create(flowTime, cc.p(36 + i * this.starSize,
                            36 + j * this.starSize));
                        upSprite.runAction(fallAction);
                    }
                    k++;
                }
            }
        }
    }

    this.deadStar();
    // this.combineStar();
}

 

6 combine stars

 

MainLayer.prototype.combineStar = function () {
    for (var m = 0; m < this.starTable.length; m++) {
        var mSprite0 = this.starTable[m][0];
        if (mSprite0 == null) {
            if (m == (this.starTable.length - 1)) {
                for (var j = 0; j < this.starTable[m].length; j++) {
                    this.starTable[m].splice(j, 1, null);
                }
            }
            else {
                for (var i = (m + 1); i < this.starTable.length; i++) {
                    // this.starTable.splice((i - 1), 1, this.starTable[i]);
                    for (var j = 0; j < this.starTable[i].length; j++) {
                        var pSprite0 = this.starTable[i][j];
                        this.starTable[i - 1].splice(j, 1, pSprite0);
                        if (pSprite0 != null) {
                            pSprite0.starData.indexOfColumn = (i - 1);
                            var col = pSprite0.starData.indexOfColumn;
                            var row = pSprite0.starData.indexOfRow;
                            var moveAction = cc.MoveTo.create(0.1, cc.p(36 + col * this.starSize,
                                36 + row * this.starSize));
                            pSprite0.runAction(moveAction);
                        }
                    }
                }
            }
        }
    }
    this.deadStar();
}

 

7 check dead

 

MainLayer.prototype.deadStar = function () {
    var isDead = true;
    for (var i = 0; i < this.starTable.length; i++) {
        var sprites = this.starTable[i];
        var length = sprites.length;
        for (var j = 0; j < length; j++) {
            var pSprite0 = sprites[j];
            if (pSprite0 != null) {
                if (this.checkOneStarFourSide(pSprite0).length > 0) {
                    isDead = false;
                    return;
                }
            }
        }
    }

    if (isDead) {
        for (var jj = 9; jj >= 0; jj--) {
            for (var ii = 0; ii < 10; ii++) {
                var pSprite0 = this.starTable[ii][jj];
                if (pSprite0 != null) {
                    var delay = 4 + 0.3 * ii - 0.4 * jj;
                    pSprite0.runAction(cc.Sequence.create(
                        cc.DelayTime.create(delay),
                        cc.CleanUp.create(pSprite0)
                    ));
                    var starParticle = cc.StarParticle.create(this.rootNode, (36 + ii * this.starSize), (36 + jj * this.starSize), "spark");
                    starParticle.runAction(cc.Sequence.create(cc.ScaleTo.create(0, 0),
                        cc.DelayTime.create(delay), cc.ScaleTo.create(0, 1), cc.DelayTime.create(0.8),
                        cc.CleanUp.create(starParticle)));
                }
            }
        }
    }
}

 

popstar.rar - popstar code (2 MB) touchsnow, 2014-03-27 03:16

Comments

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