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

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

概述       本节小编为读者们讲述如何在cocos2d上做一个剪刀石头布的游戏,游戏画面如下:       首先看到这个画面:我们来分析一下它到底有几个层?有菜单层、显示结果层、显示分数层、显示菜单背景层。宏观角度上看:有3个层:背景层->精灵层->菜单层。       让我们看看SettingScene.h头文件中都定义了什么: #include "cocos2d.h"#include "He

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


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

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

#include "cocos2d.h"#include "HelloWorldScene.h"class SettingScene:public cocos2d::Layer{public:	static cocos2d::Scene* createScene();	//初始化╮(╯▽╰)╭    virtual bool init();    //定义的监听回调函数╮(╯▽╰)╭    voID shitou(cocos2d::Ref* pSender);    voID jiandao(cocos2d::Ref* pSender);	voID bu(cocos2d::Ref* pSender);	//宏定义*************************************PS:当时就是因为下边括号里是HelloWorld而纠结为何场景不能切换╮(╯▽╰)╭	CREATE_FUNC(SettingScene);	 //分别设定点击按钮的背景、分数框体的背景    cocos2d::Layercolor* backcolor1;	cocos2d::Layercolor* backcolor2;	cocos2d::Layercolor* backcolor3;	//定义显示数字的标签控件    cocos2d::LabelTTF* LabelTTFCountscore;	cocos2d::LabelTTF* LabelTTFWinscore;	cocos2d::LabelTTF* LabelTTFLosescore;	cocos2d::LabelTTF* LabelTTfdrawscore;	//定义计算出拳次数	voID ChuquanCount();    //定义胜利次数	voID WinCount();	//定义失败次数	voID LoseCount();	//定义平局次数	voID DrawCount();};
之后先在SettingScene.cpp中实现场景的回调:

Scene* SettingScene::createScene(){    auto scene = Scene::create();	auto layer = SettingScene::create();    scene->addChild(layer);    return scene;}
在Init()中定义:

	//设置按钮的背景	backcolor1 = cocos2d::Layercolor::create(cocos2d::color4B(0xFF,0x00,0x80),visibleSize.wIDth-350,200);	backcolor1->setposition(Point(200,10));	//backcolor1->setAnchorPoint(Point(200,0));    this->addChild(backcolor1);	//设置分数框体背景	backcolor2= cocos2d::Layercolor::create(cocos2d::color4B(30,144,255,255),140,150);	backcolor2->setposition(Point(30,340));	this->addChild(backcolor2);	//设置PLAY标签	auto LabelTTFPlay=LabelTTF::create("Play:","HiraKakuProN-W3",30);	LabelTTFPlay->setposition(Point(250,190));	LabelTTFPlay->setcolor(color3B(0,127));//*********************RGB颜色	addChild(LabelTTFPlay);	//设置分数标签	auto LabelTTFCount=LabelTTF::create("count:",30);	LabelTTFCount->setposition(Point(80,460));	addChild(LabelTTFCount);	auto LabelTTFWin=LabelTTF::create("Win:",30);	LabelTTFWin->setposition(Point(80,430));	addChild(LabelTTFWin);	auto LabelTTFLose=LabelTTF::create("Lose:",30);	LabelTTFLose->setposition(Point(80,400));	addChild(LabelTTFLose);	auto LabelTTfdraw=LabelTTF::create("draw:",30);	LabelTTfdraw->setposition(Point(80,370));	addChild(LabelTTfdraw);	//游戏提示关键词:	auto LabelTTFPlayer=LabelTTF::create("Player",40);	LabelTTFPlayer->setposition(Point(350,500));	addChild(LabelTTFPlayer);	auto LabelTTFCom=LabelTTF::create("Com",40);	LabelTTFCom->setposition(Point(650,500));	addChild(LabelTTFCom);
现在我们把几个背景层建立好了,下边就是将菜单层放到我们的背景中:

        //添加所需要的精灵	auto sprite01=Sprite::create("shitou.png");	sprite01->setposition(Point(300,100));	this->addChild(sprite01);	auto sprite02=Sprite::create("jiandao.png");	sprite02->setposition(Point(500,100));	this->addChild(sprite02);	auto sprite03=Sprite::create("bu.png");	sprite03->setposition(Point(700,100));	this->addChild(sprite03);	auto sprite04=Sprite::create("Title.png");	sprite04->setposition(Point(500,550));	this->addChild(sprite04);	auto spritePlayer=Sprite::create("shitou.png");	spritePlayer->setposition(Point(350,350));	this->addChild(spritePlayer);	spritePlayer->setScale(1,1);	auto spriteComputer=Sprite::create("shitou.png");	spriteComputer->setposition(Point(650,350));	this->addChild(spriteComputer);	spriteComputer->setScale(1,1);	//加入具体的分数	LabelTTFCountscore=LabelTTF::create("0",30);	LabelTTFCountscore->setposition(Point(150,460));	addChild(LabelTTFCountscore);	LabelTTFWinscore=LabelTTF::create("0",30);	LabelTTFWinscore->setposition(Point(150,430));	addChild(LabelTTFWinscore);        LabelTTFLosescore=LabelTTF::create("0",30);	LabelTTFLosescore->setposition(Point(150,400));	addChild(LabelTTFLosescore);	LabelTTfdrawscore=LabelTTF::create("0",30);	LabelTTfdrawscore->setposition(Point(150,370));	addChild(LabelTTfdrawscore);
PS:小编我当时编写程序的时候遇到了个问题,在这里想跟大家分享一下

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

cocos2d::color4B(30,150

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

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

总结

以上是内存溢出为你收集整理的寒風的Cocos2dx之旅之剪刀、石头、布系列专题(1)全部内容,希望文章能够帮你解决寒風的Cocos2dx之旅之剪刀、石头、布系列专题(1)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1051427.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存