学生信息管理系统(cocos2d引擎)——数据结构课程设计
老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!!
给我的感想就是全都是指针!
添加图片精灵:
CCSprite* spBG = CCSprite::create("");
this->addChild(spBG); //添加到当前层
spGB->setPosition(ccp(x,y)); //设置坐标
spGB->setScale(0.5); //缩放
文字按钮:
CCMenuItemFont* pItem = CCMenuItemFont::create("wenzi",this,menu_selector(回调函数));
再将文字按钮添加到菜单中
CCMenu* menu = CCMen::create(pItem,NULL);
this->addChild(menu);
menu->setPosition(ccp(x,y));
//创建文本
CCLabelTTF* ttf = CCLabelTTF::create("wenben","字体",48.0f);
this->addChild(ttf);
ttf->setPosition(cco(x,y));
//创建输入框(1、九点图,2、输入框)
CCScale9Sprite* input = CCScale9Sprite::create("");
accountBox = CCEditBox::create(CCSizeMake(r,c),input);
this->addChild(accounBox);
accountBox->setPosition(ccp(x,y));
accountBox->setInputFlag(kEditBoxInputFlagPassword); //输入模式为密码输入
按钮回调函数,看输入框accountBox的内容是否是密码;
不是密码:
弹出输入框 CCMessageBox("","");
是密码切换场景
CCDirector* director = CCDirector::sharedDirector();
director->replaceScene(ListViewLayer::scene()); //另一个头文件,里面的一个场景;
接下来,和之前的一样了;无非就是一些按钮,文本,输入框。
在init()里面画好面板;
各种按钮里面的回调函数。
然后中文转码utf-8老师已经写好了;
贴上自己写的东西,其他的老师已经写好了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef _Students_H_ 2 #define _Students_H_ 3 #include <iostream> 4 using namespace std; 5 class Students 6 { 7 public: 8 int m_iNumber; 9 char m_strName[32]; 10 char m_sex; 11 public: 12 Students(int iNumber, char* strName,char sex); 13 }; 14 #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "Students.h" 2 3 4 Students::Students(int iNumber, char* strName,char sex) 5 { 6 m_iNumber = iNumber; 7 strcpy(m_strName, strName); 8 m_sex = sex; 9 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef _Manager_H_ 2 #define _Manager_H_ 3 #include <iostream> 4 using namespace std; 5 #include <vector> 6 #include "Students.h" 7 class Manager 8 { 9 private: 10 Manager(); 11 public: 12 static Manager* m_pStance; 13 static Manager* getInstance(); 14 15 // 管理学生容器 16 vector<Students*> _vStudent; 17 }; 18 19 #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "Manager.h" 2 3 Manager* Manager::m_pStance = NULL; 4 Manager* Manager::getInstance() 5 { 6 if(m_pStance == NULL) 7 { 8 m_pStance = new Manager; 9 } 10 return m_pStance; 11 } 12 13 Manager::Manager() 14 { 15 Students* s = new Students(1000,"TreeDream",'F'); 16 _vStudent.push_back(s); 17 s = new Students(1001,"马里奥", 'M'); 18 _vStudent.push_back(s); 19 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef __TABLEVIEWTESTSCENE_H__ 2 #define __TABLEVIEWTESTSCENE_H__ 3 4 #include "cocos2d.h" 5 #include "cocos-ext.h" 6 USING_NS_CC; 7 USING_NS_CC_EXT; 8 9 #include "ChineseString.h" 10 #include "Manager.h" 11 12 //需要实现CCTabelViewDelegate和CCTabelViewDataSource这两个接口 13 class ListViewLayer : public CCLayer, 14 public CCTableViewDataSource, 15 public CCTableViewDelegate 16 { 17 private: 18 CCTableView* pTableView; 19 vector<Students*>::iterator it; 20 21 CCLabelTTF* ttf; //提示信息 22 23 // 添加对话框需要的UI 24 CCLayerColor* layerBg; 25 CCEditBox* edit_id; 26 CCEditBox* edit_name; 27 CCEditBox* edit_sex; 28 CCMenu* menu_add_done; 29 // 添加按钮回调函数 30 void AddCallBack(CCObject* obj); 31 // 添加按钮回调函数 32 void AddDoneCallBack(CCObject* obj); 33 // 添加按钮回调函数 34 void AddDel(CCObject* obj); 35 void DelDoneCallBack(CCObject* obj); 36 37 void Oride(CCObject* obj); 38 void OrideCallBack(CCObject* obj); 39 void OrideInf(CCObject* obj); 40 41 42 void Searchs(CCObject* obj); 43 void searchsInf(CCObject* obj); 44 45 void MySort (); 46 47 public: 48 static cocos2d::CCScene* scene(); 49 // 初始化层 50 virtual bool init(); 51 52 /*因为CCTableView继承自CCScrollView所以要实现这俩个方法,里边一般都不写东西*/ 53 virtual void scrollViewDidScroll(CCScrollView* view); 54 virtual void scrollViewDidZoom(CCScrollView* view); 55 56 /*CCTabelViewDelegate类的虚函数*/ 57 //处理触摸点击事件 58 virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell); 59 60 /*CCTabelViewDataSource类的虚函数*/ 61 //每一项的宽度和高度 62 virtual CCSize cellSizeForTable(CCTableView *table); 63 //生成列表每一项的内容 64 virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx); 65 //一共多少项 66 virtual unsigned int numberOfCellsInTableView(CCTableView *table); 67 68 // 创建层 69 CREATE_FUNC(ListViewLayer); 70 }; 71 72 #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "ListViewLayer.h" 2 3 CCScene* ListViewLayer::scene() 4 { 5 CCScene *scene = CCScene::create(); 6 ListViewLayer *layer = ListViewLayer::create(); 7 scene->addChild(layer); 8 return scene; 9 } 10 11 bool ListViewLayer::init() 12 { 13 // 添加背景 14 CCSprite* spBg = CCSprite::create("HelloWorld.jpg"); 15 this->addChild(spBg); 16 spBg->setPosition(ccp(1240/2, 640/2)); 17 // 放大 18 spBg->setScale(1.5f); 19 20 bool bRet = false; 21 do 22 { 23 CC_BREAK_IF( !CCLayer::init() ); 24 25 // 创建对话框(参数:窗口大小) 26 pTableView = CCTableView::create(this, CCSizeMake(960, 600)); 27 // 设置表格方向(垂直) 28 pTableView->setDirection(kCCScrollViewDirectionVertical); 29 // 设置坐标 30 pTableView->setPosition(CCPointZero); 31 // 设置delegate代理 32 pTableView->setDelegate(this); 33 // 设置起点(从上往下) 34 pTableView->setVerticalFillOrder(kCCTableViewFillTopDown); 35 // 添加到当前层 36 this->addChild(pTableView); 37 // 必须加,意思是用现有的配置去刷新所有的cell 38 pTableView->reloadData(); 39 40 41 bRet = true; 42 }while(0); 43 44 // 添加按钮 45 CCMenuItemImage* item_add = CCMenuItemImage::create( 46 "CloseNormal.png", 47 "CloseSelected.png", 48 this, 49 menu_selector(ListViewLayer::AddCallBack)); 50 // 添加菜单 51 52 CCMenuItemImage* item_del = CCMenuItemImage::create( 53 "del.png", 54 "CloseSelected.png", 55 this, 56 menu_selector(ListViewLayer::AddDel)); 57 58 CCMenuItemImage*item_oride = CCMenuItemImage::create( 59 "oride.png", 60 "CloseSelected.png", 61 this, 62 menu_selector(ListViewLayer::Oride)); 63 64 CCMenuItemImage* item_search = CCMenuItemImage::create( 65 "search.png", 66 "search.png", 67 this, 68 menu_selector(ListViewLayer::Searchs)); 69 70 CCMenu* menu_add = CCMenu::create(item_add,item_del,item_oride,item_search,NULL); 71 item_del->setPositionY(50); 72 item_oride->setPositionY(100); 73 item_search->setPositionY(150); 74 this->addChild(menu_add); 75 menu_add->setPosition(ccp(700,300)); 76 77 return true; 78 } 79 80 void ListViewLayer::Searchs(CCObject* obj) { 81 82 // 添加半透明的背景 83 layerBg = CCLayerColor::create(ccc4(0,0,0,127)); 84 this->addChild(layerBg); 85 86 // 1. 创建点9图 87 CCScale9Sprite* input = CCScale9Sprite::create("input.png"); 88 // 2. 创建输入框 89 edit_id = CCEditBox::create( 90 CCSizeMake(220,40), 91 input); 92 // 3.添加当前层 93 this->addChild(edit_id); 94 // 4.设置坐标 95 edit_id->setPosition(ccp(450,300)); 96 edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入姓名:")); 97 98 // 完成添加菜单项 99 CCMenuItemFont* item_add_done = CCMenuItemFont::create( 100 ChineseString::GBKToUTF8("查询"), 101 this, 102 menu_selector(ListViewLayer::searchsInf)); 103 menu_add_done = CCMenu::create(item_add_done,NULL); 104 this->addChild(menu_add_done); 105 menu_add_done->setPosition(ccp(450,50)); 106 107 } 108 109 void ListViewLayer::searchsInf(CCObject* obj) { 110 111 112 char* name = (char*)ChineseString::UTF8ToGBK(edit_id->getText()); 113 114 this->removeChild(ttf); 115 this->removeChild(edit_id); 116 this->removeChild(layerBg); 117 this->removeChild(menu_add_done); 118 119 bool flag = false; 120 Students* sTemp; 121 for(it = Manager::getInstance()->_vStudent.begin();it!=Manager::getInstance()->_vStudent.end();it++) { 122 sTemp = *it; 123 if(strcmp(name,sTemp->m_strName)==0) { 124 // 显示详细内容 125 ttf = CCLabelTTF::create( 126 ChineseString::GBKToUTF8(CCString::createWithFormat 127 ("%d %s %c", 128 sTemp->m_iNumber, 129 sTemp->m_strName, 130 sTemp->m_sex)->getCString()), 131 "Arial", 132 24); 133 this->addChild(ttf); 134 ttf->setPosition(ccp(100,100)); 135 flag = true; 136 break; 137 } 138 } 139 if(flag==false) { 140 // 显示详细内容 141 this->removeChild(ttf); 142 ttf = CCLabelTTF::create( 143 ChineseString::GBKToUTF8(CCString::createWithFormat 144 ("%s %s",name,"不存在")->getCString()), 145 "Arial", 146 24); 147 this->addChild(ttf); 148 ttf->setPosition(ccp(100,100)); 149 } 150 } 151 152 //产生学号检查 153 void ListViewLayer::Oride(CCObject* obj) { 154 155 // 添加半透明的背景 156 layerBg = CCLayerColor::create(ccc4(0,0,0,127)); 157 this->addChild(layerBg); 158 159 // 1. 创建点9图 160 CCScale9Sprite* input = CCScale9Sprite::create("input.png"); 161 // 2. 创建输入框 162 edit_id = CCEditBox::create( 163 CCSizeMake(220,40), 164 input); 165 // 3.添加当前层 166 this->addChild(edit_id); 167 // 4.设置坐标 168 edit_id->setPosition(ccp(450,300)); 169 edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); 170 171 // 完成添加菜单项 172 CCMenuItemFont* item_add_done = CCMenuItemFont::create( 173 ChineseString::GBKToUTF8("修改"), 174 this, 175 menu_selector(ListViewLayer::OrideCallBack)); 176 menu_add_done = CCMenu::create(item_add_done,NULL); 177 this->addChild(menu_add_done); 178 menu_add_done->setPosition(ccp(450,50)); 179 180 } 181 182 //产生更替信息 183 void ListViewLayer::OrideCallBack(CCObject* obj) { 184 185 this->removeChild(edit_id); 186 this->removeChild(menu_add_done); 187 this->removeChild(layerBg); 188 189 190 bool flag = false; 191 192 int id = ChineseString::StringToInt(edit_id->getText()); 193 for(it = Manager::getInstance()->_vStudent.begin();it!=Manager::getInstance()->_vStudent.end();it++) { 194 Students* sTemp = *it; 195 if(id==sTemp->m_iNumber) { 196 flag = true; 197 break; 198 } 199 } 200 if(flag==true) { 201 // 添加半透明的背景 202 layerBg = CCLayerColor::create(ccc4(0,0,0,127)); 203 this->addChild(layerBg); 204 205 206 // 1. 创建点9图 207 CCScale9Sprite* input = CCScale9Sprite::create("input.png"); 208 // 2. 创建输入框 209 edit_id 210 = CCEditBox::create( 211 CCSizeMake(220,40), 212 input); 213 // 3.添加当前层 214 this->addChild(edit_id); 215 // 4.设置坐标 216 edit_id->setPosition(ccp(450,300)); 217 edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); 218 219 input = CCScale9Sprite::create("input.png"); 220 // 2. 创建输入框 221 edit_name = CCEditBox::create( 222 CCSizeMake(220,40), 223 input); 224 edit_name->setPlaceHolder(ChineseString::GBKToUTF8("请输入姓名:")); 225 // 3.添加当前层 226 this->addChild(edit_name); 227 // 4.设置坐标 228 edit_name->setPosition(ccp(450,200)); 229 230 input = CCScale9Sprite::create("input.png"); 231 // 2. 创建输入框 232 edit_sex = CCEditBox::create( 233 CCSizeMake(220,40), 234 input); 235 edit_sex->setPlaceHolder(ChineseString::GBKToUTF8("请输入性别:")); 236 // 3.添加当前层 237 this->addChild(edit_sex); 238 // 4.设置坐标 239 edit_sex->setPosition(ccp(450,100)); 240 241 // 完成添加菜单项 242 CCMenuItemFont* item_add_done = CCMenuItemFont::create( 243 ChineseString::GBKToUTF8("修改"), 244 this, 245 menu_selector(ListViewLayer::OrideInf)); 246 menu_add_done = CCMenu::create(item_add_done,NULL); 247 this->addChild(menu_add_done); 248 menu_add_done->setPosition(ccp(450,50)); 249 } 250 else { 251 // 显示详细内容 252 253 ttf = CCLabelTTF::create( 254 ChineseString::GBKToUTF8(CCString::createWithFormat 255 ("%d %s",id,"这个ID不存在")->getCString()), 256 "Arial", 257 24); 258 this->addChild(ttf); 259 ttf->setPosition(ccp(100,100)); 260 } 261 } 262 263 void ListViewLayer::OrideInf(CCObject* obj) { 264 265 int id2 = ChineseString::StringToInt(edit_id->getText()); 266 char* name = (char*)ChineseString::UTF8ToGBK(edit_name->getText()); 267 char sex = 'M'; 268 Students* s = new Students(id2,name,sex); 269 *it = s; 270 271 this->removeChild(ttf); 272 this->removeChild(edit_id); 273 this->removeChild(edit_name); 274 this->removeChild(edit_sex); 275 this->removeChild(menu_add_done); 276 this->removeChild(layerBg); 277 278 MySort(); 279 pTableView->reloadData(); 280 } 281 282 // 添加按钮回调函数 283 void ListViewLayer::AddCallBack(CCObject* obj) 284 { 285 // 添加半透明的背景 286 layerBg = CCLayerColor::create(ccc4(0,0,0,127)); 287 this->addChild(layerBg); 288 289 290 // 1. 创建点9图 291 CCScale9Sprite* input = CCScale9Sprite::create("input.png"); 292 // 2. 创建输入框 293 edit_id = CCEditBox::create( 294 CCSizeMake(220,40), 295 input); 296 // 3.添加当前层 297 this->addChild(edit_id); 298 // 4.设置坐标 299 edit_id->setPosition(ccp(450,300)); 300 edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); 301 302 input = CCScale9Sprite::create("input.png"); 303 // 2. 创建输入框 304 edit_name = CCEditBox::create( 305 CCSizeMake(220,40), 306 input); 307 edit_name->setPlaceHolder(ChineseString::GBKToUTF8("请输入姓名:")); 308 // 3.添加当前层 309 this->addChild(edit_name); 310 // 4.设置坐标 311 edit_name->setPosition(ccp(450,200)); 312 313 input = CCScale9Sprite::create("input.png"); 314 // 2. 创建输入框 315 edit_sex = CCEditBox::create( 316 CCSizeMake(220,40), 317 input); 318 edit_sex->setPlaceHolder(ChineseString::GBKToUTF8("请输入性别:")); 319 // 3.添加当前层 320 this->addChild(edit_sex); 321 // 4.设置坐标 322 edit_sex->setPosition(ccp(450,100)); 323 324 // 完成添加菜单项 325 CCMenuItemFont* item_add_done = CCMenuItemFont::create( 326 ChineseString::GBKToUTF8("添加"), 327 this, 328 menu_selector(ListViewLayer::AddDoneCallBack)); 329 menu_add_done = CCMenu::create(item_add_done,NULL); 330 this->addChild(menu_add_done); 331 menu_add_done->setPosition(ccp(450,50)); 332 } 333 334 // 添加按钮回调函数(字符添加) 335 void ListViewLayer::AddDoneCallBack(CCObject* obj) 336 { 337 this->removeChild(ttf); 338 // 处理添加信息 339 340 int id = ChineseString::StringToInt(edit_id->getText()); 341 char* name = (char*)ChineseString::UTF8ToGBK(edit_name->getText()); 342 343 char sex = edit_sex->getText()[0]; 344 Students* s = new Students(id,name,sex); 345 Manager::getInstance()->_vStudent.push_back(s); 346 // 关闭对话框 347 this->removeChild(edit_id); 348 this->removeChild(edit_name); 349 this->removeChild(edit_sex); 350 this->removeChild(menu_add_done); 351 this->removeChild(layerBg); 352 // 刷新列表 353 354 MySort(); 355 356 pTableView->reloadData(); 357 } 358 359 360 void ListViewLayer::AddDel(CCObject* obj) { 361 // 添加半透明的背景 362 layerBg = CCLayerColor::create(ccc4(0,0,0,127)); 363 this->addChild(layerBg); 364 365 // 1. 创建点9图 366 CCScale9Sprite* input = CCScale9Sprite::create("input.png"); 367 // 2. 创建输入框 368 edit_id = CCEditBox::create( 369 CCSizeMake(220,40), 370 input); 371 // 3.添加当前层 372 this->addChild(edit_id); 373 // 4.设置坐标 374 edit_id->setPosition(ccp(450,300)); 375 edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); 376 377 // 完成添加菜单项 378 CCMenuItemFont* item_add_done = CCMenuItemFont::create( 379 ChineseString::GBKToUTF8("删除"), 380 this, 381 menu_selector(ListViewLayer::DelDoneCallBack)); 382 menu_add_done = CCMenu::create(item_add_done,NULL); 383 this->addChild(menu_add_done); 384 menu_add_done->setPosition(ccp(450,50)); 385 386 } 387 388 void ListViewLayer::DelDoneCallBack(CCObject* obj) { 389 int id = ChineseString::StringToInt(edit_id->getText()); 390 391 bool flag = false; 392 393 vector<Students*>::iterator it; 394 for(it=Manager::getInstance()->_vStudent.begin(); 395 it!=Manager::getInstance()->_vStudent.end();it++) { 396 Students* sTemp = *it; 397 if (id==sTemp->m_iNumber) 398 { 399 // 删除元素 400 Manager::getInstance()->_vStudent.erase(it); 401 flag = true; 402 break; 403 } 404 } 405 406 if(flag==false) { 407 this->removeChild(ttf); 408 ttf = CCLabelTTF::create( 409 ChineseString::GBKToUTF8(CCString::createWithFormat 410 ("%d %s",id,"这个ID不存在")->getCString()), 411 "Arial", 412 24); 413 this->addChild(ttf); 414 ttf->setPosition(ccp(100,100)); 415 } 416 417 //关闭 418 this->removeChild(edit_id); 419 this->removeChild(menu_add_done); 420 this->removeChild(layerBg); 421 422 MySort(); 423 pTableView->reloadData(); 424 425 } 426 427 bool cmpById(Students* a,Students *b) { 428 if(a->m_iNumber<b->m_iNumber) 429 return true; 430 return false; 431 } 432 433 void ListViewLayer::MySort () { 434 std::sort(Manager::getInstance()->_vStudent.begin(),Manager::getInstance()->_vStudent.end(),cmpById); 435 } 436 437 438 // cell被点击后的回调函数 439 void ListViewLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell) 440 { 441 pTableView->reloadData(); 442 } 443 444 //这里设置每个cell的大小 445 CCSize ListViewLayer::cellSizeForTable(CCTableView *table) 446 { 447 return CCSizeMake(600, 60); 448 } 449 450 CCTableViewCell* ListViewLayer::tableCellAtIndex(CCTableView *table, unsigned int idx) 451 { 452 /* 453 获得一个可用的cell,因为在我们滑动cell的时候有些cell是显示的, 454 有些不是显示出来的,把没有渲染的cell拿过来,这样就不用重新new出一个cell了, 455 可以减小内存的开销 456 */ 457 Students* sTmep = Manager::getInstance()->_vStudent[idx]; 458 CCString *pString = CCString::createWithFormat( 459 "%d %s %c", sTmep->m_iNumber , 460 sTmep->m_strName, sTmep->m_sex); 461 CCTableViewCell *pCell = table->dequeueCell(); 462 if (!pCell) { 463 pCell = new CCTableViewCell(); 464 pCell->autorelease(); 465 // 单个选项背景 466 CCSprite *pSprite = CCSprite::create("listitem.png"); 467 pSprite->setAnchorPoint(CCPointZero); 468 pSprite->setPosition(CCPointZero); 469 pCell->addChild(pSprite); 470 pSprite->setScale(0.5f); 471 472 CCLabelTTF *pLabel = CCLabelTTF::create(ChineseString::GBKToUTF8(pString->getCString()), "楷体", 20.0); 473 pLabel->setColor(ccc3(255, 255, 0)); 474 pLabel->setFontSize(48); 475 pLabel->setPosition(ccp(20,20)); 476 pLabel->setAnchorPoint(CCPointZero); 477 pLabel->setTag(123); 478 pCell->addChild(pLabel); 479 } 480 else//获得是没有渲染出来的cell,cell中原有的内容还存在 481 { 482 CCLabelTTF *pLabel = (CCLabelTTF*)pCell->getChildByTag(123); 483 pLabel->setString(ChineseString::GBKToUTF8(pString->getCString())); 484 } 485 return pCell; 486 } 487 488 // 这里设置一共有多少个cell 489 unsigned int ListViewLayer::numberOfCellsInTableView(CCTableView *table) 490 { 491 // 返回容器的大小 492 return Manager::getInstance()->_vStudent.size(); 493 } 494 495 void ListViewLayer::scrollViewDidScroll(CCScrollView *view) 496 { 497 } 498 499 void ListViewLayer::scrollViewDidZoom(CCScrollView *view) 500 { 501 }