当角色走到股市图标时,进入股市界面。每走完一个回合,增加一条股票数据,
股市界面上半部分显示股票信息,包括代码,名称,当前价格,买入价格,涨跌百分比,角色持有的股票数量
下半部分显示股票价格走势,当点击一个股票时,显示相关股票的价格走势,共显示最新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();//创建菜单,包括买入,卖出,返回Menu
selectedTag =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;
}
这就是大体代码,详细内容,请参考代码,如下效果图
未完待续...................