标签:cocos2dx cocos2dx3.0 cocos2dx3.1 超级玛丽
学完cocos2dx课程的第一个项目选择了超级玛丽。可以说有自己的想法,简单但是确实不简单。
我花了一天把一份2.1版本的超级玛丽源码升级到了3.0,改改删删,参考那个源码虽然好多不懂,但是马虎升级成功,游戏正常玩耍。
本着不为把游戏做出来而写代码的想法,罗列了一下这个游戏可以使用到的知识点。数据持久化的三种方式、loading页面、tmx地图解析、cocosStudio场景、屏幕适配、关卡如何选择、代码结构的优化(各种类的抽象继承),在基本功能出来后可以自己去设计变态关卡等。
两天实现了loading界面、 主界面所有场景 和选关场景。
效果如下gif:
loadProgress = ProgressTimer::create(Sprite::create("image/loadingbar.png")); loadProgress->setBarChangeRate(Point(1, 0));//设置进程条的变化速率 loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型 loadProgress->setMidpoint(Point(0, 1));//设置进度的运动方向 loadProgress->setPosition(visibleSize.width / 2, visibleSize.height / 2 ); loadProgress->setPercentage(progressPercent); this->addChild(loadProgress);
void LoadingScene::preloadResource() { //这里分开写测试, 后期如果确定是一个场景中的直接使用plist加载 std::string resouceMain = "image/mainscene/"; float count = 20;//一共加载十七张 everyAdd = 100 / count; Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"about_normal.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"about_select.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"backA.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"backB.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"background.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"bg.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"music_off.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"music_on.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"quitgame_normal.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"quitgame_select.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"Set_Music.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"Setting_n.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"setting_s.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"startgame_normal.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"startgame_select.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"wxb.jpg", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"zhy.jpg", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain + "sound_effect_off.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain + "sound_effect_on.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); Director::getInstance()->getTextureCache()->addImageAsync(resouceMain + "switchBg.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this)); }
void LoadingScene::loadingCallback(Texture2D*) { progessAdd(); } void LoadingScene::progessAdd() { progressPercent += everyAdd; if (100-progressPercent <everyAdd) { progressPercent = 100; auto scene = MainScene::createScene(); Director::getInstance()->replaceScene(TransitionFade::create(1, scene)); } percentLabel->setString(StringUtils::format("%d", int(progressPercent)));//更新percentLabel的值 loadProgress->setPercentage(progressPercent); }在loading到100%的时候切换场景。
tableView = TableView::create(this, Size(spWidth, spHeight)); tableView->setDirection(ScrollView::Direction::HORIZONTAL); tableView->setPosition(Point((winSize.width - spWidth)/2, (winSize.height-spHeight)/2)); tableView->setDelegate(this); this->addChild(tableView); tableView->reloadData();
void SelectLevel::tableCellTouched(TableView* table, TableViewCell* cell) { CCLOG("cell touched at index: %ld", cell->getIdx()); } Size SelectLevel::tableCellSizeForIndex(TableView *table, ssize_t idx) { return Director::getInstance()->getVisibleSize(); } TableViewCell* SelectLevel::tableCellAtIndex(TableView *table, ssize_t idx) { auto string = String::createWithFormat("%ld", idx+1); TableViewCell *cell = table->dequeueCell(); if (!cell) { cell = new TableViewCell(); cell->autorelease(); auto sprite = Sprite::create(StringUtils::format("image/level/select%d.jpg", idx)); sprite->setAnchorPoint(Point::ZERO); sprite->setPosition(Point(0, 0)); cell->addChild(sprite); auto label = Label::createWithSystemFont(string->getCString(), "Helvetica", 20.0); label->setPosition(Point::ZERO); label->setAnchorPoint(Point::ZERO); label->setTag(123); cell->addChild(label); } else { auto label = (Label*)cell->getChildByTag(123); label->setString(string->getCString()); } return cell; } ssize_t SelectLevel::numberOfCellsInTableView(TableView *table) { return Global::getInstance()->getTotalLevels(); }
void SelectLevel::pageViewEvent(Ref *pSender, PageViewEventType type) { }千万不要使用:
void SelectLevel::pageViewEvent(Ref *pSender) { }后面这种写法编译不报错,但是运行的时候报错崩溃。
pageView = PageView::create(); pageView->setSize(Size(winSize.width, winSize.height)); pageView->setPosition(Point(0,0)); for (int i = 1; i < Global::getInstance()->getTotalLevels(); i++) { Layout* layout = Layout::create(); layout->setSize(Size(winSize.width, winSize.height)); ImageView* imageView = ImageView::create(StringUtils::format("image/level/select%d.jpg", i)); imageView->setScale9Enabled(true); imageView->setSize(Size(spWidth, spHeight)); imageView->setPosition(Point(layout->getSize().width / 2.0f, layout->getSize().height / 2.0f)); layout->addChild(imageView); Text* label = Text::create(StringUtils::format("page %d", i), "fonts/Marker Felt.ttf", 30); label->setColor(Color3B(192, 192, 192)); label->setPosition(Point(layout->getSize().width / 2.0f, layout->getSize().height / 2.0f)); layout->addChild(label); pageView->addPage(layout); } pageView->addEventListenerPageView(this, pagevieweventselector(SelectLevel::pageViewEvent)); this->addChild(pageView);
auto layerr = Layer::create(); layerr->setContentSize(Size(spWidth, spHeight)); layerr->setPosition(size.width/2, size.height/2); layerr->setZOrder(111); this->addChild(layerr); auto listenTouch = EventListenerTouchOneByOne::create(); listenTouch->onTouchBegan = CC_CALLBACK_2(SelectLevel::onTouchBegan, this); //listenTouch->onTouchMoved = CC_CALLBACK_2(SelectLevel::onTouchMoved, this); listenTouch->onTouchEnded = CC_CALLBACK_2(SelectLevel::onTouchEnded, this); //listenTouch->onTouchCancelled = CC_CALLBACK_2(SelectLevel::onTouchCancelled, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenTouch, layerr);其实是做好触摸回调:
bool SelectLevel::onTouchBegan(Touch* touch, Event* event) { clickBeginPoint = touch->getLocation(); return true; } void SelectLevel::onTouchEnded(Touch* touch, Event* event) { int dragDistance = abs(touch->getLocation().x - clickBeginPoint.x); //如果单击超过5像素 切换场景 if (dragDistance < 5 ) { Rect layerRect = Rect((winSize.width - spWidth) / 2, (winSize.height - spHeight) / 2, spWidth, spHeight); if (layerRect.containsPoint(touch->getLocation())) { //要切换的关 int level = pageView->getCurPageIndex()+1; } } }
cocos2dx3.0 超级马里奥开发笔记(一)——loadingbar和pageview的使用,布布扣,bubuko.com
cocos2dx3.0 超级马里奥开发笔记(一)——loadingbar和pageview的使用
标签:cocos2dx cocos2dx3.0 cocos2dx3.1 超级玛丽
原文地址:http://blog.csdn.net/hanbingfengying/article/details/37606467