Previously Used version is 2.0.1 version, and later with 2.1.1 re-write a part, now brand new start, with the latest version, cocos2d-x-3.0beta, this version and the previous changes quite large.
This is the background material.
Program Implementation Method:
// ================ Attach a background map ============== auto bg = TMXTiledMap: create (bg. tmx); bg-> setPosition (Point (0, 0); this-> addChild (bg, 0 );
In this way, we can load the edited map to the scene, and then modify the bg. tmx file to record our route.
Method for initializing the route in the program:
auto map = TMXTiledMap::create(bg.tmx);auto group = map->getObjectGroup(path);auto& objects = group->getObjects();for (auto& obj : objects){ValueMap& dict = obj.asValueMap();int x = dict[x].asInt();int y = 480 - dict[y].asInt();wayPoint.push_back(Point(x,y));wayCount++;}
The wayPoint of the container records the points in the tmx file.
Well, now we have a background, a route, and a monster.
Monster material Diagram
Read method:
void Enemy::uploadEnemy(){//RightMove()Vector
tempRightMove;for (int i = 8; i < 12; i++){tempRightMove.pushBack(Myframes[i]);}Animation* aniRightMove = Animation::createWithSpriteFrames(tempRightMove,0.2f);actionRightMove = Animate::create(aniRightMove);actionRightMove->retain();//LeftMove()Vector
tempLeftMove;for (int i = 4; i < 8; i++){tempLeftMove.pushBack(Myframes[i]);}Animation* aniLeftMove = Animation::createWithSpriteFrames(tempLeftMove,0.2f);actionLeftMove = Animate::create(aniLeftMove);actionLeftMove->retain();//UpMove()Vector
tempUpMove;for (int i = 12; i < 16; i++){tempUpMove.pushBack(Myframes[i]);}Animation* aniUpMove = Animation::createWithSpriteFrames(tempUpMove,0.2f);actionUpMove = Animate::create(aniUpMove);actionUpMove->retain();//DownMove()Vector
tempDownMove;for (int i = 0; i < 4; i++){tempDownMove.pushBack(Myframes[i]);}Animation* aniDownMove = Animation::createWithSpriteFrames(tempDownMove,0.2f);actionDownMove = Animate::create(aniDownMove);actionDownMove->retain();}
Create a Monster:
Void GameScene: addEnemy () {// ================ Add a monster ================== auto enemy = Enemy :: create (); enemy-> setPosition (Tools: ConvertViewPoint (DataManage: getInstance ()-> wayPoint [0]); this-> addChild (enemy, 1 ); dataManage: getInstance ()-> v_enemy.push_back (enemy );}
Monster movement:
Void Enemy: run () {if (this-> iTurn <RoadCount-1 & this-> hp> 0) {Point point1 = DataManage: getInstance () -> wayPoint [iTurn]; Point point2 = DataManage: getInstance ()-> wayPoint [iTurn + 1]; Point p0 = Tools: ConvertViewPoint (point2 ); point p = this-> getPosition (); // frame animation if (point2.x-point1.x = 0) // vertical {if (point2.y-point1.y> = 0) {if (iChangeDire! = 1) {this-> stopAllActions (); this-> runAction (RepeatForever: create (actionDownMove); // 1 iChangeDire = 1;} p. y-= this-> speed * 1; if (p. y-p0.y <= 0) {iTurn + +; p = p0 ;}} else if (point2.y-point1.y <0) {if (iChangeDire! = 2) {this-> stopAllActions (); this-> runAction (RepeatForever: create (actionUpMove); // 2 iChangeDire = 2;} p. y + = this-> speed * 1; if (p. y-p0.y >=0) {iTurn ++; p = p0 ;}} else if (point2.y-point1.y = 0) // level {if (point2.x-point1.x> 0) {if (iChangeDire! = 3) {this-> stopAllActions (); this-> runAction (RepeatForever: create (actionRightMove); // 3 iChangeDire = 3;} p. x + = this-> speed * 1; if (p. x-p0.x> = 0) {iTurn ++; p = p0 ;}} else if (point2.x-point1.x <= 0) {if (iChangeDire! = 4) {this-> stopAllActions (); this-> runAction (RepeatForever: create (actionLeftMove); // 4 iChangeDire = 4;} p. x-= this-> speed * 1; if (p. x-p0.x <= 0) {iTurn + +; p = p0 ;}} this-> setPosition (p);} // else if (this-> iTurn> = RoadCount-1) /// {// this-> removeFromParentAndCleanup (true); // DataManage: getInstance ()-> v_enemy.erase (pos );//}}
The time when a monster appears is written in a plist.
level1
1
time
0
2
time
1
3
time
2
4
time
3
5
time
4
6
time
5
7
time
6
Read method:
__Dictionary* plistDic = __Dictionary::createWithContentsOfFile(level1.plist);__Dictionary* levelDic = dynamic_cast<__Dictionary*>(plistDic->objectForKey(level1));char str[10];for (int i=1;i<=levelDic->count();i++){sprintf_s(str,%d,i);__Dictionary* farScene = dynamic_cast<__Dictionary*>(levelDic->objectForKey(str));__String* spriteName = dynamic_cast<__String*>(farScene->objectForKey(time)); float time = spriteName->floatValue();v_time.push_back(time);}
Enable a timer in the main scenario to traverse all the monsters and make them appear and run.
void GameScene::update( float dt ){//==========enemy run================vector
::iterator it=DataManage::getInstance()->v_enemy.begin();while(it!=DataManage::getInstance()->v_enemy.end()){if((*it)->iTurn >= (*it)->RoadCount-1){(*it)->removeFromParentAndCleanup(true);it = DataManage::getInstance()->v_enemy.erase(it);}else{(*it)->run();it++;}}//========enemy show time===================vector
::iterator it_time=DataManage::getInstance()->v_time.begin();while(it_time!=DataManage::getInstance()->v_time.end()){float tmp = fabs(time-*it_time);if(( tmp>-0.0001) && (tmp < 0.0001)){addEnemy();it_time = DataManage::getInstance()->v_time.erase(it_time);}else{it_time++;}}time += 1.0/60;}
Coordinate Functions
Point Tools::ConvertViewPoint( Point p ){return Director::getInstance()->convertToGL(CCPoint(p.x * 60 + 30, p.y * 60));}
View the running diagram
This article ends.