cocos3.x 扫雷03 游戏主场景布局设计

前端之家收集整理的这篇文章主要介绍了cocos3.x 扫雷03 游戏主场景布局设计前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

扫雷@H_403_4@@H_502_6@03 @H_403_4@游戏主场景布局设计@H_403_4@ @H_403_4@

游戏主场景的布局如图@H_403_4@ @H_403_4@

@H_403_4@ @H_403_4@

游戏场景类的设计@H_403_4@ @H_403_4@

游戏主场景类@H_403_4@GameScene@H_403_4@继承自@H_403_4@Layer@H_403_4@类,这是@H_403_4@cocos2.2.3@H_403_4@时代的习惯,现在其实不需要这样做了。继承自@H_403_4@Layer@H_403_4@是为了添加触摸事件的方便,现在@H_403_4@3.x@H_403_4@版本没有必要这样了。@H_403_4@

  1. 1:@H_403_4@ class@H_403_4@ GameScene : public@H_403_4@ cocos2d::Layer
  1. 2:@H_403_4@ {
  1. @H_404_119@ 3:@H_403_4@ public@H_403_4@:
  1. 4:@H_403_4@
  1. 5:@H_403_4@ virtual@H_403_4@ bool@H_403_4@ init();
  1. 6:@H_403_4@
  1. 7:@H_403_4@ //创建scene的时候选关,1==》9*9,2==》16*16,3==》24*24@H_403_4@
  1. 8:@H_403_4@ static@H_403_4@ cocos2d::CCScene* scene(int@H_403_4@ customs = 1);
  1. 9:@H_403_4@ //创建的时候就要确定@H_403_4@
  1. 10:@H_403_4@ static@H_403_4@ GameScene* create(int@H_403_4@ customs = 1);
  1. 11:@H_403_4@
  1. 12:@H_403_4@ protected@H_403_4@:
  1. 13:@H_403_4@ bool@H_403_4@ LoadResource(); //加载资源@H_403_4@
  1. 14:@H_403_4@ bool@H_403_4@ addBackGround(); //设置背景@H_403_4@
  1. 15:@H_403_4@ private@H_403_4@:
  1. 16:@H_403_4@ int@H_403_4@ _mine; //地雷数@H_403_4@
  1. 17:@H_403_4@ int@H_403_4@ _customs; //关数@H_403_4@
  1. 18:@H_403_4@ int@H_403_4@ _MaxRanks; //最大可用土地行列@H_403_4@
  1. 19:@H_403_4@ Sprite * _bgland; //雷区背景精灵@H_403_4@
  1. 20:@H_403_4@ Sprite * _bgmenu; //菜单项背景精灵@H_403_4@
  1. 21:@H_403_4@ };

@H_502_6@create@H_403_4@函数与@H_403_4@@H_502_6@scene@H_403_4@函数的实现@H_403_4@ @H_403_4@

create@H_403_4@函数创建了一个类实例,并对其的一些自有的成员(非继承@H_403_4@Layer@H_403_4@的)进行了赋值。@H_403_4@ @H_403_4@

1:@H_403_4@ GameScene* GameScene::create(int@H_403_4@ customs)
  1. @H_404_119@ 3:@H_403_4@ GameScene *s = new@H_403_4@ GameScene();
  1. 4:@H_403_4@ if@H_403_4@ (s != NULL) {
  1. 5:@H_403_4@ if@H_403_4@ (customs<0 || customs >3) {
  1. 6:@H_403_4@ CCLog("customs value error"@H_403_4@);
  1. 7:@H_403_4@ return@H_403_4@ NULL;
  1. 8:@H_403_4@ }
  1. 9:@H_403_4@ s->_customs = customs;
  1. 10:@H_403_4@ s->_count = 0; //记录当前扫了多少块区域@H_403_4@
  1. 11:@H_403_4@ s->_toFlag = false; //是不是去标记雷块@H_403_4@
  1. 12:@H_403_4@ s->init();
  1. 13:@H_403_4@ s->autorelease(); //此处需要,在addChile的时候自然会被retain@H_403_4@
  1. 14:@H_403_4@ }
  1. 15:@H_403_4@ else@H_403_4@ {
  1. 16:@H_403_4@ CC_SAFE_DELETE(s); //安全删除@H_403_4@
  1. 17:@H_403_4@ //#define CC_SAFE_DELETE(p) do { if(p) { delete (p); (p) = 0; } } while(0)@H_403_4@
  1. 18:@H_403_4@ //等价于===> do{ //使用do while是为了适应我们习惯性了加上分号,没有分号会提示错误@H_403_4@
  1. 19:@H_403_4@ // if(s!=NULL) {@H_403_4@
  1. 20:@H_403_4@ // delete s;@H_403_4@
  1. 21:@H_403_4@ // s=NULL;@H_403_4@
  1. 22:@H_403_4@ // }@H_403_4@
  1. 23:@H_403_4@ // }while(0)@H_403_4@
  1. 24:@H_403_4@ }
  1. 25:@H_403_4@ return@H_403_4@ s;
  1. 26:@H_403_4@ }
  1. 27:@H_403_4@
  1. 28:@H_403_4@
  1. 29:@H_403_4@ CCScene* GameScene::scene(int@H_403_4@ customs)
  1. 30:@H_403_4@ {
  1. 31:@H_403_4@ CCScene *scene = CCScene::create();
  1. 32:@H_403_4@
  1. 33:@H_403_4@ GameScene *layer = GameScene::create(customs);
  1. 34:@H_403_4@
  1. 35:@H_403_4@ scene->addChild(layer);
  1. 36:@H_403_4@
  1. 37:@H_403_4@ return@H_403_4@ scene;
  1. 38:@H_403_4@ }
@H_502_6@@H_403_4@
@H_502_6@LoadResource()@H_403_4@ 加载资源文件函数实现@H_403_4@ @H_403_4@

加载资源本来不应该在此处,因为还没有设计游戏起始选关界面,所以在此处做了。游戏需要加载的资源也不多,就几张小图片。音效素材没有,什么时候找到了随时可以添加。@H_403_4@ @H_403_4@

1:@H_403_4@ /////////////////////////////////////////////////////////////////////////@H_403_4@
  1. 2:@H_403_4@ // 初始化加载资源@H_403_4@
  1. @H_404_119@ 3:@H_403_4@ bool@H_403_4@ GameScene::LoadResource()
  1. 4:@H_403_4@ {
  1. 5:@H_403_4@ const@H_403_4@ char@H_403_4@* ImgFile[] = {
  1. 6:@H_403_4@ "0.png"@H_403_4@,
  1. 7:@H_403_4@ "1.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 8:@H_403_4@ "2.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 9:@H_403_4@ "3.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 10:@H_403_4@ "4.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 11:@H_403_4@ "5.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 12:@H_403_4@ "6.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 13:@H_403_4@ "7.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 14:@H_403_4@ "8.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 15:@H_403_4@ "no.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 16:@H_403_4@ "-1.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 17:@H_403_4@ "bg_land.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 18:@H_403_4@ "bg_menu.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 19:@H_403_4@ "boom.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 20:@H_403_4@ "flag.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 21:@H_403_4@ "blink_1.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 22:@H_403_4@ "blink_2.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 23:@H_403_4@ "exit.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 24:@H_403_4@ "exits.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 25:@H_403_4@ "restart.png"@H_403_4@,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 26:@H_403_4@ "restarts.png"@H_403_4@
  1. 27:@H_403_4@ };
  1. 28:@H_403_4@ for@H_403_4@ (int@H_403_4@ i = 0; i < sizeof@H_403_4@(ImgFile) / sizeof@H_403_4@(char@H_403_4@*); ++i) {
  1. 29:@H_403_4@ if@H_403_4@ (NULL == TextureCache::getInstance()->addImage(ImgFile[i])) {
  1. 30:@H_403_4@ return@H_403_4@ false;
  1. 31:@H_403_4@ }
  1. 32:@H_403_4@ }
  1. 33:@H_403_4@ return@H_403_4@ true;
  1. 34:@H_403_4@ }

@H_502_6@init@H_403_4@函数的实现@H_403_4@
@H_403_4@

init@H_403_4@函数要做的事情如下面代码的注释,因为这是之前写的,所以本次不一定如此。@H_403_4@ @H_403_4@

大致流程是这样的,被注释掉的说明本次还没有完全做好。现在只要加载背景来显示即可。@H_403_4@ @H_403_4@

  1. 1:@H_403_4@ bool@H_403_4@ GameScene::init()
  1. 2:@H_403_4@ {
  1. @H_404_119@ 3:@H_403_4@ //////////////////////////////@H_403_4@
  1. 4:@H_403_4@ // 1. super init first@H_403_4@
  1. 5:@H_403_4@ if@H_403_4@ (!CCLayer::init()) {
  1. 6:@H_403_4@ return@H_403_4@ false;
  1. 7:@H_403_4@ }
  1. 8:@H_403_4@ //加载资源@H_403_4@
  1. 9:@H_403_4@ LoadResource();
  1. 10:@H_403_4@ //设置背景@H_403_4@
  1. 11:@H_403_4@ addBackGround();
  1. 12:@H_403_4@ //设置菜单项@H_403_4@
  1. 13:@H_403_4@ //addMenu();@H_403_4@
  1. 14:@H_403_4@ //初始化土地,埋雷@H_403_4@
  1. 15:@H_403_4@ //BuryMine();@H_403_4@
  1. 16:@H_403_4@
  1. 17:@H_403_4@ //初始化雷区精灵@H_403_4@
  1. 18:@H_403_4@ //InitMine();@H_403_4@
  1. 19:@H_403_4@
  1. 20:@H_403_4@ //设置触摸事件@H_403_4@
  1. 21:@H_403_4@ //setTouchEnabled(true);@H_403_4@
  1. 22:@H_403_4@ //setTouchMode(kCCTouchesOneByOne);@H_403_4@
  1. 23:@H_403_4@ //setTouchPriority(2);@H_403_4@
  1. 24:@H_403_4@
  1. 25:@H_403_4@ CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  1. 26:@H_403_4@ CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  1. 27:@H_403_4@
  1. 28:@H_403_4@ return@H_403_4@ true;
  1. 29:@H_403_4@ }

@H_403_4@

@H_403_4@

@H_502_6@addBackGround()@H_403_4@加载背景函数的实现@H_403_4@

@H_403_4@ @H_403_4@

这里设计得有点繁琐,是为了少修改之前写的代码。@H_403_4@ @H_403_4@

1:@H_403_4@ //////////////////////////////////////////////////////////////////////@H_403_4@
  1. 2:@H_403_4@ // 设置背景@H_403_4@
  1. @H_404_119@ 3:@H_403_4@ bool@H_403_4@ GameScene::addBackGround()
  1. 5:@H_403_4@ //创建一个土地放置层,其是一个CCSprite,因为要显示背景@H_403_4@
  1. 6:@H_403_4@ Texture2D * texture = TextureCache::getInstance()->textureForKey("bg_land.png"@H_403_4@);
  1. 7:@H_403_4@ if@H_403_4@ (!texture) {
  1. 8:@H_403_4@ return@H_403_4@ false;
  1. 9:@H_403_4@ }
  1. 10:@H_403_4@ //设置雷区背景@H_403_4@
  1. 11:@H_403_4@ Sprite * bgland = Sprite::createWithTexture(texture);
  1. 12:@H_403_4@ //设置背景为宽度适应@H_403_4@
  1. 13:@H_403_4@ bgland->setScale(WinWidth / bgland->getContentSize().width);
  1. 14:@H_403_4@ //中央点为锚点@H_403_4@
  1. 15:@H_403_4@ bgland->setAnchorPoint(ccp(.0f,.0f));
  1. 16:@H_403_4@ addChild(bgland);
  1. 17:@H_403_4@ bgland->setPosition(ccp(0,WinHeigth - bgland->getBoundingBox().size.height)); //放置在中央@H_403_4@
  1. 18:@H_403_4@ _bgland = bgland;
  1. 19:@H_403_4@
  1. 20:@H_403_4@ //设置菜单背景@H_403_4@
  1. 21:@H_403_4@ texture = TextureCache::getInstance()->textureForKey("bg_menu.png"@H_403_4@);
  1. 22:@H_403_4@ Sprite * bgmenu = Sprite::createWithTexture(texture);
  1. 23:@H_403_4@ bgmenu->setScale(WinWidth / bgmenu->getContentSize().width);
  1. 24:@H_403_4@ bgmenu->setAnchorPoint(CCPointZero);
  1. 25:@H_403_4@ bgmenu->setPosition(CCPointZero);
  1. 26:@H_403_4@ addChild(bgmenu);
  1. 28:@H_403_4@ //添加了一个半透明的Layer,到时候用来防止雷区的格子块@H_403_4@
  1. 29:@H_403_4@ //本来是想放置在Sprite上的,算了,还是这个比较实在@H_403_4@
  1. 30:@H_403_4@ //加载一个半透明的CCLayerColor,来遮住背景@H_403_4@
  1. 31:@H_403_4@ _layerColor = LayerColor::create(ccc4(128,128,128),monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 32:@H_403_4@ WinWidth,//设置其大小和雷区背景一致@H_403_4@
  1. 33:@H_403_4@ bgland->getBoundingBox().size.height);
  1. 34:@H_403_4@ _layerColor->setPosition(bgland->getPosition());
  1. 35:@H_403_4@ addChild(_layerColor);
  1. 36:@H_403_4@ //设置触摸优先级 这个没有用,就没有在这里设置触摸事件@H_403_4@
  1. 37:@H_403_4@ //_layerColor->setTouchPriority(10);@H_403_4@
  1. 38:@H_403_4@ return@H_403_4@ true;
  1. 39:@H_403_4@ }

测试一下加载背景效果@H_403_4@

@H_403_4@

@H_403_4@

本来应该把菜单项也加进去的,不急这一点。先看看效果,待会再加。@H_403_4@ @H_403_4@

运行前应该把相关的资源拷贝到项目目录下的@H_403_4@res@H_403_4@文件夹@H_403_4@

@H_403_4@ @H_403_4@

测试前要先修改下窗口的尺寸和设计尺寸

和之前2.2.3版本的效果不一样,看来还得修改修改

修改后的效果@H_403_4@ @H_403_4@

修改后的@H_403_4@addBackGround()@H_403_4@函数@H_403_4@ @H_403_4@

6:@H_403_4@ Texture2D * texture = TextureCache::getInstance()->getTextureForKey("bg_land.png"@H_403_4@);
  1. 14:@H_403_4@ //零点为锚点@H_403_4@
  1. 15:@H_403_4@ bgland->setAnchorPoint(Vec2::ZERO);
  1. 17:@H_403_4@ bgland->setPosition(Vec2(0,WinHeigth - bgland->getBoundingBox().size.height)); //放置在上半屏幕@H_403_4@
  1. 19:@H_403_4@ bgland = nullptr;
  1. 21:@H_403_4@ texture = TextureCache::getInstance()->getTextureForKey("bg_menu.png"@H_403_4@);
  1. 23:@H_403_4@ //设置缩放宽度为适应屏幕宽度@H_403_4@
  1. 24:@H_403_4@ bgmenu->setScaleX(WinWidth / bgmenu->getContentSize().width);
  1. 25:@H_403_4@ //设置高度为适应除去_bgland后剩下的部分@H_403_4@
  1. 26:@H_403_4@ bgmenu->setScaleY(_bgland->getBoundingBox().getMinY() / bgmenu->getContentSize().height);
  1. 28:@H_403_4@ bgmenu->setAnchorPoint(Vec2::ZERO);
  1. 29:@H_403_4@ bgmenu->setPosition(Vec2::ZERO);
  1. 30:@H_403_4@ addChild(bgmenu);
  1. 31:@H_403_4@ _bgmenu = bgmenu;
  1. 32:@H_403_4@ bgmenu = nullptr;
  1. 33:@H_403_4@ //添加了一个半透明的Layer,到时候用来防止雷区的格子块@H_403_4@
  1. 34:@H_403_4@ //本来是想放置在Sprite上的,算了,还是这个比较实在@H_403_4@
  1. 35:@H_403_4@ //加载一个半透明的CCLayerColor,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 36:@H_403_4@ _layerColor = LayerColor::create(Color4B(128,64),monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 37:@H_403_4@ WinWidth,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 38:@H_403_4@ _bgland->getBoundingBox().size.height);
  1. 39:@H_403_4@ _layerColor->setPosition(_bgland->getPosition());
  1. 40:@H_403_4@ addChild(_layerColor);
  1. 41:@H_403_4@ //设置触摸优先级 这个没有用,就没有在这里设置触摸事件@H_403_4@
  1. 42:@H_403_4@ //_layerColor->setTouchPriority(10);@H_403_4@
  1. 43:@H_403_4@ return@H_403_4@ true;
  1. 44:@H_403_4@ }

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