cocos2dx的学习以及flappy bird制作
-做游戏的初衷
-对cocos2dx 的学习
-flappybird的着手制作
-总结感想
做游戏的初衷
做游戏的这个想法一直都有,然后也想做个自己的RPG游戏然后可以自己玩的那种
,然后因为RPG游戏的标志性就是平面2d,所以我了解到了cocos2d这个东西至于为什么后来做的并不是RPG。
只能说当时比较咸,学完之后也无从下手。只好找了个简单点的做。
对cocos2dx的学习
我暑假从图书馆借了一本书顺便对着网上的基础教程学习了cocos的一些基础语法。
例如简单的精灵,事件,层,场景等。
flappy bird 着手制作
首先整个游戏分为四个场景:菜单场景,游戏场景,积分场景,设置场景。
菜单场景制作以及准备:
主要是制作四个按钮控件,我直接将一些数据的初始化也加在此类中了
比如积分排名,然后就是音乐开关的变量。
//建立积分榜
if (UserDefault::getInstance()->getBoolForKey("exit")!=1)
{
//log("%s", UserDefault::getInstance()->getXMLFilePath());
auto userdefault = UserDefault::getInstance();
userdefault->setBoolForKey("exit", 1);
userdefault->setIntegerForKey("score1", 0);
userdefault->setIntegerForKey("score2", 0);
userdefault->setIntegerForKey("score3", 0);
userdefault->setIntegerForKey("score4", 0);
userdefault->setIntegerForKey("score5", 0);
userdefault->setBoolForKey("m_sound", 1);
userdefault->flush();
//log("restart");
}
//获取按钮
Layout* panel = (Layout*)rootNode->getChildByName("Panel");
Button* startButton = (Button*)Helper::seekWidgetByName(panel, "startButton");
Button* quitButton = (Button*)Helper::seekWidgetByName(panel, "quitButton");
Button* optionalButton = (Button*)Helper::seekWidgetByName(panel, "optionalButton");
Button* rankingButton = (Button*)Helper::seekWidgetByName(panel, "rankingButton");
//绑定事件
startButton->addTouchEventListener(CC_CALLBACK_2(GameMenu::startGame, this));
quitButton->addTouchEventListener(CC_CALLBACK_2(GameMenu::quitGame, this));
optionalButton->addTouchEventListener(CC_CALLBACK_2(GameMenu::optional, this));
rankingButton->addTouchEventListener(CC_CALLBACK_2(GameMenu::ranking, this));
CCLOG("!!");
//预加载背景音乐
if (UserDefault::getInstance()->getBoolForKey("m_sound")== 1)
{
auto audioengine = SimpleAudioEngine::getInstance();
audioengine->preloadEffect("sfx_wing.ogg");
audioengine->preloadEffect("sfx_hit.ogg");
audioengine->preloadEffect("sfx_die.ogg");
}
return true;
}
游戏场景制作
游戏场景的制作稍微麻烦一点。
首先构建场地,增加一个地面以及顶端边缘(采用物理引擎库创建刚体等。。)
然后构建一个咸鱼精灵,同上面的步骤;然后再增加一个单点触控事件。然后就是基本上差不多的步骤构建这个柱子。相关的一些大小参数我都是直接参考的教程;然后添加了一个积分分数随时间增加。再就是一个鸡肋的暂停键。死亡之后就出现本次得分
以下贴上背景的代码
void HelloWorld::addLand()
{
Size winSize2 = Director::getInstance()->getWinSize();
auto landSprite = Sprite::create("land.png");
auto landOriginalSize = landSprite->getContentSize();
auto landSize = Size(540, 355);
landSprite->setAnchorPoint(Vec2(0, 0));
landSprite->setContentSize(landSize);
landSprite->setScale(landSize.width / landOriginalSize.width, landSize.height / landOriginalSize.height);
landSprite->setPosition(Vec2(0, 0));
rootNode->addChild(landSprite,1);
b2BodyDef landDef;
landDef.type = b2_staticBody;
landDef.position = b2Vec2(winSize.width/2/RATIO, landSize.height/2/RATIO);//定义物体属性
b2Body* landBody = world->CreateBody(&landDef);//创建刚体
b2PolygonShape landShape;
landShape.SetAsBox(landSize.width / 2 / RATIO, landSize.height / 2 / RATIO);//定义形状,实心,接收半宽半高
b2FixtureDef landFixtureDef;
landFixtureDef.shape = &landShape;//定义夹具属性
landBody->CreateFixture(&landFixtureDef);//创建物体
//增加顶层边缘
b2BodyDef topDef;
topDef.type = b2_staticBody;
topDef.position=b2Vec2(winSize.width/2/RATIO,(winSize.height+50)/RATIO);
b2Body* topBody = world->CreateBody(&topDef);
b2PolygonShape topShape;
topShape.SetAsBox(winSize.width / 2 / RATIO, 2 / RATIO);
b2FixtureDef topFixtureDef;
topFixtureDef.shape = &topShape;
topBody->CreateFixture(&topFixtureDef);
}
设置场景以及高分榜场景制作
就很简单加个背景然后将存在本地的数据调出并显示就行。