[Ocos2d-x Game Engine Development Notes] Tiled Map Editor

Source: Internet
Author: User

1. Add enemies to the object Layer and add enemies,
Note that a key-value pair is added. The key is n and the value is 1. This key-value pair is mainly used to distinguish between enemies and heroes. Draw enemies on the map. [Cpp] for (int I = 0; I <int (objects-> getObjects ()-> count (); I ++) {CCDictionary * enemy = (CCDictionary *) objects-> getObjects ()-> objectAtIndex (I); if (enemy-> valueForKey ("n")-> intValue () = 1) {CCSprite * s = CCSprite:: create ("enemy1.png"); float x = enemy-> valueForKey ("x")-> floatValue (); float y = enemy-> valueForKey ("y ") -> floatValue (); s-> setPosition (ccp (x, y); s-> setAnchorPoint (CCPoint (0, 0); _ tileMap-> addChild (s, 4); CCActionInterval * move = CCMoveBy: create (2, CCPoint (_ player-> getPosition (). x-s-> getPosition (). x> 0? 10:-10, _ player-> getPosition (). y-s-> getPosition (). y> 0? 10:-10); // CCFiniteTimeAction * func = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: goon); s-> runAction (CCSequence :: create (move, func, NULL) ;}}for (int I = 0; I <int (objects-> getObjects ()-> count (); I ++) {CCDictionary * enemy = (CCDictionary *) objects-> getObjects ()-> objectAtIndex (I); if (enemy-> valueForKey ("n")-> intValue () = 1) {CCSprite * s = CCSprite: create ("enemy1.png"); float x = enemy-> valueForKey ("x ")-> FloatValue (); float y = enemy-> valueForKey (" y ")-> floatValue (); s-> setPosition (ccp (x, y )); s-> setAnchorPoint (CCPoint (0, 0); _ tileMap-> addChild (s, 4); CCActionInterval * move = CCMoveBy: create (2, CCPoint (_ player-> getPosition (). x-s-> getPosition (). x> 0? 10:-10, _ player-> getPosition (). y-s-> getPosition (). y> 0? 10:-10); // CCFiniteTimeAction * func = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: goon); s-> runAction (CCSequence :: create (move, func, NULL) ;}} this code obtains all objects from the object Layer, judges whether the key is 1 for n, if yes, It is an enemy, and adds it to the map, and let the enemy exercise. Call the goon function: [cpp] void HelloWorld: goon (CCNode * pSender) {CCSprite * s = (CCSprite *) pSender; CCActionInterval * move = CCMoveBy :: actionWithDuration (2, CCPoint (_ player-> getPosition (). x-s-> getPosition (). x> 0? 10:-10, _ player-> getPosition (). y-s-> getPosition (). y> 0? 10:-10); // CCFiniteTimeAction * func = CCCallFuncN: actionWithTarget (this, callfuncN_selector (HelloWorld: goon); s-> runAction (CCSequence :: actions (move, func, NULL);} void HelloWorld: goon (CCNode * pSender) {CCSprite * s = (CCSprite *) pSender; CCActionInterval * move = CCMoveBy :: actionWithDuration (2, CCPoint (_ player-> getPosition (). x-s-> getPosition (). x> 0? 10:-10, _ player-> getPosition (). y-s-> getPosition (). y> 0? 10:-10); // CCFiniteTimeAction * func = CCCallFuncN: actionWithTarget (this, callfuncN_selector (HelloWorld: goon); s-> runAction (CCSequence :: actions (move, func, NULL);} continues to call itself in the goon function, so that the enemy keeps moving to the hero. 2. so that the hero can attack the current game click is to make the hero move to the specified location, so in order to make the hero have an attack, you can add a status, if the status is true, control the hero movement, if the status is false, control the bullets. Add [cpp] bool mode; to the header file, add the menu [cpp] CCMenuItem * oon, * ooff; oon = CCMenuItemImage: create ("projectile-button-on.png ", "projectile-button-on.png"); ooff = CCMenuItemImage: create ("projectile-button-off.png", "projectile-button-off.png"); CCMenuItemToggle * toggle = CCMenuItemToggle: createWithTarget (this, // The class menu_selector (HelloWorld: toggleGame) where the callback function is located, // The callback function ooff, oon, NULL // The added menu); CCMenu * menu = CCMenu: create (toggle, NULL); menu-> setPosition (ccp (oon-> getContentSize (). width/2, oon-> getContentSize (). height/2); addChild (menu); CCMenuItem * oon, * ooff; oon = CCMenuItemImage: create ("projectile-button-on.png", "projectile-button-on.png"); ooff = CCMenuItemImage :: create ("projectile-button-off.png", "projectile-button-off.png"); CCMenuItemToggle * toggle = CCMenuItemToggle: createWithTarget (this, // The class me where the callback function is located Nu_selector (HelloWorld: toggleGame), // callback function ooff, oon, NULL // added menu); CCMenu * menu = CCMenu: create (toggle, NULL ); menu-> setPosition (ccp (oon-> getContentSize (). width/2, oon-> getContentSize (). height/2); addChild (menu); In the toggleGame function, add the code to control status changes: [cpp] view plaincopyprint? Void HelloWorld: toggleGame (CCObject * pSender) {mode = mode? False: true;} void HelloWorld: toggleGame (CCObject * pSender) {mode = mode? False: true;} When the status is false, hero emits a bullet: [cpp] else {CCSprite * s = CCSprite: create ("Projectile.png "); s-> setPosition (_ player-> getPosition (); _ tileMap-> addChild (s, 4); float dx = pp. x-_ player-> getPosition (). x; float dy = pp. y-_ player-> getPosition (). y; if (dx> 0) {float lx = 32*30-_ player-> getPosition (). x; float ly = dy/dx * lx; CCActionInterval * move = CCMoveBy: create (3, ccp (lx + s-> getContentSize (). width, ly); CCFiniteT ImeAction * ff = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: targetFinish); s-> runAction (CCSequence: actions (move, ff, NULL ));} else {float lx = 0-_ player-> getPosition (). x; float ly = dy/dx * lx; CCActionInterval * move = CCMoveBy: actionWithDuration (3, ccp (lx-s-> getContentSize (). width, ly); CCFiniteTimeAction * ff = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: targetFinish); s-> runAction (CCSequence: actions (move, ff, NULL);} else {CCSprite * s = CCSprite: create ("Projectile.png "); s-> setPosition (_ player-> getPosition (); _ tileMap-> addChild (s, 4); float dx = pp. x-_ player-> getPosition (). x; float dy = pp. y-_ player-> getPosition (). y; if (dx> 0) {float lx = 32*30-_ player-> getPosition (). x; float ly = dy/dx * lx; CCActionInterval * move = CCMoveBy: create (3, ccp (lx + s-> getContentSize (). width, ly); CCFiniteTimeAction * ff = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: targetFinish); s-> runAction (CCSequence: actions (move, ff, NULL ));} else {float lx = 0-_ player-> getPosition (). x; float ly = dy/dx * lx; CCActionInterval * move = CCMoveBy: actionWithDuration (3, ccp (lx-s-> getContentSize (). width, ly); CCFiniteTimeAction * ff = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: targetFinish); s-> runAction (CCSequence: actions (mo Ve, ff, NULL);} after the bullet is fired, let the bullet move out of the map and know the angle of the bullet through hero's position and click position, then, through the principle that the sides of a similar triangle are proportional, we can know the position where x exceeds the map. In this way, the bullet is removed from the map. 3. After work in the future, there will be something on the map now, and there will be only the problem of determining the outcome and defeat. Specifically, the hero wins after eating all the watermelons and loses when the enemy encounters them. This requires collision detection. In the cocos2d-x to judge the collision is very good to do, I just talk about the principle, the specific implementation of no difficulty. Add all enemies to a CCArray, add all bullets to another CCArray, and add a schedule to this CCLayer, perform Collision Detection on the enemy's CCArray and the bullet's CCArray at a certain time. If a collision occurs, remove the collision enemies and bullets, and detect the collision between the enemy and hero. If a collision occurs, the game ends. How to detect the collision? We can get the CCRect of the bullet, the CCRect of the enemy, and the CCRect of the hero. Use the CCRect member function [cpp] bool intersectsRect (const CCRect & rect) const bool intersectsRect (const CCRect & rect) const to determine the collision. Well, it's time to use Tiled to make the game. In the end, how can this game be better? Consider the following aspects: 1. add more enemies 2. add more items. 3. map grows and becomes more complex 4. add Level 5. make better game pictures 6. displays the life of a hero.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.