cocos2dx有限状态机

前端之家收集整理的这篇文章主要介绍了cocos2dx有限状态机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@在游戏开发中,状态机几乎是必不可少的,简单地说,状态机就是控制游戏对象在不同的状态下应该做什么事情的一个机制。

@H_502_0@

@H_502_0@

@H_502_0@

  1. @H_502_0@#ifndef__MUTOU_H__

  2. @H_502_0@#define__MUTOU_H__

  3. @H_502_0@

  4. @H_502_0@#include"cocos2d.h"

  5. @H_502_0@USING_NS_CC;

  6. @H_502_0@

  7. @H_502_0@enumEnumState{

  8. @H_502_0@enStateWriteCode,/*状态:写代码*/

  9. @H_502_0@enStateWriteArticle,0);">/*状态:写教程*/

  10. @H_502_0@enStateRest,0);">/*状态:休息*/

  11. @H_502_0@};

  12. @H_502_0@

  13. @H_502_0@classMutou:publicCCNode{

  14. @H_502_0@public:

  15. @H_502_0@CREATE_FUNC(Mutou);

  16. @H_502_0@virtualboolinit();

  17. @H_502_0@

  18. @H_502_0@/*当前状态*/

  19. @H_502_0@EnumStateenCurState;

  20. @H_502_0@

  21. @H_502_0@/*判断是否写代码写累了*/

  22. @H_502_0@boolisTire();

  23. @H_502_0@

  24. @H_502_0@/*是否想写教程*/

  25. @H_502_0@boolisWantToWriteArticle();

  26. @H_502_0@

  27. @H_502_0@/*写代码*/

  28. @H_502_0@voidwriteCode();

  29. @H_502_0@

  30. @H_502_0@/*写教程*/

  31. @H_502_0@voidwriteArticle();

  32. @H_502_0@

  33. @H_502_0@/*休息*/

  34. @H_502_0@voidrest();

  35. @H_502_0@

  36. @H_502_0@/*切换状态*/

  37. @H_502_0@voidchangeState(EnumStateenState);

  38. @H_502_0@

  39. @H_502_0@virtualvoidupdate(floatdt);

  40. @H_502_0@};

  41. @H_502_0@

  42. @H_502_0@#endif

  43. @H_502_0@

  44. @H_502_0@

  45. @H_502_0@

    @H_502_0@

    @H_502_0@输出

    mutouiswirtingCode.

    mutouisresting.

    mutouiswritingarticle.

    mutouiswirtingCode.

    @H_502_0@

    @H_502_0@有限状态机的运用可以极大的简化程序 让代码更加的清晰易懂 便于阅读 当游戏主角能根据当前状态进行动作自动切换执行的时候可以极大的减少代码

    1. @H_502_0@boolHelloWorld::init()

    2. @H_502_0@{

    3. @H_502_0@boolbRet=false;

    4. @H_502_0@do

    5. @H_502_0@{

    6. @H_502_0@CC_BREAK_IF(!CCLayer::init());

    7. @H_502_0@

    8. @H_502_0@/*新建木头角色*/

    9. @H_502_0@mMutou=Mutou::create();

    10. @H_502_0@

    11. @H_502_0@/*初始化木头的状态为休息*/

    12. @H_502_0@mMutou->changeState(enStateRest);

    13. @H_502_0@

    14. @H_502_0@this->addChild(mMutou);

    15. @H_502_0@bRet=true;

    16. @H_502_0@}while(0);

    17. @H_502_0@

    18. @H_502_0@returnbRet;

    19. @H_502_0@}

    20. @H_502_0@#include"Mutou.h"

    21. @H_502_0@

    22. @H_502_0@boolMutou::init(){

    23. @H_502_0@this->scheduleUpdate();

    24. @H_502_0@returntrue;

    25. @H_502_0@}

    26. @H_502_0@

    27. @H_502_0@voidMutou::changeState(EnumStateenState){

    28. @H_502_0@this->enCurState=enState;

    29. @H_502_0@}

    30. @H_502_0@

    31. @H_502_0@boolMutou::isTire(){

    32. @H_502_0@/*每次问木头累不累,他都会说:累~*/

    33. @H_502_0@returntrue;

    34. @H_502_0@}

    35. @H_502_0@

    36. @H_502_0@boolMutou::isWantToWriteArticle(){

    37. @H_502_0@/*有10%的概率想写教程(好懒~!)*/

    38. @H_502_0@floatran=CCRANDOM_0_1();

    39. @H_502_0@if(ran<0.1f){

    40. @H_502_0@returntrue;

    41. @H_502_0@}

    42. @H_502_0@

    43. @H_502_0@returnfalse;

    44. @H_502_0@}

    45. @H_502_0@

    46. @H_502_0@voidMutou::writeCode(){

    47. @H_502_0@CCLOG("mutouiswirtingCode.");

    48. @H_502_0@}

    49. @H_502_0@

    50. @H_502_0@voidMutou::writeArticle(){

    51. @H_502_0@CCLOG("mutouiswritingarticle.");

    52. @H_502_0@}

    53. @H_502_0@

    54. @H_502_0@

    55. @H_502_0@voidMutou::rest(){

    56. @H_502_0@CCLOG("mutouisresting.");

    57. @H_502_0@}

    58. @H_502_0@

    59. @H_502_0@voidMutou::update(floatdt){

    60. @H_502_0@/*判断在每一种状态下应该做什么事情*/

    61. @H_502_0@switch(enCurState){

    62. @H_502_0@caseenStateWriteCode:

    63. @H_502_0@/*如果累了就休息,并且切换到休息状态*/

    64. @H_502_0@if(isTire()){

    65. @H_502_0@rest();

    66. @H_502_0@changeState(enStateRest);

    67. @H_502_0@}

    68. @H_502_0@break;

    69. @H_502_0@caseenStateWriteArticle:

    70. @H_502_0@

    71. @H_502_0@if(isTire()){

    72. @H_502_0@rest();

    73. @H_502_0@changeState(enStateRest);

    74. @H_502_0@}

    75. @H_502_0@break;

    76. @H_502_0@caseenStateRest:

    77. @H_502_0@/*一定的概率写代码,一定的概率写教程,并且切换到相应的状态*/

    78. @H_502_0@if(isWantToWriteArticle()){

    79. @H_502_0@writeArticle();

    80. @H_502_0@changeState(enStateWriteArticle);

    81. @H_502_0@}

    82. @H_502_0@else{

    83. @H_502_0@writeCode();

    84. @H_502_0@changeState(enStateWriteCode);

    85. @H_502_0@}

    86. @H_502_0@break;

    87. @H_502_0@}

    88. @H_502_0@}

猜你在找的Cocos2d-x相关文章