寒風的Cocos2dx之旅之剪刀、石头、布系列专题(1)

前端之家收集整理的这篇文章主要介绍了寒風的Cocos2dx之旅之剪刀、石头、布系列专题(1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本节小编为读者们讲述如何在cocos2d上做一个剪刀石头布的游戏,游戏画面如下:


首先看到这个画面:我们来分析一下它到底有几个层?有菜单层、显示结果层、显示分数层、显示菜单背景层。宏观角度上看:有3个层:背景层->精灵层->菜单层。

让我们看看SettingScene.h头文件中都定义了什么:

  1. #include "cocos2d.h"
  2. #include "HelloWorldScene.h"
  3. class SettingScene:public cocos2d::Layer
  4. {
  5. public:
  6. static cocos2d::Scene* createScene();
  7. //初始化╮(╯▽╰)╭
  8. virtual bool init();
  9. //定义的监听回调函数╮(╯▽╰)╭
  10. void shitou(cocos2d::Ref* pSender);
  11. void jiandao(cocos2d::Ref* pSender);
  12. void bu(cocos2d::Ref* pSender);
  13. //宏定义*************************************PS:当时就是因为下边括号里是HelloWorld而纠结为何场景不能切换╮(╯▽╰)╭
  14. CREATE_FUNC(SettingScene);
  15. //分别设定点击按钮的背景、分数框体的背景
  16. cocos2d::LayerColor* backcolor1;
  17. cocos2d::LayerColor* backcolor2;
  18. cocos2d::LayerColor* backcolor3;
  19. //定义显示数字的标签控件
  20. cocos2d::LabelTTF* LabelTTFCountscore;
  21. cocos2d::LabelTTF* LabelTTFWinscore;
  22. cocos2d::LabelTTF* LabelTTFLosescore;
  23. cocos2d::LabelTTF* LabelTTFDrawscore;
  24. //定义计算出拳次数
  25. void ChuquanCount();
  26. //定义胜利次数
  27. void WinCount();
  28. //定义失败次数
  29. void LoseCount();
  30. //定义平局次数
  31. void DrawCount();
  32. };
之后先在SettingScene.cpp中实现场景的回调:

  1. Scene* SettingScene::createScene()
  2. {
  3. auto scene = Scene::create();
  4. auto layer = SettingScene::create();
  5. scene->addChild(layer);
  6. return scene;
  7. }
在Init()中定义:

  1. //设置按钮的背景
  2. backcolor1 = cocos2d::LayerColor::create(cocos2d::Color4B(0xFF,0x00,0x80),visibleSize.width-350,200);
  3. backcolor1->setPosition(Point(200,10));
  4. //backcolor1->setAnchorPoint(Point(200,0));
  5. this->addChild(backcolor1);
  6. //设置分数框体背景
  7. backcolor2= cocos2d::LayerColor::create(cocos2d::Color4B(30,144,255,255),140,150);
  8. backcolor2->setPosition(Point(30,340));
  9. this->addChild(backcolor2);
  10. //设置PLAY标签
  11. auto LabelTTFPlay=LabelTTF::create("Play:","HiraKakuProN-W3",30);
  12. LabelTTFPlay->setPosition(Point(250,190));
  13. LabelTTFPlay->setColor(Color3B(0,127));//*********************RGB颜色
  14. addChild(LabelTTFPlay);
  15. //设置分数标签
  16. auto LabelTTFCount=LabelTTF::create("count:",30);
  17. LabelTTFCount->setPosition(Point(80,460));
  18. addChild(LabelTTFCount);
  19. auto LabelTTFWin=LabelTTF::create("Win:",30);
  20. LabelTTFWin->setPosition(Point(80,430));
  21. addChild(LabelTTFWin);
  22. auto LabelTTFLose=LabelTTF::create("Lose:",30);
  23. LabelTTFLose->setPosition(Point(80,400));
  24. addChild(LabelTTFLose);
  25. auto LabelTTFDraw=LabelTTF::create("draw:",30);
  26. LabelTTFDraw->setPosition(Point(80,370));
  27. addChild(LabelTTFDraw);
  28. //游戏提示关键词:
  29. auto LabelTTFPlayer=LabelTTF::create("Player",40);
  30. LabelTTFPlayer->setPosition(Point(350,500));
  31. addChild(LabelTTFPlayer);
  32. auto LabelTTFCom=LabelTTF::create("Com",40);
  33. LabelTTFCom->setPosition(Point(650,500));
  34. addChild(LabelTTFCom);
现在我们把几个背景层建立好了,下边就是将菜单层放到我们的背景中:

  1. //添加所需要的精灵
  2. auto sprite01=Sprite::create("shitou.png");
  3. sprite01->setPosition(Point(300,100));
  4. this->addChild(sprite01);
  5.  
  6. auto sprite02=Sprite::create("jiandao.png");
  7. sprite02->setPosition(Point(500,100));
  8. this->addChild(sprite02);
  9.  
  10. auto sprite03=Sprite::create("bu.png");
  11. sprite03->setPosition(Point(700,100));
  12. this->addChild(sprite03);
  13.  
  14. auto sprite04=Sprite::create("title.png");
  15. sprite04->setPosition(Point(500,550));
  16. this->addChild(sprite04);
  17.  
  18. auto spritePlayer=Sprite::create("shitou.png");
  19. spritePlayer->setPosition(Point(350,350));
  20. this->addChild(spritePlayer);
  21. spritePlayer->setScale(1,1);
  22.  
  23. auto spriteComputer=Sprite::create("shitou.png");
  24. spriteComputer->setPosition(Point(650,350));
  25. this->addChild(spriteComputer);
  26. spriteComputer->setScale(1,1);
  27.  
  28. //加入具体的分数
  29. LabelTTFCountscore=LabelTTF::create("0",30);
  30. LabelTTFCountscore->setPosition(Point(150,460));
  31. addChild(LabelTTFCountscore);
  32. LabelTTFWinscore=LabelTTF::create("0",30);
  33. LabelTTFWinscore->setPosition(Point(150,430));
  34. addChild(LabelTTFWinscore);
  35. LabelTTFLosescore=LabelTTF::create("0",30);
  36. LabelTTFLosescore->setPosition(Point(150,400));
  37. addChild(LabelTTFLosescore);
  38. LabelTTFDrawscore=LabelTTF::create("0",30);
  39. LabelTTFDrawscore->setPosition(Point(150,370));
  40. addChild(LabelTTFDrawscore);
PS:小编我当时编写程序的时候遇到了个问题,在这里想跟大家分享一下

①Layer这个类没法定义锚点,所以要setPosition来定义它的位置,在你定义背景的长度宽度基础上。

cocos2d::Color4B(30,150

括号里的前三个是颜色值RGB,百度一下RGB颜色表。最后一个是透明度,255是不透明,0是全透明。140和159分别代表背景区域的宽度和高度。

这样游戏界面就顺利完成了!

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