扫雷@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@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@
@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@
- 8:@H_403_4@ }
- 9:@H_403_4@ s->_customs = customs;
- 12:@H_403_4@ s->init();
- 14:@H_403_4@ }
- 24:@H_403_4@ }
- 26:@H_403_4@ }
- 27:@H_403_4@
- 28:@H_403_4@
- 30:@H_403_4@ {
- 31:@H_403_4@ CCScene *scene = CCScene::create();
- 32:@H_403_4@
- 33:@H_403_4@ GameScene *layer = GameScene::create(customs);
- 34:@H_403_4@
- 35:@H_403_4@ scene->addChild(layer);
- 36:@H_403_4@
- 38:@H_403_4@ }
加载资源本来不应该在此处,因为还没有设计游戏起始选关界面,所以在此处做了。游戏需要加载的资源也不多,就几张小图片。音效素材没有,什么时候找到了随时可以添加。@H_403_4@ @H_403_4@
- 4:@H_403_4@ {
- 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@
- 27:@H_403_4@ };
- 31:@H_403_4@ }
- 32:@H_403_4@ }
- 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@
- 2:@H_403_4@ {
- 7:@H_403_4@ }
- 9:@H_403_4@ LoadResource();
- 11:@H_403_4@ addBackGround();
- 16:@H_403_4@
- 19:@H_403_4@
- 24:@H_403_4@
- 25:@H_403_4@ CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- 26:@H_403_4@ CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
- 27:@H_403_4@
- 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@
- 9:@H_403_4@ }
- 11:@H_403_4@ Sprite * bgland = Sprite::createWithTexture(texture);
- 13:@H_403_4@ bgland->setScale(WinWidth / bgland->getContentSize().width);
- 15:@H_403_4@ bgland->setAnchorPoint(ccp(.0f,.0f));
- 16:@H_403_4@ addChild(bgland);
- 18:@H_403_4@ _bgland = bgland;
- 19:@H_403_4@
- 22:@H_403_4@ Sprite * bgmenu = Sprite::createWithTexture(texture);
- 23:@H_403_4@ bgmenu->setScale(WinWidth / bgmenu->getContentSize().width);
- 24:@H_403_4@ bgmenu->setAnchorPoint(CCPointZero);
- 25:@H_403_4@ bgmenu->setPosition(CCPointZero);
- 26:@H_403_4@ addChild(bgmenu);
- 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@
- 34:@H_403_4@ _layerColor->setPosition(bgland->getPosition());
- 35:@H_403_4@ addChild(_layerColor);
- 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@
修改后的@H_403_4@addBackGround()@H_403_4@函数@H_403_4@ @H_403_4@
- 15:@H_403_4@ bgland->setAnchorPoint(Vec2::ZERO);
- 19:@H_403_4@ bgland = nullptr;
- 24:@H_403_4@ bgmenu->setScaleX(WinWidth / bgmenu->getContentSize().width);
- 28:@H_403_4@ bgmenu->setAnchorPoint(Vec2::ZERO);
- 29:@H_403_4@ bgmenu->setPosition(Vec2::ZERO);
- 30:@H_403_4@ addChild(bgmenu);
- 31:@H_403_4@ _bgmenu = bgmenu;
- 32:@H_403_4@ bgmenu = nullptr;
- 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);
- 39:@H_403_4@ _layerColor->setPosition(_bgland->getPosition());
- 40:@H_403_4@ addChild(_layerColor);
- 44:@H_403_4@ }