Fruit Ninja(水果忍者)游戏源代码下载、分析(下)—可运行Android,Ios,Window,Mac,Html5平台…

  • Post author:
  • Post category:其他



背景:

这一篇是结尾篇,主要分析地雷检测,游戏结束和保存最高分;

ps:

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

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

3 运行demo需要配置好

CocosEditor

,暂不支持其他工具。demo是跨平台的,可移植运行android,ios,html5网页等。


源代码下载:

请到代码集中营下载(

水果忍者

):

http://blog.makeapp.co/?p=319


效果图:






代码分析:


1 进入主场景从本地数据库中获取最高分,并显示最高分文字this.bestScoreLabel;



  1. //bestScore





  2. this


    .bestScore = sys.localStorage.getItem(


    “bestScore”


    );



  3. if


    (


    this


    .bestScore !=


    null


    &&


    this


    .bestScore != undefined) {



  4. this


    .bestScore = Number(


    this


    .bestScore);


  5. }


  6. else


    {



  7. this


    .bestScore = 0;


  8. }

  9. cc.log(

    “bestScore==”


    +


    this


    .bestScore);



  10. this


    .bestScoreLabel.setString(FRUIT_STRINGS.bestScore +


    this


    .bestScore);



  11. this


    .overLayer.setVisible(


    false


    );




2  在触摸移动的过程中,会切到水果也会切刀地雷,如果是地雷,游戏直接结束 ;

#我们已经建立过水果数组,地雷的编号num是5,所以很简单,只要判断水果的编号就可以轻易的确定地雷;

#如果是地雷,游戏状态over,播放音效boom;

#创建地雷光芒light精灵;

#播放一个序列动画,放大选择 然后清除,最后回调到结束函数this.gameOver();



  1. //if bomb





  2. if


    (fruit.num == 5) {



  3. this


    .gameStatus = OVER;


  4. cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.boom,

    false


    );



  5. var


    light = cc.MySprite.create(


    this


    .rootNode, FRUIT_DATA[5].cutImage, loc, 1100);


  6. light.runAction(cc.Sequence.create(

  7. cc.Spawn.create(cc.ScaleTo.create(2, 10), cc.RotateBy.create(1, 360)),

  8. cc.CleanUp.create(light),

  9. cc.CallFunc.create(

    function


    ()


  10. {


  11. this


    .gameOver();


  12. },

    this


    )


  13. ));


  14. return


    ;


  15. }

3 游戏结束有两种情况,一种是时间到,另一种是切到地雷;

#游戏结束后,显示游戏层overLayer;

#提示你得到的分数gameScoreTip;

#如果本次玩的最高分大于历史最佳分数,本地数据库存储当前分数

#延时6s回到开始界面;



  1. MainLayer.prototype.gameOver =


    function


    ()


  2. {

  3. cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.over,

    false


    );



  4. this


    .overLayer.setZOrder(1000);



  5. this


    .overLayer.setVisible(


    true


    );




  6. var


    gameScoreTip = FRUIT_STRINGS.youGet +


    this


    .totalScore + FRUIT_STRINGS.score;


  7. cc.log(

    “this.totalScore=”


    +


    this


    .totalScore);


  8. cc.log(

    “this.bestScore=”


    +


    this


    .bestScore);



  9. if


    (


    this


    .totalScore >


    this


    .bestScore) {


  10. cc.log(

    “this.bestScore=”


    +


    this


    .bestScore);


  11. sys.localStorage.setItem(

    “bestScore”


    ,


    this


    .totalScore +


    “”


    );


  12. gameScoreTip = gameScoreTip + FRUIT_STRINGS.record;

  13. }


  14. this


    .overScoreLabel.setString(gameScoreTip);




  15. this


    .overLayer.scheduleOnce(


    function


    ()


  16. {

  17. cc.AudioEngine.getInstance().stopAllEffects();

  18. cc.BuilderReader.runScene(

    “”


    ,


    “StartLayer”


    );


  19. }, 6);


  20. };




水果忍者系列结束



版权声明:本文为u012769906原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。