当角色走到股市图标时,进入股市界面。每走完一个回合,增加一条股票数据,
股市界面上半部分显示股票信息,包括代码,名称,当前价格,买入价格,涨跌百分比,角色持有的股票数量
下半部分显示股票价格走势,当点击一个股票时,显示相关股票的价格走势,共显示最新14条的价格走势。

每次点击购买,买入100股 。点击卖出,则卖出所持有的该股的所有股票。成交价格 等信息动态更新
点击返回,返回到游戏主界面,并更新角色资金值

1、首先添加股票类
包括代码,名称,买入价格,涨跌百分比,持仓数量等定义以及相关的get 、set方法

class Stock : public Sprite
{
................
private:
<span style="white-space:pre"> </span>int stockCode; //股票代码String* stockName;//股票名称int nowPrice;//当前价格int makedealprice;//成交价格float percent;//涨跌百分比int storeNumber;//持仓数量
};

2、VisibleRect类,主要是为了方便的获取屏幕的尺寸等信息

class VisibleRect
{
public:static cocos2d::Rect getVisibleRect();
...................
private:static void lazyInit();static cocos2d::Rect s_visibleRect;
};

3、股票记录中的单元格类StockCellCard,包括单元格背景和显示文字的label。同彩票类card大体相同,不再解释了

4、在RicherPlayer的init方法中,给角色创建股票持有信息,主要是股票代码和持仓数量和买入价格。其他信息无关紧要

bool RicherPlayer::init(char* name, int tag,bool enemy,int money,int strength)
{
...........stockMap.insert(0,Stock::create(800100,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,10,0,100));stockMap.insert(1,Stock::create(800200,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,20,0,200));stockMap.insert(2,Stock::create(800300,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,70,0,800));stockMap.insert(3,Stock::create(800400,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,10,0,400));stockMap.insert(4,Stock::create(800500,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,0,0,0));..........
}

5、当角色走到股票图标时RicherGameController 控制器,发送进入股市的消息

  if(passId == GameBaseScene::stock_tiledID){String * str = String::createWithFormat("%d-%f-%f-%d-%d",MSG_STOCK_TAG,1.0f,1.0f,_richerPlayer->getTag(),MOVEPASS);NotificationCenter::getInstance()->postNotification(MSG_STOCK,str);return;}

6、GameBaseScene类定义了股票容器类,存放所以股票相关信息

vector<float> GameBaseScene::stock_pointvec1;
vector<float> GameBaseScene::stock_pointvec2;
vector<float> GameBaseScene::stock_pointvec3;
vector<float> GameBaseScene::stock_pointvec4;
vector<float> GameBaseScene::stock_pointvec5;

在更新回合计数时,调用updateStockVec()方法,添加股票一条新纪录
void GameBaseScene::refreshRoundDisplay()
{
..........
updateStockVec()
.......

}

//最多纪录14条纪录,超过的会被覆盖掉
void GameBaseScene::updateStockVec()
{float valule1 = rand()%800+10;float valule2 = rand()%800+10;float valule3 = rand()%800+10;float valule4 = rand()%800+10;float valule5 = rand()%800+10;if(stock_pointvec1.size()>13){for(int i=0;i<13;i++){stock_pointvec1.at(i)=stock_pointvec1.at(i+1);stock_pointvec2.at(i)=stock_pointvec2.at(i+1);stock_pointvec3.at(i)=stock_pointvec3.at(i+1);stock_pointvec4.at(i)=stock_pointvec4.at(i+1);stock_pointvec5.at(i)=stock_pointvec5.at(i+1);}stock_pointvec1.at(13) =valule1;stock_pointvec2.at(13) =valule2;stock_pointvec3.at(13) =valule3;stock_pointvec4.at(13) =valule4;stock_pointvec5.at(13) =valule5;}else{stock_pointvec1.push_back(valule1);stock_pointvec2.push_back(valule2);stock_pointvec3.push_back(valule3);stock_pointvec4.push_back(valule4);stock_pointvec5.push_back(valule5);}
}    

7、当GameBaseScene收到进入股市界面的消息时,添加股市layer,显示股市

 case MSG_STOCK_TAG:{auto lineView = LineChart::createChart(player1);   lineView->setPosition(Point(0, 0));moveTag = messageVector.at(4)->intValue();lineView->moveTag = moveTag;                addChild(lineView);..............break;}    

8、LineChart类是股票界面类

(1)initChart方法

bool LineChart::initChart(RicherPlayer* player)
{if ( !LayerColor::initWithColor(Color4B(0,0,0,255))) //黑色背景{return false; }richerPlayer = player; //角色playerStockMap = player->stockMap;//角色持有的股票容器initStockVector(playerStockMap);drawNode = DrawNode::create();//创建DrawNode ,准备画图this->addChild(drawNode);tv = TableView::create(this, Size(650, 160));//创建TableView对象tv->setAnchorPoint(Point(0, 0));tv->setPosition(10, VisibleRect::getVisibleRect().size.height * 1 /2);  tv->setDelegate(this);addChild(tv);initMenu();//创建菜单,包括买入,卖出,返回MenuselectedTag =0;  float tableY =  VisibleRect::getVisibleRect().size.height * 1/2;//选择股票时,箭头移动到相关股票arrowSprite_left->setPosition(600+arrowSprite_left->getContentSize().width,tableY +selectedTag*32);arrowSprite_right->setPosition(10,tableY + selectedTag*32);setData(getsock_pointVec(selectedTag));//设置股票数据drawpic();//画走势图return true;
}

(2)initMenu方法创建菜单,包括买入,卖出,返回Menu,以及箭头提示

void LineChart::initMenu()
{Menu* menu = Menu::create();menu->setPosition(CCPointZero);setMenu(menu);MenuItemImage* buyMenuItemButton = MenuItemImage::create("images/buy_normal.png", "images/buy_pressed.png", this, menu_selector(LineChart::buttonCallback));buyMenuItemButton->setPosition(ccp(700,VisibleRect::getVisibleRect().size.height-110));buyMenuItemButton->setAnchorPoint(ccp(0,0));buyMenuItemButton->setTag(buy_button);menu->addChild(buyMenuItemButton);............arrowSprite_left = Sprite::create("images/arrow_left.png");arrowSprite_left->setPosition(ccp(-500,-500));arrowSprite_left->setAnchorPoint(ccp(0,0));addChild(arrowSprite_left);arrowSprite_right = Sprite::create("images/arrow_right.png");arrowSprite_right->setPosition(ccp(-500,-500));arrowSprite_right->setAnchorPoint(ccp(0,0));addChild(arrowSprite_right);
}

(3)initStockVector()添加股票上半部分表格的标题,以及给股票容器添加各个股票信息

void LineChart::initStockVector(Map<int,Stock*> stockMap)
{float percent = 0;if(GameBaseScene::stock_pointvec1.size()>1){percent = (GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-1) - GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-2))/GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-2)*100;}stockVector.pushBack(Stock::create(800100,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-1),stockMap.at(0)->getMakedealprice(),percent,stockMap.at(0)->getStoreNumber()));..................Label* code = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_CODE)->getCString(),"", 20);code->setPosition(Point(20, 410 ));code->setAnchorPoint(ccp(0,0));addChild(code);Label* name = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_NAME)->getCString(),"", 20);name->setPosition(Point(stockCellWidth+20, 410 ));name->setAnchorPoint(ccp(0,0));addChild(name);Label* nowprice = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_NOWPRICE)->getCString(),"", 20);nowprice->setPosition(Point(stockCellWidth*2+20, 410 ));nowprice->setAnchorPoint(ccp(0,0));addChild(nowprice);Label* dealprice = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_DEALPRICE)->getCString(),"", 20);dealprice->setPosition(Point(stockCellWidth*3+20, 410 ));dealprice->setAnchorPoint(ccp(0,0));addChild(dealprice);Label* percentLabel = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_PERCENT)->getCString(),"", 20);percentLabel->setPosition(Point(stockCellWidth*4+20, 410 ));percentLabel->setAnchorPoint(ccp(0,0));addChild(percentLabel);Label* store = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_STORE)->getCString(),"", 20);store->setPosition(Point(540, 410 ));store->setAnchorPoint(ccp(0,0));addChild(store);playerMoneyLabel = Label::createWithSystemFont(String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(PLAYER_MONEY)->getCString(),richerPlayer->getMoney())->getCString(),"", 20);playerMoneyLabel->setPosition(Point(20, 450 ));playerMoneyLabel->setAnchorPoint(ccp(0,0));addChild(playerMoneyLabel);}

(4)buttonCallback(),点击买入 ,卖出,返回的回调方法
 void LineChart::buttonCallback(CCObject* pSender)
 {
.............

}

(5)TableView 相关实现方法

//当点击股票时,移动箭头,更新数据,重画走势图
void LineChart::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell)
{for(int i=0;i<30;i++){this->removeChildByTag(100+i);}int tag = cell->getTag();selectedTag =tag;log("******click id = %d",tag);float height = VisibleRect::getVisibleRect().size.height;float tableY =  VisibleRect::getVisibleRect().size.height * 1/2;arrowSprite_left->setPosition(600+arrowSprite_left->getContentSize().width,tableY +tag*32);arrowSprite_right->setPosition(10,tableY + tag*32);log("all height is %f",height);log("all cellY is %f",tableY);setData(getsock_pointVec(tag));drawpic();}
//创建tableview相关单元格,当股市上涨时,背景色为红色,下跌时为绿色
TableViewCell* LineChart::tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx)
{TableViewCell *cell = table->dequeueCell();  LabelTTF *label;int colorTag = 0;if(stockVector.at(idx)->getPercent()>0){colorTag = 1;}else{colorTag = -1;}if (cell==NULL) {       cell = TableViewCell::create();cell->setTag(idx);  for(int i=0; i<6; i++)    {  switch(i){case 0:{StockCellCard* card = StockCellCard::createCardSprite(String::createWithFormat("%d",stockVector.at(idx)->getCode()), stockCellWidth, stockCellHeight, stockCellWidth*i+10, 0,colorTag);    cell->addChild(card);break;}case 1:{StockCellCard* card = StockCellCard::createCardSprite(stockVector.at(idx)->getStockName(), stockCellWidth, stockCellHeight, stockCellWidth*i+10, 0,colorTag);    cell->addChild(card);break;}
....................................}} }return cell;
}

//共5支股票
ssize_t LineChart::numberOfCellsInTableView(cocos2d::extension::TableView *table){
    return 5;
}

(6)画走势图

void LineChart::drawpic()
{drawNode->clear();int maxValue = getMaxValue(pointvec);int maxValue2 = int((maxValue+100) / 100)* 100 ;maxValue1 = maxValue2  / 10;spaceRatio = 0.08f;  //y轴间距系数leftRatioX = 0.1f;   //x轴左侧间距系数int fontSize = 20;string fontName = StringUtils::format("Thonburi");Size layerSize = Size(VisibleRect::getVisibleRect().size.width, VisibleRect::getVisibleRect().size.height * 1 /2);layerHeight1 = 30;float layerHeight = layerHeight1;float layerWidth = layerSize.width;int count = layerSize.width /50;/***********************画xy轴标签*************************************/for (int i = 0; i < 11; i++) {Point bPoint = Point(layerWidth* leftRatioX, layerHeight );Point ePoint = Point(layerWidth* leftRatioX+(count-2) * 50, layerHeight );Label* label = Label::createWithSystemFont(StringUtils::format("%d",maxValue1* i).c_str(), fontName.c_str(), fontSize);       label->setPosition(Point(layerWidth* 0.05f, layerHeight ));label->setTag(100+i);addChild(label);      drawNode->drawSegment(bPoint, ePoint, 0.5,Color4F(100, 100, 200, 200));      layerHeight += layerSize.height * spaceRatio;         }float layer_wd = layerSize.width * leftRatioX;for (int i = 0; i < count; i++) {Point bPoint  = Point(layer_wd, layerHeight1);        Point ePoint  = Point(layer_wd, layerSize.height * spaceRatio*10+layerHeight1); if(i%2 == 0){drawNode->drawSegment(bPoint,ePoint,0.5,Color4F(100, 100, 200, 200));}auto labelX = Label::createWithSystemFont(StringUtils::format("%d",i).c_str(), "Thonburi", 20);labelX->setPosition(Point(ePoint.x, 0));labelX->setAnchorPoint(ccp(0,0));labelX->setTag(100+11+i);this->addChild(labelX);layer_wd += 50;}drawLine(pointvec, Color4B(0, 255, 255, 255),Color4B(255, 0, 255, 255));
}

//画走势线条

void LineChart::drawLine(vector<Point> vec,Color4B lineColor,Color4B dotColor)
{Size layerSize = Size(VisibleRect::getVisibleRect().size.width, VisibleRect::getVisibleRect().size.height * 1 /2);float layerWidth = layerSize.width;float tempWidth = layerSize.height * spaceRatio;float tempWidth2 = 0;float tempHeight1 = maxValue1  ;   double  ratio = tempWidth/tempHeight1;/**********************开始画线**********************/std::vector<Point>::iterator beforePoint;std::vector<Point>::iterator currentPoint;beforePoint = vec.begin();for (currentPoint = vec.begin() + 1;currentPoint != vec.end() ; currentPoint++) {Point bPoint  = *beforePoint;bPoint = Point(bPoint.x + layerWidth* leftRatioX, bPoint.y * ratio + layerHeight1 +tempWidth2);Point ePoint  = *currentPoint;ePoint = Point(ePoint.x + layerWidth* leftRatioX, ePoint.y * ratio + layerHeight1 +tempWidth2);drawNode->drawSegment(bPoint, ePoint, 0.8,Color4F::RED);beforePoint = currentPoint;}/**********************结束画线**********************/   /********************开始画点**********************************************/beforePoint = vec.begin();DrawPrimitives::setDrawColor4B(dotColor.r, dotColor.g, dotColor.b, dotColor.a);Point bPoint  = *beforePoint;bPoint = Point(bPoint.x +layerWidth* leftRatioX, bPoint.y * ratio + layerHeight1 +tempWidth2);drawNode->drawDot(bPoint, 5, Color4F::YELLOW); int i = 2;for (currentPoint = vec.begin() + 1;currentPoint != vec.end() ; currentPoint++) {Point ePoint  = *currentPoint;    ePoint = Point(ePoint.x + layerWidth* leftRatioX, ePoint.y * ratio + layerHeight1 + tempWidth2);              drawNode->drawDot(ePoint, 5, Color4F::YELLOW); i++;}/********************结束画点*********************************************END**/}

//设置股票数据

void LineChart::setData(vector<float> data)
{pointvec.clear();vector<float>::iterator it;int i = 0;for (it = data.begin();it != data.end();it++) {float f = *it;pointvec.push_back(Point(50 * (i+1), f));               i++;}}
//获取最大值
double LineChart::getMaxValue(std::vector<Point> vec)
{double maxY =1;for (int i = 0; i < vec.size(); i++){float num = vec.at(i).y;if (maxY < abs(num)) {maxY = abs(num);}}return maxY;
}

这就是大体代码,详细内容,请参考代码,如下效果图

点击下载代码

未完待续...................

Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市相关推荐

  1. Cocos2d-x 3.2 大富翁游戏项目开发-第二十六部分 人物技能

    本节主要是添加人物技能,技能包括 暴风骤雨:此技能可以把地块变成空白地块, 随心步:  选择想走的步数,想走几步走几步 巧取豪夺:把对手的土地变成自己的 技能包含的信息:等级 和 耗费体力  等级共5 ...

  2. Cocos2d-x 3.2 大富翁游戏项目开发-第二十八部分 游戏保存和载入存档游戏

    1.游戏保存 如图,在右下角增加保存图标,点击后进行游戏的保存. 游戏保存采用json格式,具体如下: {"map_level":2, // 游戏关卡"gameRound ...

  3. Cocos2d-x 3.2 大富翁游戏项目开发-第二十部分 螃蟹挡路(code)

    该部分我们添加螃蟹伤人事件,道路位置随机添加螃蟹精灵,当角色行走完毕如果停留位置碰到了螃蟹,首先播放伤人动画,然后是播放救护车把角色带走动画. 如果轮流到该角色行走时,吐司提示住院还有几天,当住院天数 ...

  4. Cocos2d-x 3.2 大富翁游戏项目开发-第二十四部分 彩票开奖

    每隔N个回合,彩票开奖一次,每期开奖奖金固定5万,暂不累积.摇奖效果一般,以后考虑用物理引擎实现 1.定义彩票开奖类 bool LotteryPublish::init() {addItemSprit ...

  5. Android UI开发第二十五篇——分享一篇自定义的 Action Bar

    Action Bar是android3.0以后才引入的,主要是替代3.0以前的menu和tittle bar.在3.0之前是不能使用Action Bar功能的.这里引入了自定义的Action Bar, ...

  6. Cocos2d-x 3.2 大富翁游戏项目开发-第二十三部分 购买彩票

    当角色路过彩票的标志或者停留位置有彩票标志时,弹出购买彩票的对话框,提示购买彩票,已经买过的号码,不显示.当机器对手路过时则直接购买彩票. 1. 在RicherPlayer.h中增加std::vect ...

  7. Cocos2d-x 3.2 大富翁游戏项目开发-第十八部分 问号随机事件

    角色走完要求的步数后,先查看停留位置是否有问号,如果有,先处理问号事件,处理完毕后,再处理相邻周边上下左右地块问题,购买.升级或缴纳过路费. 问号随机事件主要是如下事件: 政府鼓励投资,返还税金100 ...

  8. 【正点原子STM32连载】 第二十五章 TFTLCD(MCU屏)实验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1

    1)实验平台:正点原子MiniPro H750开发板 2)平台购买地址:https://detail.tmall.com/item.htm?id=677017430560 3)全套实验源码+手册+视频 ...

  9. 【正点原子MP157连载】第二十五章 pinctrl和gpio子系统实验-摘自【正点原子】STM32MP1嵌入式Linux驱动开发指南V1.7

    1)实验平台:正点原子STM32MP157开发板 2)购买链接:https://item.taobao.com/item.htm?&id=629270721801 3)全套实验源码+手册+视频 ...

最新文章

  1. Java Script 第四节课 Java Script的隐式转换
  2. 一文总览机器学习中各种【熵】的含义及本质
  3. 前端面试经历(持续更新)
  4. OpenCV imgproc直方图的实例(附完整代码)
  5. Maven 搭建多模块企业级项目
  6. 从RSS Feed和YQL创建数据表
  7. JavaScript触发事件大全--能力工场
  8. CSS基础必备知识点03
  9. Portainer复制Docker容器
  10. java fragment_初步认识Fragment 之一 编写简单的fragment代码
  11. codeforces 111A/112C Petya and Inequiations
  12. 轻量化网络:SqueezeNext
  13. 每个电脑都会自己的SID号和GUID号,而且不会相同。。
  14. 代码chaid_R或Python中的CHAID决策树
  15. LeetCode 68. Text Justification
  16. 【GD32F310开发板试用】gcc启动文件制作
  17. 【HTML5】字体删除线代码
  18. NOIP 2005 篝火晚会
  19. mac home/end/pageup/pageDown
  20. rtthread 线程

热门文章

  1. 计算机开机引导过程的结果是,计算机开机的引导过程是怎么样的,学会了能解决大部分的开机问题...
  2. oracle10行转列,【转】oracle 10g 行列转换的写法
  3. 数据结构(C语言)中双向链表的定义及基本操作
  4. Android欢迎页面动画
  5. dem4j对xml进行解析
  6. 魅族-魅蓝note无法链接adb解决办法
  7. 教你用matlab制作一款黄金矿工小游戏
  8. qt 如何让窗口置顶
  9. 教导,职业经理人最重要的能力
  10. 零基础如何学好python?